Many algorithms (like Binary Search) require data to be sorted to work. Sorting is a fundamental problem in computer science. Learning how to sort data efficiently is crucial for building fast software.
A Sorting Algorithm is a method for reorganizing a large number of items into a specific order, such as highest to lowest, or vice-versa.
Repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The largest elements 'bubble' to the top.
Divides the list into a sorted and an unsorted region. Repeatedly selects the smallest element from the unsorted region and moves it to the sorted region.
Both Bubble and Selection Sort are slow for large lists (O(n²)). There are much faster algorithms like Quick Sort and Merge Sort!
A common mistake is forgetting that swapping two variables requires a temporary third variable! If you try 'A = B' then 'B = A', both variables will end up with the same value.
Select an algorithm and watch how the bars are sorted step-by-step!
Test your knowledge on Sorting!
Sorting is so fundamental that there are dozens of algorithms! In 1945, John von Neumann invented 'Merge Sort', a highly efficient algorithm still used today. Python and Java currently use 'TimSort', a hybrid algorithm invented in 2002 by Tim Peters.