Understanding Bubble sort Using The Number Guessing Game
Before diving into sorting algorithms, let us first understand the application we are working with: a Number Guessing Game. This will help give context to why sorting is needed. A Number Guessing Game is a simple interactive game where: The system generates a random number within a given range (for
Christina Sharon S
Before diving into sorting algorithms, let us first understand the application we are working with: a Number Guessing Game. This will help give context to why sorting is needed.
A Number Guessing Game is a simple interactive game where:
- The system generates a random number within a given range (for example, 1 to 100)
- The player tries to guess the number
-
After each guess, the system provides feedback:
- "Too high" if the guess is greater than the number
- "Too low" if the guess is smaller than the number
The game continues until the player guesses the correct number
To make the game more interesting, it can include different difficulty levels:
- Easy: Smaller range (e.g., 1โ50)
- Medium: Moderate range (e.g., 1โ100)
- Hard: Larger range (e.g., 1โ200)
Higher difficulty levels make the game more challenging.
Leaderboard Concept
To track player performance, we introduce a leaderboard.
Each playerโs result is stored with:
- Name
- Difficulty level
- Number of attempts taken to guess correctly
Example:
Alice Hard 7
Bob Easy 3
Charlie Medium 5
David Easy 2
Why Sorting is Needed
The leaderboard should display players in a meaningful order:
- Players who chose easier difficulty should appear first
- If two players have the same difficulty, the one with fewer attempts should rank higher
To achieve this, we need a sorting algorithm.
Introduction to Bubble Sort
Bubble Sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order.
The process continues until the entire list is sorted.
How Bubble Sort Works
- Start from the first element in the list
- Compare it with the next element
- Swap them if they are not in the correct order
- Move to the next pair
- Repeat this process for the entire list
After one complete pass, the largest element moves to the end.
This process is repeated for the remaining elements until the list is fully sorted.
Example
Consider the list:
[5, 2, 4]
First pass:
- Compare 5 and 2 โ swap โ [2, 5, 4]
- Compare 5 and 4 โ swap โ [2, 4, 5]
Second pass:
- Compare 2 and 4 โ no swap
Final sorted list:
[2, 4, 5]
Applying Bubble Sort to the Leaderboard
In our leaderboard:
- We first compare players based on difficulty
- If difficulty is the same, we compare based on number of attempts
- We swap players if they are not in the correct order
This process continues until the leaderboard is properly sorted.
Python Implementation
Leaderboard data
leaderboard = [
{"name": "Alice", "difficulty": "Hard", "attempts": 7},
{"name": "Bob", "difficulty": "Easy", "attempts": 3},
{"name": "Charlie", "difficulty": "Medium", "attempts": 5},
{"name": "David", "difficulty": "Easy", "attempts": 2}
]
Convert difficulty into numeric value
def get_difficulty_value(level):
if level == "Easy":
return 1
elif level == "Medium":
return 2
else:
return 3
Bubble sort function
def sort_leaderboard(data):
n = len(data)
for i in range(n):
for j in range(0, n - i - 1):
d1 = get_difficulty_value(data[j]["difficulty"])
d2 = get_difficulty_value(data[j + 1]["difficulty"])
if d1 > d2:
data[j], data[j + 1] = data[j + 1], data[j]
elif d1 == d2:
if data[j]["attempts"] > data[j + 1]["attempts"]:
data[j], data[j + 1] = data[j + 1], data[j]
return data
Output
sorted_list = sort_leaderboard(leaderboard)
for player in sorted_list:
print(player)
Conclusion
The Number Guessing Game provides a simple and practical use case for understanding sorting. By implementing a leaderboard and applying Bubble Sort, we can organize player data effectively.
Although Bubble Sort is not efficient for large datasets, it is an excellent starting point for learning how sorting algorithms work.
Found this useful? Share it!
Read the Full Story
Continue reading on Dev.to
Related Stories
Majority Element
about 2 hours ago
Building a SQL Tokenizer and Formatter From Scratch โ Supporting 6 Dialects
about 2 hours ago
Markdown Knowledge Graph for Humans and Agents
about 2 hours ago

Moving Beyond Disk: How Redis Supercharges Your App Performance
about 2 hours ago