Sorting algorithms are a set of algorithms that are designed to arrange a collection of elements in a specific order. The most common order is numerical or alphabetical, but it can also be any other logical ordering, such as dates, frequencies, or priorities.
Sorting algorithms vary in their complexity, efficiency, and the types of collections they can handle. Some algorithms are simple and efficient for small collections, while others are more complex but can handle large collections quickly. The choice of sorting algorithm depends on the requirements of the specific use case.
Sorting algorithms can be divided into two main categories: comparison-based and non-comparison-based. Comparison-based algorithms compare pairs of elements in the collection and swap them if necessary until the entire collection is sorted. Non-comparison-based algorithms do not compare elements directly, but instead use properties of the elements to sort them.
Some common sorting algorithms include:
- Bubble Sort: Compares adjacent elements and swaps them if they are in the wrong order. Repeatedly iterates through the collection until no more swaps are necessary.
- Selection Sort: Finds the smallest element in the unsorted part of the collection and puts it at the beginning. Repeatedly iterates through the collection until all elements are sorted.
- Insertion Sort: Builds the final sorted collection one item at a time by inserting each item into its proper position. Compares each item with the previous items and inserts it in the correct location.
- Merge Sort: Divides the collection into two halves, sorts each half, and then merges the two sorted halves into one. Recursively divides the collection until the base case is reached.
- Quick Sort: Chooses a pivot element and partitions the collection around the pivot, putting all smaller elements before it and all larger elements after it. Recursively applies the same process to the two resulting sub-collections.
- Heap Sort: Builds a binary heap and repeatedly extracts the largest element from the heap and puts it at the end of the collection. Repeatedly applies the same process until the heap is empty.
- Counting Sort: Counts the number of occurrences of each element and uses that information to build the sorted collection.
- Radix Sort: Sorts elements based on their digits or bits, starting from the least significant digit/bit and working up to the most significant one.
- Bucket Sort: Divides the range of values into a set of buckets, puts each element into its corresponding bucket, and then sorts each bucket.
- Shell Sort: Sorts elements by comparing elements that are far apart, and then gradually reduces the gap between them until they are adjacent.
- Comb Sort: Compares elements that are far apart, and then gradually reduces the gap between them until it becomes 1, at which point it switches to a bubble sort.
- Gnome Sort: Compares adjacent elements and swaps them if they are in the wrong order, but also moves the cursor back one step if necessary to ensure that all adjacent pairs are compared.
- Cocktail Sort: Similar to bubble sort, but also alternates between moving the largest element to the end and moving the smallest element to the beginning.
- Odd-Even Sort: Similar to cocktail sort, but only compares elements at odd and even positions.
- Pancake Sort: Sorts elements by repeatedly flipping the prefix of the collection until the largest element is at the beginning, and then flipping the entire collection.
- Cycle Sort: Sorts elements by finding the correct position for each element and then rotating the cycle to put the element in that position.
- Stooge Sort: Recursively divides the collection into thirds and swaps the first and last thirds if necessary, then applies the same process to the remaining sub-collections.
- Tim Sort: Combines merge sort and insertion sort, using insertion sort to sort small sub-collections and merge sort to sort larger ones.
- Gravity Sort: Sorts elements by repeatedly moving them down until they hit a barrier, and then moving them back up in the correct order.
- Bead Sort: Sorts elements by using gravity to move beads down a set of rods, representing the elements in the collection.