[Reprint] 8 major sorting algorithms

https://www.jianshu.com/p/e6ad4423efcd

Sorting algorithms can be divided into internal sorting and external sorting. Internal sorting is the sorting of data records in memory, while external sorting is because the data to be sorted is large and cannot accommodate all sorting records at one time, and external memory needs to be accessed during the sorting process.

Common internal sorting algorithms are: Insertion Sort, Hill Sort, Selection Sort, Bubble Sort, Merge Sort, Quick Sort, Heap Sort, Radix Sort, etc.

This article will introduce the above eight sorting algorithms in turn.

Algorithm 1: Insertion Sort

Insertion sort diagram

Insertion sort is one of the simplest and most intuitive sorting algorithms. Its working principle is to construct an ordered sequence. For unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert it.

Algorithm steps:

  1. Treat the first element of the first to-be-sorted sequence as one 有序序列, and treat the second to last element as yes 未排序序列.
  2. Scans sequentially from start to finish 未排序序列, inserting each element scanned into the appropriate position in the ordered sequence. (If the element to be inserted is equal to an element in the ordered sequence, the element to be inserted is inserted after the equal element.)

Algorithm 2: Hill sort

Hill sort diagram

Hill sort , also known as the decreasing-increment sorting algorithm , is a more efficient and improved version of insertion sort. But Hill sort is a non-stable sorting algorithm.

Hill sort is an improved method based on the following two properties of insertion sort:

  • Insertion sorting has high efficiency when operating on almost sorted data, that is, it can achieve the efficiency of linear sorting;
  • But insertion sort is generally inefficient because insertion sort can only move the data one bit at a time.

The basic idea of ​​Hill sorting is to first divide the entire sequence of records to be sorted into several sub-sequences for direct insertion sorting, and when the records in the entire sequence are "basically ordered", perform direct insertion sorting on all records in turn.

Algorithm steps :

  1. Choose an incremental sequence t1, t2, ..., tk, where ti>tj, tk=1;
  2. Sort the sequence by k times according to the number of incremental sequences k;
  3. For each sorting, according to the corresponding increment ti, the sequence to be sorted is divided into several sub-sequences of length m, and each sub-table is directly inserted and sorted. Only when the increment factor is 1, the entire sequence is treated as a table, and the table length is the length of the entire sequence.

Algorithm 3: Selection Sort

Selection sort diagram

Selection sort is also a simple and intuitive sorting algorithm.

Algorithm steps :

  1. First find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence;
  2. Continue to find the smallest (largest) element from the remaining unsorted elements, and put it at the end of the sorted sequence;
  3. Repeat step 2 until all elements are sorted.

Algorithm 4: Bubble Sort

Schematic diagram of bubble sort

Bubble Sort is also a simple and intuitive sorting algorithm. 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 the smaller elements are slowly "floated" to the top of the sequence by swapping.

Algorithm steps :

  1. Compare adjacent elements. If the first is bigger than the second, swap the two of them.
  2. Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. After this step, the last element will be the largest number.
  3. Repeat the above steps for all elements except the last one.
  4. Keep repeating the above steps for fewer and fewer elements each time, until there are no pairs of numbers to compare.

Algorithm 5: Merge Sort

Merge sort diagram

Merge sort (Merge sort) is an efficient sorting algorithm based on the merge operation. This algorithm is a very typical application of Divide and Conquer.

Algorithm steps:

  1. Apply for space to be the sum of the two sorted sequences, and this space is used to store the merged sequence;
  2. Set two pointers, the initial positions are the starting positions of the two sorted sequences;
  3. Compare the elements pointed to by the two pointers, select a relatively small element into the merge space, and move the pointer to the next position;
  4. Repeat step 3 until a pointer reaches the end of the sequence;
  5. Copies all remaining elements of the other sequence directly to the end of the merged sequence.

Details: Merge Sort

Algorithm 6: Quick Sort

Quick sort diagram

Quicksort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O ( n log n ) comparisons. O ( n 2) comparisons are required in the worst case , but this is not common. In fact, quicksort is usually significantly faster than other O ( n log n ) algorithms because its inner loop can be implemented efficiently on most architectures.

Quicksort uses a divide and conquer strategy to divide a list into two sub-lists.

Algorithm steps:

  1. Pick an element from the sequence, called a "pivot".
  2. Reorder the sequence, all elements smaller than the reference value are placed in front of the reference, and all elements larger than the reference value are placed at the back of the reference (same numbers can go to either side). After the partition exits, the benchmark is in the middle of the sequence. This is called a partition operation.
  3. Recursively sort the subarrays of elements smaller than the reference value and the subarrays of elements larger than the reference value.

The bottom case of recursion is that the size of the sequence is zero or one, which is always sorted. Although it keeps recursing, the algorithm will always exit, because in each iteration, it will put at least one element to its last position.

Details: Quick Sort

Algorithm 7: Heap Sort

Heap sort diagram

Heapsort (Heapsort) refers to a sorting algorithm designed using the data structure of the heap. Stacking is a structure that approximates a complete binary tree, and at the same time satisfies the property of stacking : that is, the key value or index of a child node is always less than (or greater than) its parent node.

The average time complexity of heapsort is Ο ( n log n ).

Algorithm steps:

  1. create a heap H[0..n-1];
  2. Swap heap head (maximum value) and heap tail;
  3. Reduce the size of the heap by 1, and call shift_down(0), the purpose is to adjust the top data of the new array to the corresponding position;
  4. Repeat step 2 until the size of the heap is 1.

Details: Heap Sort

Algorithm 8: Radix Sort

Radix sort is a non-comparative integer sorting algorithm. Since integers can also express strings (such as names or dates) and floating-point numbers in a specific format, radix sort is not limited to integers.

Before talking about radix sorting, we briefly introduce bucket sorting:

Algorithm idea: Divide the array into a limited number of buckets. Each bucket is then sorted individually (it is possible to use another sorting algorithm or to continue to use the bucket sort to sort in a recursive manner). 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. Simply put, it is to group the data into buckets one by one, and then sort the contents in each bucket.

For example, to sort n integers A[1..n] of size [1..1000].

First, the bucket can be set to a range of size 10. Specifically, set B[1] to store integers of [1..10], set B[2] to store integers of [10..20], ... The integers stored in the set B[i] [(i-1)*10, i*10], i=1,2,..100. There are 100 barrels in total.

Then, scan A[1..n] from the beginning to the end, and put each A[i] into the corresponding bucket B[j]. Then sort the numbers in each of the 100 buckets. At this time, you can use bubbling, selection, and even quick sorting. Generally speaking, any sorting method can be used.

Finally, the numbers in each bucket are output in turn, and the numbers in each bucket are output from small to large, so that a sequence in which all numbers are sorted is obtained.

Assuming there are n numbers and m buckets, if the numbers are evenly distributed, there will be an average of n/m numbers in each bucket. If quicksort is used for the numbers in each bucket, then the complexity of the whole algorithm is O(n+m*n/m*log(n/m))=O(n+nlogn–nlogm). It can be seen from the above formula that when m is close to n, the complexity of bucket sorting is close to O(n).

Of course, the calculation of the above complexity is based on the assumption that the input n numbers are evenly distributed. This assumption is very strong, and the effect in practical application is not so good. If all the numbers fall into the same bucket, it degenerates into a general ordering.

Most of the sorting algorithms mentioned above have a time complexity of O(n2), and some sorting algorithms have a time complexity of O(nlogn). The bucket sort can achieve O(n) time complexity. But the disadvantages of bucket sort are:

  1. The first is that the space complexity is relatively high, and the extra overhead required is large. Sorting has the space overhead of two arrays, one stores the array to be sorted, and the other is the so-called bucket. For example, if the value to be sorted is from 0 to m-1, then m buckets are required, and the bucket array requires at least m space.
  2. Secondly, the elements to be sorted must be within a certain range and so on.

Summarize

The stability, time complexity, space complexity and stability of various sorting are summarized as follows:

About time complexity:

  1. Square order (O(n2)) sorting: Various simple sorting: direct insertion, direct selection and bubble sort.
  2. Linear logarithmic (O(nlog2n)) sorting: quicksort, heapsort, and mergesort.
  3. O(n1+§)) sort: § is a constant between 0 and 1.
  4. Linear order (O(n)) sorting: radix sorting, in addition to bucket and bin sorting.

Regarding stability:

Stable sorting algorithms: bubble sort, insertion sort, merge sort, and radix sort;
not stable sorting algorithms: selection sort, quicksort, hill sort, heap sort.

refer to



Author: IT Program Lion
Link : https://www.jianshu.com/p/e6ad4423efcd
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

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