Top 10 Classic Sorting Algorithms

Common Classical Sorting Algorithms

Image

  • Nonlinear time comparison sorting: The relative order between elements is determined by comparison. Since its time complexity cannot exceed O(nlogn), it is called nonlinear time comparison sorting.

  • Linear-time non-comparative sorting: The relative order between elements is not determined by comparison. It can break through the time lower bound based on comparative sorting and run in linear time, so it is called linear-time non-comparative sorting.

Time Complexity & Space Complexity & Stability

Image

  • Stable: If a was originally in front of b, and a=b, a is still in front of b after sorting.

  • Unstable: if a was originally in front of b, and a=b, a may appear after b after sorting.

  • Time complexity: The total number of operations on sorted data. It reflects the regularity of the number of operations when n changes.

  • Space complexity: a measure of the size of the storage space temporarily occupied by an algorithm during its operation, denoted as S(n)=O(f(n)).


Top 10 Classic Sorting Algorithms

Bubble Sort

Bubble Sort is a relatively simple sorting algorithm in the field of computer science.

It repeatedly walks through the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until no more exchanges are needed, that is, the sequence has been sorted.

The name of this algorithm comes from the fact that larger elements will slowly "float" to the top of the sequence by swapping, hence the name "bubble sort".

Image

Selection Sort

Compare the size of the previous element and the next element in the array. If the latter element is smaller than the previous element, a variable k is used to remember its position, and then the second comparison, the previous "next element" now becomes "Previous element", continue to compare with his "next element". If the latter element is smaller than him, use the variable k to remember its position (subscript) in the array. When the loop ends, we should Find the subscript of the smallest number, and then make a judgment. If the subscript of this element is not the subscript of the first element, let the first element exchange the value with him, so as to find the smallest value in the entire array Counted. Then find the second smallest number in the array, let it swap the value with the second element in the array, and so on.

Image

Insertion Sort (Insertion Sort) There is an already-ordered data sequence, and a number is required to be inserted into the already-ordered data sequence, but it is required that the data sequence is still in order after insertion. At this time, a new Sorting method - insertion sorting method, the basic operation of insertion sorting is to insert a data into the sorted ordered data, so as to obtain a new ordered data with the number plus one, the algorithm is suitable for a small amount of data. Sorting, the time complexity is O(n^2). is a stable sorting method. The insertion algorithm divides the array to be sorted into two parts: the first part contains all elements of the array, except for the last element (so that the array has one more space to insert the position), and the second part contains only this one element (ie the element to be inserted). After the first part is sorted, insert this last element into the sorted first part.

The basic idea of ​​insertion sort is: insert a record to be sorted in each step, according to the size of its key code value, into the appropriate position in the previously sorted file until all the records are inserted.

Image

Shell Sort

Shell's Sort is a kind of insertion sort, also known as "Diminishing Increment Sort", which is a more efficient and improved version of the direct insertion sorting algorithm. Hill sort is an unstable sorting algorithm. This method is named after DLShell was proposed in 1959.

Hill sorting is to group the records by a certain increment of the subscript, and use the direct insertion sorting algorithm to sort each group; as the increment gradually decreases, each group contains more and more keywords. When the increment is reduced to 1, The entire file is just grouped together, and the algorithm terminates.

Image

Merge Sort

Merge sort (MERGE-SORT) is an efficient sorting algorithm based on the merge operation, which is a very typical application of the divide and conquer method (Divide and Conquer). Merge the ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence ordered, and then make the subsequence segments ordered. If two sorted lists are merged into one sorted list, it is called two-way merge.

Image

Quick Sort

Quicksort is an improvement on bubble sort.

Quicksort was proposed by CAR Hoare in 1962. Its basic idea is: divide the data to be sorted into two independent parts by one sorting, all the data in one part is smaller than all the data in the other part, and then use this method to quickly perform the two parts of the data respectively. Sorting, the entire sorting process can be performed recursively, so that the entire data becomes an ordered sequence.

Image

Heap Sort

Heapsort (Heapsort) refers to a sorting algorithm designed using the data structure of a stacking tree (heap), which is a kind of selection sort. You can use the characteristics of the array to quickly locate the element at the specified index. The heap is divided into a large root heap and a small root heap, which is a complete binary tree. The requirement of the big root heap is that the value of each node is not greater than the value of its parent node, that is, A[PARENT[i]] >= A[i]. In the non-descending sorting of the array, the big root heap needs to be used, because according to the requirements of the big root heap, the largest value must be at the top of the heap.

Image

Counting Sort

Counting sort is a non-comparison-based sorting algorithm proposed in 1954 by Harold H. Seward. Its advantage is that when sorting a range of integers, its complexity is Ο(n+k) (where k is the range of integers), which is faster than any comparison sorting algorithm.

Of course, this is a way of sacrificing space for time, and when O(k)>O(n log(n)), its efficiency is not as good as comparison-based sorting (the time complexity of comparison-based sorting is theoretical The lower bound is O(n log(n)), such as merge sort, heap sort)

Image

Bucket Sort

Bucket sort, or so-called bin sort, is a sorting algorithm that works by dividing an array into a finite number of buckets. Each bucket is then sorted individually (possibly using another sorting algorithm or recursively continuing to use bucket sort to sort). Bucket sort is an inductive result of pigeonhole sort. Bucket sort uses linear time (Θ(n)) when the values ​​in the array to be sorted are evenly distributed. But bucket sort is not a comparison sort, it is not affected by the O(n log n) lower bound.

Image

Radix Sort

Radix sort (radix sort) belongs to "distribution sort", also known as "bucket sort" or bin sort. As the name suggests, it assigns the elements to be sorted through partial information of key values. To some "buckets", in order to achieve the function of sorting, the radix sorting method is a stable sorting, and its time complexity is O (nlog(r)m), where r is the cardinality taken, and m is the heap. Number, in some cases, the efficiency of the radix sorting method is higher than other stable sorting methods.

Image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324608966&siteId=291194637
Recommended