Sorting is the process of arranging data in a specific order (ascending or descending). It's one of the most fundamental operations in computer science and is used everywhere - from organizing contacts to ranking search results.
Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they're in the wrong order. Larger values "bubble up" to the end.
Algorithm BubbleSort
var arr = [64, 34, 25, 12, 22, 11, 90]
var n = Size(arr)
Print "Original array:"
For i = 0 To n - 1
Print arr[i], " "
Endfor
Print ""
Print ""
# Bubble sort algorithm
For i = 0 To n - 2
For j = 0 To n - i - 2
# Compare adjacent elements
If arr[j] > arr[j + 1] Then
# Swap elements
var temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
Endif
Endfor
Endfor
Print "Sorted array:"
For i = 0 To n - 1
Print arr[i], " "
Endfor
Print ""
Endalgorithm
Pass 1: [64, 34, 25, 12, 22, 11, 90]
→ [34, 64, 25, 12, 22, 11, 90] (swap 64, 34)
→ [34, 25, 64, 12, 22, 11, 90] (swap 64, 25)
→ [34, 25, 12, 64, 22, 11, 90] (swap 64, 12)
... 90 bubbles to end
Pass 2: [34, 25, 12, 22, 11, 64, 90]
... 64 bubbles to second-last position
... continues until fully sorted
Selection Sort finds the minimum element and places it at the beginning, then repeats for the remaining unsorted portion.
Algorithm SelectionSort
var arr = [29, 10, 14, 37, 13]
var n = Size(arr)
Print "Original:"
For i = 0 To n - 1
Print arr[i], " "
Endfor
Print ""
Print ""
# Selection sort algorithm
For i = 0 To n - 2
# Find minimum in unsorted portion
var minIndex = i
For j = i + 1 To n - 1
If arr[j] < arr[minIndex] Then
minIndex = j
Endif
Endfor
# Swap minimum with first unsorted element
If minIndex != i Then
var temp = arr[i]
arr[i] = arr[minIndex]
arr[minIndex] = temp
Endif
Endfor
Print "Sorted:"
For i = 0 To n - 1
Print arr[i], " "
Endfor
Print ""
Endalgorithm
Insertion Sort builds the sorted array one item at a time by inserting each element into its correct position.
Algorithm InsertionSort
var arr = [12, 11, 13, 5, 6]
var n = Size(arr)
Print "Original:"
For i = 0 To n - 1
Print arr[i], " "
Endfor
Print ""
Print ""
# Insertion sort algorithm
For i = 1 To n - 1
var key = arr[i]
var j = i - 1
# Move elements greater than key one position ahead
While j >= 0 and arr[j] > key Do
arr[j + 1] = arr[j]
j = j - 1
Endwhile
# Insert key in correct position
arr[j + 1] = key
Endfor
Print "Sorted:"
For i = 0 To n - 1
Print arr[i], " "
Endfor
Print ""
Endalgorithm
Algorithm | Concept | Best For | Difficulty |
---|---|---|---|
Bubble Sort | Compare adjacent, swap if wrong order | Small arrays, learning | Easy |
Selection Sort | Find minimum, place at start | Small arrays, minimal swaps | Easy |
Insertion Sort | Insert each element in correct position | Nearly sorted data | Medium |
Algorithm Leaderboard
var players = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
var scores = [850, 1200, 750, 950, 1100]
var n = 5
Print "=== ORIGINAL SCORES ==="
For i = 0 To n - 1
Print players[i], ":", scores[i]
Endfor
# Sort by score (descending) using bubble sort
For i = 0 To n - 2
For j = 0 To n - i - 2
# Swap if current score is lower than next (descending)
If scores[j] < scores[j + 1] Then
# Swap scores
var tempScore = scores[j]
scores[j] = scores[j + 1]
scores[j + 1] = tempScore
# Swap corresponding player names
var tempName = players[j]
players[j] = players[j + 1]
players[j + 1] = tempName
Endif
Endfor
Endfor
Print ""
Print "=== LEADERBOARD (Highest to Lowest) ==="
For i = 0 To n - 1
Print "#", i + 1, "-", players[i], ":", scores[i]
Endfor
Endalgorithm