A Survey of Basic Sorting Algorithms

A Survey of Basic Sorting Algorithms

References

  1. The course "Algorithms and Data Structures" by Mr. liuyubobobo of MOOC and the corresponding GitHub code repository
  2. The course "Visible Algorithms" by Mr. liuyubobobo of MOOC and the corresponding GitHub code repository

abstract

In this section, we only introduce two basic sorting algorithms. In the past, I only knew these two sorting algorithms when I was looking for a job and learning algorithms, just to cope with the interview. I was really too young and too simple to know. In the world of algorithms, there is a lot of knowledge waiting for us to learn and dig.

  • selection sort
  • Insertion sort, insertion sort optimization

selection sort

Introduction to selection sort

  • Selection sort is a very easy to understand and simple sorting algorithm. Through the learning of selection sorting algorithm, we open the door to our learning algorithm;
  • The time complexity of selection sort is O ( n 2 ) , although it is not the algorithm with the lowest time complexity, it has a characteristic that in general, or on average, selection sort exchanges elements the least number of times during the sorting process , which can be achieved by and other sorting The comparison of the algorithm is to understand, because the position of the element is only exchanged once in each round of the outer loop;
  • Below is a visual gif showing the execution of the "selection sort" algorithm, not written by me, from reference 2.

A brief description of the execution process of the selection sort algorithm

  • In each round of the outer loop, the smallest of the remaining elements is selected. The outer loop of selection sort always assumes that the first element is the smallest element at the beginning. Of course, this is definitely wrong, so choose the smallest element. The revision process of the index of the element is completed in the inner loop, and after the inner loop is completed, the position is exchanged with the first element in the outer loop.
  • Below is a visual gif showing the execution of the "insertion sort" algorithm, not written by me, from reference 2.

The following is a specific example showing the selection sorting algorithm: On the premise of understanding the selection sorting algorithm, you can try to write the selection sorting yourself, and verify whether the selection sorting method you wrote is correct by Debugging.

Features of selection sort:

Selection sort has two distinct features:

  1. The running time has nothing to do with the characteristics of the array itself: that is, for the three arrays listed below, for selection sorting, the same number of comparisons and the same number of times to exchange element positions are performed;
    (1) The sorted arrays ;
    (2) An array whose elements are all equal in value or an array whose elements are nearly equal;
    (3) A randomly generated array in the general sense;
  2. During the sorting process, the total number of element swaps is minimal.

Code implementation (Java)

https://gist.github.com/liweiwei1419/40a3ef067d3feb3510904b0b234419

Code implementation (Python)

https://gist.github.com/liweiwei1419/2ad3fa550924b66489bb501cf6fdff4a

Insertion sort

An overview of the basic execution flow of the insertion sort algorithm

  • Use two layers of loops to complete the sorting work, which is consistent with selection sorting;
  • In the general case, the time complexity of insertion sort is also O ( n 2 )
  • You can easily understand the execution flow of insertion sort through an animation;
  • The key steps of the insertion sorting algorithm are: the inner loop compares the elements one by one from the back to the front. If an element larger than itself is encountered, the position is exchanged until it is less than or equal to the current element, which looks like Each round inserts an element into an array that has already been sorted, I think this should be the reason why it is called insertion sort;
  • Part of the execution flow of the insertion sort algorithm is similar to how we play poker and sort the cards in our hands.

Code implementation (Java)

https://gist.github.com/liweiwei1419/9231657e0b8506f8e6d3f2702dc9e8ec

The following figure shows the analysis of the core process of the above algorithm:

The key point:

  • The 1st round has only 1 element, so the elements are sorted, so the loop should start from the 2nd element (the element with index 1) until the last element of the array (this is where the bounds value is set to < length) reason;
  • The inner loop takes the element at the beginning as the calibration point, and compares it with the previous element in turn. If it encounters an element larger than itself, it will be exchanged with it until it encounters an element equal to or smaller than itself, the inner loop terminated .
  • The following is an equivalent way of writing:

Note: In the for loop, when the branch of the if conditional judgment is break, the part of the conditional judgment can be placed in the loop body judgment statement of the for loop.

Below, we make a comparison between "selection sort" and "insertion sort".

Comparison of Selection Sort and Insertion Sort Algorithms

  • Selection sort selects a smallest element to be placed at the front of the array in each outer loop, and the inner loop of insertion sort will terminate as long as it encounters an element that is not larger than itself. This is the insertion sorting algorithm. A feature of : the inner loop can be terminated early . In contrast, selection sorting, every time you select the smallest element, you must honestly look at all the remaining elements.
  • Is insertion sort necessarily better than selection sort? Not necessarily. The analysis of our team’s algorithm must be based on the application scenario. Although insertion sort will reduce the number of comparisons in the inner loop, it is comparing elements. During the process, the position of elements is also constantly exchanged. If it is time-consuming to exchange the position of elements, then "insertion sort" should not be used.

Optimization of Insertion Sort Algorithm

  • Optimization idea: From the code for exchanging elements, we can see that, exchanging an element once, we do two things: (1) introduce a temporary variable; (2) assign three assignments. For this reason, we naturally think that we can introduce only one temporary variable in an inner loop, and then achieve the same effect by assigning backwards one by one.
  • In this way, compared with the original insertion sort, a series of operations of exchanging the positions of elements are omitted, but a temporary variable is used to store the element first, and then compare it with the previous elements in turn. If the previous element is large, the The previous element is moved backward, and then the temporarily stored element is compared with another previous element. If the next previous element is equal to this temporarily stored element, or is smaller than this temporarily stored element, then the previously stored element is The stored elements should be placed in this position. It is very difficult to say. Because of my limited expressive ability, it may be misunderstood. It is better to look at the code directly.
  • In short, we use a temporary variable to turn the previous process of "swapping variables multiple times" into a process of "multiple assignments" .

Below we give two identical optimization schemes for insertion sorting for your careful comparison:

Code 1:

Note: Don't forget to assign the temporary variable that was saved at the beginning to the appropriate place at the end.

Code 2:

Note: Don't forget to assign the temporary variable that was saved at the beginning to the appropriate place at the end.

The code of the above two pictures is actually the same. If you don't understand it well, you can review forthe execution process of the loop.

Notes on Writing an Optimized Version of the Insertion Sort Algorithm

Since I am a newcomer to the algorithm, I will encounter many problems in the process of writing the algorithm. In the process of writing the "multiple assignment" insertion sort optimization algorithm, I encountered the following problem.

Because there are many assignment operations, if the algorithm is written incorrectly, one result is likely to be: the resulting array is sorted correctly, but the critical points are not chosen correctly due to the boundary values, causing the elements of the array to be changed .

However, I have made this mistake, due to incorrect writing, the array that runs out is indeed sorted, but it is not an array at all with the original array. Therefore, after writing the algorithm, be sure to look at it yourself, or we can also write a test case and write two methods, one is the sorting algorithm implemented by ourselves, and the other is to use the built-in sorting algorithm provided by the language. , and then compare the elements one by one for equality.

Therefore, when we check whether our algorithm is correct, we should not only check whether the array obtained after the algorithm we wrote is ordered, but also check whether the elements of the array we get are consistent with the elements in the original array.

All in all: writing the "multiple assignment" insertion sorting optimization algorithm has the "risk" of changing the value of the array elements. After writing, be sure to check whether the algorithm you wrote yourself has changed the element value of the original array to be sorted. .

Summarize

There is still a lot of knowledge to be mined for small sorting algorithms. Due to the space and personal current ability and time, I will only introduce so much. There are also some simple sorting algorithms such as bubble sort and shell sort, etc. There will be a dedicated section to summarize them later.

Guess you like

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