[Super detailed] Comparison of the eight sorting algorithms and their respective characteristics

 

Don't miss this article, or you will lose out.

Eight sorts, each showing its magical powers.

  • Preface
  • 1. The concept of sorting
  • 2. Features of each algorithm
    • 1. Directly select sort
    • 2. Heap sort
    • 3 . Direct insertion sort
    • 4. Hill sort
    • 5 . Bubble Sort
    • 6. Quick sort
    • 7 . Merge sort
    • 8 . Radix sort 
  • to sum up

 


Preface

Sorting is a very important operation in computer programming. It rearranges an arbitrary sequence of data elements (or records) into a sequence ordered by keywords. The efficiency of finding elements in an ordered sequence is very high, (for example, the average search length of the binary search method is log2(n+1)−1log2(n+1)−1), but the unordered sequence can only be searched one by one. The average search length is (n+1)/2(n+1)/2. Another example is the process of constructing a binary sort tree, which is a sorting process. Therefore, how to sort, especially efficient sorting, is an important topic.


Tip: Let’s take a look at each sort

1. The concept of sorting

1.1 The concept of sorting

  1. Sorting: The so-called sorting is the operation of arranging a series of records in increasing or decreasing order according to the size of one or some keywords.
  2. Stability: Assume that there are multiple records with the same key in the sequence of records to be sorted. If sorted, the relative order of these records remains unchanged, that is, in the original sequence, r[i]=r[j] , And r[i] is before r[j], and in the sorted sequence, r[i] is still before r[j], this sorting algorithm is said to be stable; otherwise it is called unstable.
  3. Internal sorting: sorting in which all data elements are placed in memory.
  4. External sorting: Too many data elements cannot be placed in the memory at the same time. According to the requirements of the sorting process, the sorting of data cannot be moved between internal and external memory.

1.2 Common sorting algorithms

1.3 Stability and complexity

Second, the characteristics of each algorithm (see the blog implementation below for details)

1. Selection sort-direct selection sort

Each time the smallest (or largest) element is selected from the data elements to be sorted and stored at the beginning of the sequence until all the data elements to be sorted are arranged.

Find the smallest key in the sequence, and then swap this element with the element at the beginning of the sequence . For example, the first i elements of the sequence are already in order, and the element with the smallest keyword is selected from the i+1 to the nth element. If the jth element is the smallest element, then the jth element and the i+1th element are exchanged The position of the element. Perform this operation in sequence until the n-1th element is also determined.

 

Summary of the characteristics of direct selection sorting:

  1. The direct selection and sorting thinking is very easy to understand, but the efficiency is not very good . Rarely used in practice
  2. Time complexity: O(N^2)
  3. Space complexity: O(1)
  4. Stability: unstable

2. Selection sort-heap sort

Heap sort (Heapsort) refers to a sorting algorithm designed by using the stacked tree (heap) data structure, which is a kind of selective sort. It selects data through the heap. It should be noted that a large pile must be built in ascending order, and a small pile must be built in descending order.

Heap insertion :

Each insertion is to put new data at the end of the array . It can be found that the sequence from the parent node to the root node of this new data must be an ordered sequence, and then this new data is inserted into this ordered data, and comparisons are made in this process.

  1. Arrange a large pile, that is, the smallest is placed on the top, that is, in ascending order
  2. Sort small piles, that is, the largest is placed on the top, that is, in descending order (see the blog below for details)

Summary of the characteristics of heap sort:

  1. Heap sorting uses the heap to select the number, and the efficiency is much higher.
  2. Time complexity: O(N*logN)
  3. Space complexity: O(1)
  4. Stability: unstable
  5. Application: Find the top K smallest numbers among M numbers and keep them in order

3. Insertion sorting---direct insertion sorting 

When inserting the i-th (i>=1) element, the previous array[0],array[1],…,array[i-1] have been sorted, then use the sort code of array[i] and array [i-1],array[i-2],... The order of the sort codes is compared, and the insertion position is found, that is, array[i] is inserted, and the order of the elements at the original position is shifted backward

Summary of the characteristics of direct insertion sort:

  1. The closer the set of elements is to order , the more time efficient the direct insertion sorting algorithm is
  2.  Time complexity: O(N^2)
  3.  Space complexity: O(1), it is a stable sorting algorithm
  4.  Stability: stable

  4.  Insertion sort --- Hill sort

Hill sorting method is also called reduced increment method. The basic idea of ​​Hill sorting method is: first select an integer, divide all the records in the file to be sorted into groups, group all records with a distance of 1 into the same group, and sort the records in each group. Then, take and repeat the above grouping and sorting work. When it reaches =1, all records are sorted in the unified group.

Applicable scene

Comparison is the most important operation in Hill sorting, not exchange. Hill sorting with the best known step sequence is faster than direct insertion sorting, and even faster than quick sorting and heap sorting in small arrays, but Hill sorting is not as good as fast sorting when a large amount of data is involved;

Suitable for small arrays of small data.

Summary of the features of Hill sorting:

  1.  Hill sorting is an optimization of direct insertion sorting.
  2.  When gap> 1, they are all pre-sorted, the purpose is to make the array closer to order. When gap == 1, the array is already close to order, so it will be very fast. In this way, as a whole, an optimized effect can be achieved. After we realize it, we can compare the performance test.
  3.  The time complexity of Hill sorting is not easy to calculate, it needs to be deduced, and the average time complexity is derived: O(N^1.3—N^2)
  4.  Stability: unstable

5. Exchange sorting---bubble sorting

Compare the size of the key value of the two records. If the size of the two record key values ​​is in the reverse order, exchange the two records, so that the record with the smaller key value is moved to the front of the sequence, and the record with the larger key value is moved to the sequence Move back

 

Summary of the characteristics of bubble sort:

  1.  Bubble sort is a very easy to understand sort
  2.  Time complexity: O(N^2)
  3.  Space complexity: O(1)
  4.  Stability: stable
  5. Disadvantages: slow, only two adjacent data can be moved at a time;

6. Exchange sort---Quick sort

Quick sort is a binary tree structure exchange sort method proposed by Hoare in 1962. Its basic idea is: take any element in the sequence of elements to be sorted as a reference value, and divide the set to be sorted into two sub-sequences according to the sort code. , All elements in the left subsequence are less than the reference value, all elements in the right subsequence are greater than the reference value, and then the process is repeated for the leftmost subsequence until all the elements are arranged in the corresponding positions.

The common ways to divide the interval into the left and right halves according to the reference value are as follows:

  1. hoare version
  2. Digging method
  3. Front and rear pointer version

Summary of the characteristics of quick sort:

  1.  The overall performance and usage scenarios of quick sort are relatively good, so I dare to call quick sort
  2.  Time complexity: O(N*logN)
  3.  Space complexity: O(logN)
  4.  Stability: unstable
  5. Applicable scenarios: When sorting large amounts of data, the efficiency of fast sorting is particularly obvious
  6. Efficiency: The efficiency of this sorting algorithm is higher when the sequence is chaotic . When the data is in order, it will degenerate into a bubble sort;

 

7.  Merge sort  

Basic idea:

Merge sorting (MERGE-SORT) is an effective sorting algorithm based on merge operations. This algorithm is a very typical application of Divide and Conquer. Combine the existing ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence in order, and then make the subsequences in order. If two ordered lists are merged into one ordered list, it is called two-way merge. 

Summary of the characteristics of merge sort:

  1.  The disadvantage of merging is that it requires O(N) space complexity. The thinking of merging sort is more to solve the problem of outer sorting in the disk.
  2.  Time complexity: O(N*logN)
  3.  Space complexity: O(N)
  4.  Stability: stable 
  5. Applicable scenarios: If n is large and the sorting is required to be stable, you can choose merge sorting;

 

8. Cardinality sort 

The idea of ​​radix sorting is to sort according to the digits that make up the key, which is a kind of distribution sorting. If the keyword is a decimal number, then let r=10, and d is the maximum number of digits in all keywords (numbers with digits less than d are prefixed with 0). The radix sort can start from the least significant bit or the most significant bit.
  The idea of ​​radix sorting is: set up r queues, the numbers are 0,1,2,3...,r-1. First, place n keywords in r queues according to the value of the least significant bit, then collect the elements from small to large, and then place the elements in each queue according to the next lowest value, and then collect, repeat the above process, Until the collection is complete.

 


to sum up

We will use different sorting algorithms in different scenarios. (The blog will talk about it in detail later)

Thank you for reading. If there are any errors, please correct me. I hope everyone will pay attention to this wave. (Thank you)

Guess you like

Origin blog.csdn.net/qq_45615577/article/details/115257685