Sort the data processing is a very common operation, modern high-level languages have ready-n kinds of sorting algorithms. But to understand their code, computer help thinking.
Simple selection sort
Each trip select the minimum (or maximum) of an element from data element to be sorted as the first element, drained until all the elements so far, simply sort is the sort of instability.
Regardless of the original array are arranged, are the same number of comparisons; is variable number of exchanges. And orderly without completely switched mobile element, in the worst case (the array to be positive in reverse sequence), a maximum number of exchanges: n-1.
Time complexity is n- 2
Bubble Sort
Previous Bowen: https://www.cnblogs.com/chentianwei/p/8244728.html
Compare two adjacent elements, if they put them in the wrong order switching over.
The number n, n-1 for comparative wheel. Round homing a maximum / minimum number, the number of already homing do not need to compare the next round.
Bubble analogy is this: each round the maximum / minimum values into the final array. Like fizzy.
Time complexity is n- 2
Direct insertion sort
The basic idea is to direct insertion sort each step will be a sort of records, has been inserted into the front row ordered sequence in order to better until complete insertion of all the elements so far.
Simple insertion sort in the best case, a relatively n-1 times, without exchanging elements, the time complexity is O (n); in the worst case time complexity is still O (n- 2 ). But in the case of randomly arranged elements of the array, insertion sort, sort, or better than the above two kinds.
The time complexity is still the n- 2