A survey of basic sorting algorithms

Source: Internet
Author: User
Tags terminates

Basic Sorting Algorithm Overview reference
    1. Mu-net Liuyubobobo teacher "algorithm and data Structure" course and corresponding GitHub code warehouse
    2. Liuyubobobo teacher "visible algorithm" course and the corresponding GitHub code warehouse
Content Summary

In this section, we only introduce two basic sorting algorithms, before I go to find work, learning algorithms only know the two sorting algorithms, just to meet the interview, really is too young, too simple, familiar, in the algorithm of the world, there is a lot of knowledge waiting for us to learn, to dig.

    • Select sort
    • Insert sort, insert sort optimization
Select Sort Selection Sort Introduction
    • The choice of sorting is a very easy to understand and simple sorting algorithm, by choosing the sorting algorithm to learn, open our learning algorithm gate;
    • The time complexity of selecting a sort is $O (n^2) $, although it is not the least time-complicated algorithm, but it has a feature, that is, in general, or, on average, the choice of sorting in the process of sorting the least number of elements exchanged , This can be understood by comparison with other sorting algorithms, because each round of the outer loop only swaps the position of the element;
    • Here is a visual gif showing the execution of the Select Sort algorithm, which I did not write, from reference 2.

Select a simple description of the sorting algorithm execution process
    • The smallest of the remaining elements is selected in each round of the outer loop, and the outer loop of the selection sort begins with the assumption that the 1th element is the smallest element, which is certainly not true, so the revision process of the index that chooses the smallest element is done in the inner loop, and after the inner loop is completed, the 1th in the outer loop The position of the element exchange.

Here is a concrete example of choosing a sorting algorithm: in understanding the choice of sorting algorithm, you can try to write out the choice of sorting, through the Debug method to verify that you write the choice of the correct sorting methods.

Select the characteristics of the sorting:

The choice of sorting has two distinct features:

    1. The elapsed time and the characteristics of the group itself are irrelevant: the three arrays listed below, for the selection of sorting, the same number of comparisons and the same interchange element position;
      (1) An array that has been sequenced;
      (2) An array of elements whose values are all equal or the values of the elements are nearly equal;
      (3) A randomly generated array in the general sense;
    2. The total number of element exchanges is minimal during the sort execution.
Code implementation (Java)

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

Code implementation (Python)

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

An overview of the basic execution flow of the insert sort insertion sorting algorithm
    • The work of sorting is done using a two-layer loop, which is consistent with the selection sort;
    • In general, the time complexity of inserting a sort is also $O (n^2) $;
    • An animation can be used to simply understand the execution flow of the insertion sort;
    • The key steps of the insertion sorting algorithm are: The inner loop moves from the back forward, one by one, and the element is compared, and if you encounter an element larger than yourself, swap the position until it encounters a less than or equal to the current element, and it looks like each round is inserting an element into an array that is already sorted, I think this should be why it's called the insertion sort reason;
    • Part of the execution flow of the Insert sort algorithm is more like playing poker with the sort of poker you put on your hand.
    • Here is a visual gif showing the execution of the "insert Sort" algorithm, which I did not write, from reference 2.

Code implementation (Java)

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

The following diagram shows an analysis of the core process of the above algorithm:

Key points:

    • The 1th round has only 1 elements, so the element is ordered, so the 2nd element (the element with index 1) should be started, until the last element of the array (this is the boundary value set to < length);
    • The inner loop takes the first element as the calibration point, compares it to the previous one, and, if it encounters a larger element than itself, swaps it until it encounters an element equal to itself or smaller than itself, and the inner loop terminates .
    • The following is an equivalent notation:

Note: In the For Loop, if the branch that is judged by the IF condition is break, you can put the conditional judgment part into the loop body judgment statement for the For loop.

Below, we make a comparison between "select Sort" and "insert sort".

Comparison of select sort and insert Sort algorithm
    • Select sort in each round of the outer loop is to pick a minimum element in front of the array, and the inner loop of the insertion sort, as long as it encounters an element not more than its own, the inner loop terminates, which is a feature of the insertion sorting algorithm: The inner loop can be terminated prematurely , the inverse of the choice of sorting, Every time you choose the smallest element, you should honestly look at all the remaining elements.
    • That is not the insertion sort is better than the choice of sorting, also not necessarily, our team algorithm analysis must be based on the application scenario, the insertion sort although the number of comparisons in the inner loop will be reduced, but it in the process of comparing elements, but also in the course of the exchange of elements in the position, If we swap the position of the element is very time consuming, then we should not use "insert sort".
optimization of the insertion sorting algorithm
    • The idea of optimization: from the code of the Exchange element, we can see that the exchange of elements, we do two things: (1) introduce a temporary variable, (2) three times assignment operation. For this reason, it is natural to think that you can introduce only one temporary variable in an inner loop, and then achieve the same effect by assigning values backwards and forwards.
    • In this way, compared to the original insertion sort, a series of switching elements is reduced, but a temporary variable is used to save the element first, and then the previous element is compared, if the preceding element is large, the previous element is moved back, and then the temporarily stored element is compared with a previous element, If one of the preceding elements equals this temporarily stored element, or is smaller than the temporarily stored element, then the previously stored elements should be placed in this position, which is very awkward, because my ability to express is limited, it may be misleading, or directly look at the code more clearly.
    • In short, we have used a temporary variable that transforms the process of "multiple swap variables" to "multiple assignment" .

Below we give two identical to the optimization of the insertion sort, for you to carefully compare:

Code Listing 1:

Note: In the end, don't forget to assign a temporary variable that was saved to the right place.

Code Listing 2:

Note: In the end, don't forget to assign a temporary variable that was saved to the right place.

The above two graphs of the code is actually the same, if not very good understanding, you can review the for loop of the implementation process.

Considerations for writing an optimized version of the Insert Sort algorithm

Since I am the new contact algorithm, in the process of writing algorithms, will encounter a lot of problems. In the process of writing "multiple assignment" of the Insert Sort optimization algorithm, I encountered the following problem.

Because there are many assignments, if the algorithm is not written correctly, one possible result is that the resulting array is sorted correctly, but because of the boundary value, the selection of the critical point is not correct, causing the elements of the array to be changed .

However, I have made such a mistake, and because it is not written correctly, the array that was run is indeed ordered, but not an array at all with the original array. Therefore, after writing the algorithm, you must look at it yourself, or we can write a test case, write two methods, one is the implementation of the sorting algorithm, the other is the language provided by the built-in sorting algorithm are ordered, and then compare the elements are equal.

Therefore, while we are testing our own algorithms for correctness, we should not just detect whether the arrays we have written are ordered or not, but we should also check that the elements of the array we get are consistent with the elements in the original array.

In summary: write "multiple assignment" of the Insert sorting optimization algorithm has changed the value of the array element "risk", after the completion of writing, it must be tested, the algorithm you write, is not the original to sort the array of elements to change the value.

Summarize

A small sorting algorithm, there is a lot of knowledge can be mined, confined to space and personal current capacity and time, the first only to introduce so much. There are also some simple sorting algorithms such as bubble sort and shell sort wait for a special space to summarize them later.

A survey of basic sorting algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.