Top 10 Classic Sorting Algorithms (Animation Demonstration)

1. Bubble algorithm

Bubble algorithm is a simple 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.

1.1 Algorithm Description

  • Compare adjacent elements. If the first is larger than the second, swap the two;
  • Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end, so that the last element should be the largest number;
  • Repeat the above steps for all elements except the last one;
  • Repeat steps 1~3 until the sorting is complete.

2. Selection Sort

Selection sort is a simple and intuitive sorting algorithm. How it works: first find the smallest (largest) element in the unsorted sequence, store it at the beginning of the sorted sequence, then continue to find the smallest (largest) element from the remaining unsorted elements, and put it in the sorted sequence end of . And so on until all elements are sorted.

2.1 Algorithm Description

The direct selection sort of n records can obtain ordered results after n-1 times of direct selection sort. The specific algorithm is described as follows:

  • Initial state: the disordered area is R[1..n], and the ordered area is empty;
  • At the beginning of the i-th sorting (i=1,2,3...n-1), the current ordered area and unordered area are R[1..i-1] and R(i..n) respectively. This round of sorting selects the record R[k] with the smallest key from the current unordered area, and exchanges it with the first record R in the unordered area, so that R[1..i] and R[i+1 ..n) respectively become a new ordered area with an increase in the number of records and a new disordered area with a decrease in the number of records by 1;
  • At the end of n-1 times, the array is sorted.

3. Insertion Sort

The algorithm description of Insertion Sort is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence, for unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert.

3.1 Algorithm Description

Generally speaking, insertion sort is implemented on the array using in-place. The specific algorithm is described as follows:

  • Starting from the first element, the element can be considered sorted;
  • Take the next element and scan from back to front in the sorted element sequence;
  • If the element (sorted) is greater than the new element, move the element to the next position;
  • Repeat step 3 until you find the position where the sorted element is less than or equal to the new element;
  • After inserting the new element at that position;
  • Repeat steps 2~5.

4. Shell Sort

Invented by Shell in 1959, the first sorting algorithm to break through O(n2) is an improved version of Simple Insertion Sort. It differs from insertion sort in that it prioritizes elements that are farther apart. Hill sort is also called shrinking incremental sort.

4.1 Algorithm Description

First, the entire record sequence to be sorted is divided into several subsequences for direct insertion sorting. The specific algorithm is described as follows:

  • Choose an incremental sequence t1, t2, ..., tk, where ti>tj, tk=1;
  • Sort the sequence by k times according to the number of incremental sequences k;
  • 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.

Enter image description

Hill sort

5. 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. Merge the ordered subsequences to obtain a completely ordered sequence; that is, order each subsequence first, and then order the subsequence segments. If two sorted lists are merged into one sorted list, it becomes a 2-way merge.

5.1 Algorithm Description

  • Divide the input sequence of length n into two subsequences of length n/2;
  • Merge sort is used for these two subsequences respectively;
  • Merge the two sorted subsequences into a final sorted sequence.

6. Quick Sort

The basic idea of ​​quick sort: divide the delegated records into two independent parts by one sorting, and the keywords of one part of the records are smaller than the keywords of the other part, then the two parts of records can be sorted separately, which has reached the whole The sequence is ordered.

6.1 Algorithm Description

Quicksort uses a divide-and-conquer method to divide a string (list) into two sub-lists (sub-lists). The specific algorithm is described as follows:

  • Pick out an element from the sequence, called a "pivot";
  • 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;
  • Recursively sort the subarrays of elements smaller than the reference value and the subarrays of elements larger than the reference value.

7. Heap Sort

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 satisfies the property of stacking at the same time: that is, the key value or index of a child node is always less than (or greater than) its parent node.

7.1 Algorithm Description

  • Build the initial key sequence to be sorted (R1, R2....Rn) into a large top heap, which is the initial unordered area;
  • Swap the top element R[1] with the last element R[n], and get a new disordered area (R1, R2,...Rn-1) and a new ordered area (Rn), and satisfy R [1,2...n-1]<=R[n];
  • Since the new heap top R[1] after the swap may violate the properties of the heap, it is necessary to adjust the current disordered area (R1, R2, ... Rn-1) to a new heap, and then re-align R[1] with the disordered The last element of the region is exchanged to obtain a new disordered region (R1, R2....Rn-2) and a new ordered region (Rn-1, Rn). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is completed.

Graphical Sorting Algorithm (3) Heap Sort

8. Counting Sort

Counting sorting is not a comparison-based sorting algorithm, and its core is to convert the input data value into a key and store it in an additionally opened array space. As a sort of linear time complexity, counting sort requires that the input data must be an integer with a certain range.

8.1 Algorithm Description

  • Find the largest and smallest elements in the array to be sorted;
  • Count the number of occurrences of each element whose value is i in the array, and store it in the i-th item of array C;
  • Accumulate all counts (starting with the first element in C, adding each item to the previous item);
  • Reverse fill the target array: place each element i in the C(i)th item of the new array, and subtract 1 from C(i) for each element placed.

9. Bucket Sort

Bucket sort is an upgraded version of count sort. It uses the mapping relationship of functions, and the key to efficiency lies in the determination of this mapping function. The working principle of bucket sort (Bucket sort): Assuming that the input data is uniformly distributed, the data is divided into a limited number of buckets, and each bucket is sorted separately (it is possible to use another sorting algorithm or continue to use it recursively. bucket sort).

9.1 Algorithm Description

  • Set a quantitative array as an empty bucket;
  • Traverse the input data and put the data into the corresponding buckets one by one;
  • sort each bucket that is not empty;
  • Concatenate sorted data from buckets that are not empty.

10. Radix Sort

Cardinality sorting is to sort according to the low order first, then collect; then sort according to the high order, and then collect; and so on, until the highest order. Sometimes some attributes have a priority order, first sorted by low priority, and then sorted by high priority. The final order is that the higher priority is first, and the lower priority is higher with the same high priority.

10.1 Algorithm Description

  • Get the maximum number in the array and get the number of digits;
  • arr is the original array, and each bit is taken from the lowest bit to form a radix array;
  • Count and sort radix (using the feature that count sort is suitable for small range numbers);

Link

Top 10 Classic Sorting Algorithms (Animation Demonstration)

Guess you like

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