โ— LIVE
OpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leakedOpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leaked
๐Ÿ“… Sat, 21 Mar, 2026โœˆ๏ธ Telegram
AiFeed24

AI & Tech News

๐Ÿ”
โœˆ๏ธ Follow
๐Ÿ Home๐Ÿค–AI๐Ÿ’ปTech๐Ÿš€Startupsโ‚ฟCrypto๐Ÿ”’Security๐Ÿ‡ฎ๐Ÿ‡ณIndiaโ˜๏ธCloud๐Ÿ”ฅDeals
โœˆ๏ธ News Channel๐Ÿ›’ Deals Channel
Home/Cloud & DevOps/Understanding Bubble sort Using The Number Guessing Game
โ˜๏ธCloud & DevOps

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

โšกQuick SummaryAI generating...
C

Christina Sharon S

๐Ÿ“… Mar 21, 2026ยทโฑ 4 min readยทDev.to โ†—
โœˆ๏ธ Telegram๐• TweetWhatsApp
๐Ÿ“ก

Original Source

Dev.to

https://dev.to/christina_sharons_2b3205/understanding-bubble-sort-using-the-number-guessing-game-5833
Read Full โ†—

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:

  1. Players who chose easier difficulty should appear first
  2. 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.

Tags:#cloud#dev.to

Found this useful? Share it!

โœˆ๏ธ Telegram๐• TweetWhatsApp

Read the Full Story

Continue reading on Dev.to

Visit Dev.to โ†—

Related Stories

โ˜๏ธ
โ˜๏ธCloud & DevOps

Majority Element

about 2 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

Building a SQL Tokenizer and Formatter From Scratch โ€” Supporting 6 Dialects

about 2 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

Markdown Knowledge Graph for Humans and Agents

about 2 hours ago

Moving Beyond Disk: How Redis Supercharges Your App Performance
โ˜๏ธCloud & DevOps

Moving Beyond Disk: How Redis Supercharges Your App Performance

about 2 hours ago

๐Ÿ“ก Source Details

Dev.to

๐Ÿ“… Mar 21, 2026

๐Ÿ• about 5 hours ago

โฑ 4 min read

๐Ÿ—‚ Cloud & DevOps

Read Original โ†—

Web Hosting

๐ŸŒ Hostinger โ€” 80% Off Hosting

Start your website for โ‚น69/mo. Free domain + SSL included.

Claim Deal โ†’

๐Ÿ“ฌ AiFeed24 Daily

Top 5 AI & tech stories every morning. Join 40,000+ readers.

โœฆ 40,218 subscribers ยท No spam, ever

Cloud Hosting

โ˜๏ธ Vultr โ€” $100 Free Credit

Deploy cloud servers in 25+ locations. From $2.50/mo. No contract.

Claim $100 Credit โ†’
AiFeed24

India's AI-powered tech news hub. Daily coverage of AI, startups, crypto and emerging technology.

โœˆ๏ธ๐Ÿ›’

Topics

Artificial IntelligenceStartups & VCCryptocurrencyCybersecurityCloud & DevOpsIndia Tech

Company

About AiFeed24Write For UsContact

Daily Digest

Top 5 AI stories every morning. 40,000+ readers.

No spam, ever.

ยฉ 2026 AiFeed24 Media.Affiliate Disclosure โ€” We earn commission on qualifying purchases at no extra cost to you.
PrivacyTermsCookies