A Roundup of Algorithms and Animations
Bubble Sort
Bubble sort is a simple sorting algorithm. It repeatedly walks through the list to be sorted, compares adjacent elements, and swaps them whenever they are out of order. This process continues until no swaps are needed anywhere in the list, producing a fully sorted list.
Selection Sort
The basic idea is to select the smallest (or largest) element from the sequence to be sorted on each pass, place it at the beginning of the sequence, and repeat until every element has been sorted. javajavajavajavajava

Selection Sort
Insertion Sort
Treat the first element of the sequence to be sorted as a sorted sequence, and the elements from the second through the last as an unsorted sequence.
Scan the unsorted sequence from beginning to end, inserting each element you encounter into the appropriate position in the sorted sequence. (If the element being inserted equals an element in the sorted sequence, insert it after the equal element.)

img
Quick Sort
Quick sort is an efficient sorting algorithm developed by Tony Hoare. On average, sorting n elements requires Ο(nlogn) comparisons, while the worst case may require Ο(n²) comparisons, although that case is rare. In fact, quick sort is usually faster than other Ο(nlogn) algorithms because its inner loop can be implemented efficiently on most hardware architectures.
Quick sort uses divide and conquer to sort a sequence (list) by dividing it into two sub-lists. As a classic application of divide and conquer in sorting algorithms, quick sort is essentially an extension of bubble sort that applies divide and conquer recursively.
In the worst case, quick sort has a running time of O(n²), as when sorting an already ordered sequence. However, its amortized expected time is O(nlogn), and the constant factor hidden by the O(nlogn) notation is much smaller than that of merge sort, whose complexity is always O(nlogn). Thus, for the vast majority of random sequences with little inherent ordering, quick sort always outperforms merge sort.

Insert image description here
Shell Sort
Shell sort, also known as diminishing-increment sort, is an efficient improvement on insertion sort, but it is not a stable sorting algorithm.
Shell sort's improvements are based primarily on two key properties of insertion sort:
- Insertion sort is efficient on nearly sorted data: When the data is almost sorted, insertion sort can run in close to linear time.
- Limitations of insertion sort: Insertion sort can move only one element at a time, making it inefficient for large-scale unsorted data.
The basic idea behind Shell sort is:
- Grouped sorting: First, divide the sequence to be sorted into several smaller subsequences, then insertion-sort each subsequence. The subsequences are grouped using a decreasing gap. The initial gap is large and gradually shrinks as the sort progresses.
- Final sort: Once the gap shrinks to 1, all the records in the sequence are already “nearly sorted”; a final insertion sort of the entire sequence completes the process.
The key to Shell sort is choosing a suitable gap sequence. Different gap sequences have a significant impact on sorting efficiency. A common gap sequence starts at n/2 and gradually decreases to 1.

Image from the internet
Merge Sort
Merge sort is an efficient sorting algorithm based on divide and conquer. Its basic idea is to recursively split the sequence to be sorted into two subsequences until each contains just one element, then gradually merge those subsequences into a sorted sequence. Merge sort can be implemented either recursively from the top down or iteratively from the bottom up.

Please add an image description
Heap Sort
Heap sort is an efficient sorting algorithm based on the heap data structure. It uses the properties of a heap to sort data, typically with a max-heap or min-heap. The basic idea is to convert the sequence to be sorted into a heap, then repeatedly extract the maximum or minimum value and adjust the heap's structure, thereby sorting the data.
Basic Steps of Heap Sort
- Build the heap: Build the array to be sorted into a max-heap (or min-heap). In a max-heap, every parent node's value is greater than or equal to the values of its children. In a min-heap, every parent node's value is less than or equal to the values of its children.
Sort:
- Extract the root node (the maximum or minimum value) and swap it with the last element in the heap.
- Adjust the heap again so that it satisfies the heap property.
- Repeat these steps until the heap is empty.

img
Counting Sort
Counting sort is a sorting algorithm with linear time complexity, suited to cases where the range of integer values is limited. Its basic idea is to count how many times each element occurs and use those counts to determine the elements' positions, thereby sorting the data. The basic steps are as follows:
- Determine the range: First determine the maximum and minimum values to be sorted. This range determines the size of the count array.
- Create the count array: Create a count array based on the range of the values. Each array index represents a value to be sorted, and the value at that index records how many times it occurs.
- Count occurrences: Traverse the original data and increment the count at the corresponding array position for each element.
- Compute cumulative counts: Sum the count array cumulatively to determine the final position of each element.
- Produce the sorted result: Use the cumulative counts to place each element in the correct position and generate the sorted array.

img
Bucket Sort
Bucket sort is a distribution-based sorting algorithm that works particularly well when the input data is evenly distributed. It assigns elements to multiple buckets, sorts the elements in each bucket separately, and finally merges all the buckets in order to produce a sorted result.
Basic Steps of Bucket Sort
- Create the buckets: Create a set number of buckets based on the range and distribution of the input data. Each bucket represents an interval.
- Distribute the elements among the buckets: Traverse the input array and place each element in the appropriate bucket based on its value.
- Sort each bucket: Choose a suitable sorting algorithm (such as insertion sort or quick sort) to sort the elements in each bucket.
- Combine the sorted buckets: Concatenate the elements from each bucket in order to form the final sorted array.

f9c64c9a4005888a34d6924613556902.gif
Radix Sort
Radix sort is a non-comparison-based integer sorting algorithm suited to sorting integers or fixed-length strings. It sorts by place value over multiple passes, from the least significant digit (LSD, Least Significant Digit) to the most significant digit (MSD, Most Significant Digit), ultimately producing a fully sorted array.
Basic Idea of Radix Sort
The core idea of radix sort is to sort numbers by digit, either from low to high or from high to low. Because radix sort depends on sorting by each digit, it generally relies on a stable sorting algorithm (such as counting sort or bucket sort) to preserve the relative order of elements with the same digit after each pass.
Steps of Radix Sort
- Determine the number of digits in the maximum value: Find the number of digits in the largest element in the array; this determines the number of passes.
- Sort from the least significant digit: Start with the least significant digit and sort by the digit at each position. You can use a stable sorting algorithm, such as counting sort.
- Repeat: Sort by each successively higher digit until you reach the most significant digit.
- Final sorted result: After all digit positions have been sorted, the array is in order.
Example
Suppose we are radix sorting the integer array [170, 45, 75, 90, 802, 24, 2, 66]:
- Determine the number of digits in the maximum value: The maximum value
802has 3 digits, so sorting takes 3 passes. First pass (sort by the ones digit):
- Sorted result:
[170, 90, 802, 2, 24, 45, 75, 66]
- Sorted result:
Second pass (sort by the tens digit):
- Sorted result:
[802, 2, 24, 45, 66, 170, 75, 90]
- Sorted result:
Third pass (sort by the hundreds digit):
- Sorted result:
[2, 24, 45, 66, 75, 90, 170, 802]
- Sorted result:

img