Bubble Sort Algorithm - Interactive Tutorial
Master the bubble sort algorithm with interactive visualization, step-by-step explanations, and hands-on TypeScript implementation. Perfect for beginners learning sorting algorithms.
Master the bubble sort algorithm with interactive visualization, step-by-step explanations, and hands-on TypeScript implementation. Perfect for beginners learning sorting algorithms.
Welcome to your interactive lesson on Bubble Sort! This fundamental sorting algorithm gets its name because smaller elements "bubble" to the top (beginning) of the list, much like bubbles rise in water. Let's explore how it works through visualization and hands-on practice.
Bubble Sort works by repeatedly comparing adjacent elements and swapping them if they're in the wrong order. After each pass through the array, the largest unsorted element "bubbles up" to its correct position at the end.
Note: Bubble sort modifies the original array directly without requiring extra space for another array.
1function bubbleSort<T>(arr: T[]): T[] {2 const n = arr.length;3 4 // Outer loop for each pass5 for (let i = 0; i < n - 1; i++) {6 let swapped = false;7 8 // Inner loop for comparisons in current pass9 for (let j = 0; j < n - i - 1; j++) {10 // Compare adjacent elements11 if (arr[j] > arr[j + 1]) {12 // Swap if they're in wrong order13 [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];14 swapped = true;15 }16 }17 18 // Optimization: if no swaps, array is sorted19 if (!swapped) {20 console.log(`Array sorted early at pass ${i + 1}`);21 break;22 }23 }24 25 return arr;26}27
28// Example usage with type annotations29const numbers: number[] = [64, 34, 25, 12, 22, 11, 90];30console.log("Original:", numbers);31console.log("Sorted:", bubbleSort([...numbers]));32
33// Works with other types too34const words: string[] = ["zebra", "apple", "orange", "banana"];35console.log("Sorted words:", bubbleSort([...words]));
1def bubble_sort(arr):2 n = len(arr)3 4 # Traverse through all array elements5 for i in range(n - 1):6 swapped = False7 8 # Last i elements are already in place9 for j in range(n - i - 1):10 # Swap if the element found is greater than the next11 if arr[j] > arr[j + 1]:12 arr[j], arr[j + 1] = arr[j + 1], arr[j]13 swapped = True14 15 # If no swapping happened, array is sorted16 if not swapped:17 print(f"Array sorted early at pass {i + 1}")18 break19 20 return arr21
22# Example usage23numbers = [64, 34, 25, 12, 22, 11, 90]24print("Original:", numbers)25print("Sorted:", bubble_sort(numbers.copy()))
Your task is to implement the bubble sort algorithm in TypeScript. The function should sort an array of elements in ascending order.
arr[j] > arr[j+1]
to arr[j] < arr[j+1]
str1.localeCompare(str2)
for comparison.Bubble Sort is a simple comparison-based sorting algorithm that's perfect for learning fundamental sorting concepts. While it's not efficient for large datasets due to its O(n²) time complexity, it remains valuable for educational purposes and small data sets.
Remember: The key insight is that after each pass, the largest element "bubbles up" to its correct position, and we can optimize by checking if any swaps occurred.
Next Steps: Try implementing the practice problems, then explore more efficient algorithms like Quick Sort or Merge Sort to see how they improve upon Bubble Sort's limitations.