O (n ^ 2) sorting algorithms
I. Bubble Sorting
1. Principle
(1) Compare adjacent elements. If the first is bigger than the second, exchange the two of them;
(2) perform the same operation on each adjacent element, starting from the first pair to the last one. At this point, the final element should be the largest number;
(3) Repeat the preceding steps for all elements except the last one;
(4) continue to repeat the above steps for fewer and fewer elements until there is no need to compare any number.
2. A direct but inefficient implementation
Public void sort (int [] array) {// the outer loop traverses from the first element to the second last element, it is mainly used to control the number of times of the inner loop. // because every time the outer loop is run, the maximum number will reach the end of the for (int I = 0; I <array. length-1; I ++) {// The inner loop that traverses each number of arrays (the number of traversal entries is decreasing, because the last one does not need to be compared) // if the preceding value is greater than the preceding value, the two numbers are exchanged for (int j = 0; j <array. length-I-1; j ++) {if (array [j]> array [j + 1]) {int temp = array [j + 1]; array [j + 1] = array [j]; array [j] = temp ;}}}}
3. Optimized version (if there is no exchange during traversal, it indicates that the order has been sorted, and you will not need to arrange it later)
Public void sort (int [] array) {boolean swaped; int n = array. length; do {swaped = false; for (int I = 1; I <n; I ++) {if (array [I-1]> array [I]) {// if there are two elements in this trip that have been replaced by positions, then whether to set the sorting order to true // if there is a trip without any two elements that have been replaced by positions, it indicates that they have been arranged, the entire sorting process can stop int temp = array [I-1]; array [I-1] = array [I]; array [I] = temp; swaped = true;} n --;} while (swaped );}Ii. Select sorting
1. Principle:
The selection of sorting is an improvement on the Bubble sorting, because after each comparison of the Bubble sorting, two element positions must be exchanged, and the swapping process will assign values several times, which is especially time-consuming.
How to Choose sorting:
Is to select the smallest element from the data elements to be sorted each time, and store it at the starting position of the sequence until all the data elements to be sorted are arranged
2. A simple implementation:
Public void sort (int [] array) {for (int I = 0; I <array. length; I ++) {// locate the index location of the smallest element int minIndex = I; for (int j = I + 1; j <array. length; j ++) {if (array [j] <array [minIndex]) {minIndex = j ;}} int temp = array [I]; array [I] = array [minIndex]; array [minIndex] = temp ;}}
3. Summary:
Select sorting because the smallest element must be selected for each traversal, so the entire array should be traversed each time, which is also a very low-efficiency sorting.
Iii. Insert sorting
1. Principle:
The principle of inserting sorting is similar to playing cards, that is, each step inserts a record to be sorted into the appropriate position of the previously sorted sequence, until all data is inserted.
2. Implement code version 1:
Public void sort (int [] array) {for (int I = 1; I <array. length; I ++) {// start from the position of the outer loop from the front, if the number of J-1 positions is larger than the number of j positions, then the for (int j = I; j> 0; j --) {if (array [j] <array [j-1]) {int temp = array [j-1]; array [j-1] = array [j]; array [j] = temp;} // This else is the key to efficient insertion sorting, because if the elements in front of position I are sorted in order, if position I is smaller than the I-1 position, it indicates that the order of else {break ;}}}}
As you can see, insertion sorting refers to inserting the current I element into a proper position. The break process exists throughout the traversal process.
Unlike sorting, it must traverse the entire array to find the minimum number.
3. But the above program is a little cumbersome. modify it.
Public void sort (int [] array) {for (int I = 1; I <array. length; I ++) {// place the break condition in the for loop. It looks refreshing for (int j = I; j> 0 & array [j] <array [j-1]; j --) {int temp = array [j-1]; array [j-1] = array [j]; array [j] = temp ;}}}
4. The above program still has a problem. We can see that three values need to be exchanged for each exchange. You can modify it further.
Public void sort (int [] array) {for (int I = 1; I <array. length; I ++) {// improved int e = array [I]; int j; for (j = I; j> 0 & array [j-1]> e; j --) {array [j] = array [j-1];} array [j] = e ;}}
A little explanation:
In the loop of the inner layer, save the number at the I position and traverse it in sequence. If the number at the J-1 position is larger than that at the j position, then directly overwrite the number of J-1 positions to the j position
Until the appropriate position of position I is found, that is, array [J-1] <e.
Finally, the number just stored is assigned to the location found.
This saves a lot of unnecessary assignment processes and improves efficiency.
5. Summary:
For insertion sorting, I would like to say that although the time complexity of insertion sorting is O (n ^ 2), it is very efficient in some cases. For example, in a basically ordered sequence, there are only a few numbers, and its order is incorrect. At this time,Insert sorting is very efficient
Iv. Hill sorting
1. Hill sorting is a powerful simplified version of insert sorting. The idea is:
First, the entire sequence of elements to be arranged is divided into several sub-sequences (composed of elements separated by an increment) for direct insertion and sorting, and then the incremental sequence is reduced and then sorted, when the elements in the entire sequence are basically ordered (the increment is small enough), directly insert and sort all the elements.
Because the efficiency of direct insert sorting is very high when the elements are basically ordered (close to the best condition), the time efficiency of hill sorting is much higher than that of the first two methods.
2. Implementation:
Public void sort (int [] arr) {int n = arr. length; int h = 1; while (h <n/3) {h = 3 * h + 1;} // calculate increment sequence: 1, 4, 13, 40,121,364,109 3... while (h> = 1) {// h-sort the array for (int I = h; I <n; I ++) {// for arr [I], arr [I-h], arr [I-2 * h], arr [I-3 * h]... use Insert to sort int e = arr [I]; int j; for (j = I; j> = h & e <arr [j-h]; j-= h) {arr [j] = arr [j-h];} arr [j] = e;} h/= 3 ;}}