In computer science a sorting algorithm is an algorithm that puts elements of a list in a certain order.
Almost any list that comes out of a computer is sorted into some sort of order, and there are many more sorted lists inside computers that the user doesn’t see. Many clever algorithms have been devised for putting values into order efficiently. Below are a few popular sorting algorithms:
Bubble Sort — Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements “bubble” to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.
Bubble Sort Complexity
Worst complexity: n²Average complexity: n²Best complexity: nSpace complexity: 1Method: ExchangingStable: YesClass: Comparison sort
Merge Sort — Merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.
Conceptually, a merge sort works as follows:
1)Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
2)Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
Merge Sort Complexity
Worst complexity: n*log(n)Average complexity: n*log(n)Best complexity: n*log(n)Space complexity: nMethod: MergingStable: Yes
Selection Sort — selection sort is a sorting algorithm, specifically an in-place comparison sort. The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
Selection Sort Complexity
Worst complexity: n²Average complexity: n²Best complexity: n²Space complexity: 1Method: SelectionStable: NoClass: Comparison sort
Heap Sort- heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. The improvement consists of the use of a heap data structure rather than a linear-time search to find the maximum. Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantage of a more favorable worst-case O(n log n) runtime.
The heapsort algorithm involves preparing the list by first turning it into a max heap. The algorithm then repeatedly swaps the first value of the list with the last value, decreasing the range of values considered in the heap operation by one, and sifting the new first value into its position in the heap. This repeats until the range of considered values is one value in length.
The steps are:
1) Call the buildMaxHeap() function on the list. Also referred to as heapify(), this builds a heap from a list in O(n) operations.
2)Swap the first element of the list with the final element. Decrease the considered range of the list by one.
3)Call the siftDown() function on the list to sift the new first element to its appropriate index in the heap.
4)Go to step (2) unless the considered range of the list is one element.
Heap Sort Complexity
Worst complexity: n*log(n)Average complexity: n*log(n)Best complexity: n*log(n)Space complexity: 1Method: SelectionStable: No
Quick Sort-Quicksort (sometimes called partition-exchange sort or Quick Sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort. Quicksort is a comparison sort, meaning that it can sort items of any type for which a “less-than” relation (formally, a total order) is defined.Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting. Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.
The steps are:
1) Pick an element, called a pivot, from the array.
2) Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
3) Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
The base case of the recursion is arrays of size zero or one, which never need to be sorted.
Worst complexity: n²Average complexity: n*log(n)Best complexity: n*log(n)Method: PartitioningStable: NoClass: Comparison sort
Insertion Sort-Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
1) Simple implementation: Bentley shows a three-line C version, and a five-line optimized version.
2) Efficient for (quite) small data sets, much like other quadratic sorting algorithms
3) More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort
4) Adaptive, i.e., efficient for data sets that are already substantially sorted: the time complexity is O(nk) when each element in the input is no more than k places away from its sorted position
5) Stable; i.e., does not change the relative order of elements with equal keys
6) In-place; i.e., only requires a constant amount O(1) of additional memory space
7) Online; i.e., can sort a list as it receives it
When people manually sort cards in a bridge hand, most use a method that is similar to insertion sort.
The most common variant of insertion sort, which operates on arrays, can be described as follows:
1) Suppose there exists a function called Insert designed to insert a value into a sorted sequence at the beginning of an array. It operates by beginning at the end of the sequence and shifting each element one place to the right until a suitable position is found for the new element. The function has the side effect of overwriting the value stored immediately after the sorted sequence in the array.
2)To perform an insertion sort, begin at the left-most element of the array and invoke Insert to insert each element encountered into its correct position. The ordered sequence into which the element is inserted is stored at the beginning of the array in the set of indices already examined. Each insertion overwrites a single value: the value being inserted.
Insertion Sort Complexity
Worst complexity: n²Average complexity: n²Best complexity: nSpace complexity: 1Method: InsertionStable: YesClass: Comparison sort
Thanks for reading this article, if you found it helpful please leave a few claps. I am a YouTuber called randerson112358, and I have many helpful videos on programming, computer science, & discrete mathematics, be sure to check them out !
Also a great book to prepare you for coding/programming interviews is called cracking the coding interview. It will help you familiarize yourself with sorting algorithms and problem solving, so be sure to check it out !