Explain common sorting algorithms and their python implementation - Section 1

  1. Bubble sort

  It should be said that bubble sort is the simplest sorting method in all sorting. Bubbling, as the name implies, is to compare two adjacent elements in turn, and if they are in the wrong order, swap them. Through this method, the larger (or smaller) elements will be continuously exchanged and slowly float. To the top of the sequence, until no elements need to be exchanged between the two, the sorting is completed. It needs to be done iteratively because each exchange may change the relationship between the previously searched elements. The specific operation steps are:

  1. Compare adjacent elements, if the former is larger than the latter, swap the two positions; check each pair of adjacent elements, and the last element becomes the largest element;

  2. Repeat the above steps again for all adjacent elements in a new round, except for the last maximum value, for a second round of comparison.

  3. Continue to compare fewer and fewer elements in each subsequent round, and repeat the above steps until no adjacent elements need to be exchanged, and the bubbling ends. Take a string of numbers as an example:

       It can be seen that if 15 numbers are sorted, then a maximum of 15 rounds of adjacent comparisons are required to complete the sorting.

  code above

  

  In fact, this example ends the sorting after the third round, so the bubble sorting can be optimized. The optimization method is to add a label to judge whether the adjacent elements no longer need to be exchanged. The code is as follows.

After optimization, the sorting takes only three rounds to complete.

 

 2. Selection sort

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

  1. Each pass selects the smallest (or largest) element from the data elements to be sorted, 

  2. The order is placed at the end of the sorted sequence until all the data elements to be sorted are finished.
  3. Selection sort is an unstable sorting method.

 

3. Insertion sort

  The principle of insertion sorting is similar to that of playing cards, that is, "drawing cards" from unsorted data is inserted into the corresponding position of the sorted sequence. This method is suitable for sorting a small amount of data and is a relatively stable sorting method.

  There is an already-ordered data sequence, and it is required to insert a number into this already-ordered data sequence, and it is required that the data sequence is still in order after the insertion. Only one element is clearly ordered at first, then one element is inserted in place, then a third element is inserted, and so on. step:

  1. Starting from the first element, treat the element as sorted;

  2. Take the next element and scan from back to front in the sorted element sequence;

  3. If the sorted element is larger than the new element, move the sorted element to the next position;

  4. Repeat 3 until the sorted element is found to be less than or equal to the position of the new element, and the new element is inserted into the position;

  5. Repeat 2-4

  See the next section for the code

  

 

 

 

 

 

 

 

 

Guess you like

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