The sorting algorithm in data structure
Simple sorting is preferred when sorting sequences are basically ordered, and the average number of quick orders is less than heap ordering
1 Insert Sort
1) Direct Insert sort
For the first time, position 0 and position 1 are compared, and the small one is placed before.
The second time the number on position 2 is inserted into position 0 and position 1.
...
K-times the number on the position k, inserted into the sequence of the k-1 time has been completed.
5 2 6 0 3 9 1 7 4 8
A trip 2 5 6 0 3 9 1 7 4 8
Two trips 2 5 6 0 3 9 1 7 4 8
Three trips 0 2 5 6 3 9 1 7 4 8
Four trips 0 2 3 5 6 9 1 7 4 8
Five trips 0 2 3 5 6 9 1 7 4 8
Six trips 0 1 2 3 5 6 9 7 4 8
Seven trips 0 1 2 3 5 6 7 9 4 8
Eight trips 0 1 2 3 4 5 6 7 9 8
Nine trips 0 1 2 3 4 5 6 7 8 9
2) Hill Sort
The entire sequence of elements to be sorted into a number of sub-sequences (consisting of an element separated by an "increment") is directly inserted into the sort, and then reduced incrementally and then sorted, and then the entire sequence of elements in order (the increment is small enough), then the whole element of a direct insertion sort.
"The "
First-pass increment 5 in Geneva , 76
Second Increase 3 55 65 97 76
Third trip Increment 1 55 65 76 97
2 Select Sort
Select sort each trip selects the smallest (or largest) element from the data element to be sorted, placing the order at the end of the ordered sequence until all the data elements to be sorted are finished.
Each time you go through the remainder of the sorted section, find its maximum or minimum value. The number of key-code comparisons is not related to the initial arrangement of records
1) Select sort directly
The first time interval [0,n], select the minimum number of the interval and the number of positions 0 to exchange;
The second take interval [1,n], select the minimum number of the interval and the number of positions 1 to exchange;
The third time interval [2,n], select the minimum number of the interval and the number of positions 2 to exchange;
In turn, you can get the sort, which is the selection sort.
Ex
8 5 9) 3 7
o ( n 2 ) "> o ( n 2 ) "> traverse number 1th 3 5 9 8 7
o ( n 2 ) "> o ( n 2 ) "> Traverse number 2nd 3 5 9 8 7
o ( n 2 ) "> o ( n 2 ) "> traverse number 3rd 3 5 7 8 9
Traverse Number 4th 3 5 7) 8 9
2) Heap Sorting
3 Switching sort
1) Bubble sort
The first interval [0,n], by comparing the size of I and i+1, if the number of i+1 is less than the first, then interchange.
The second interval [0,n-1], by comparing the size of the first and the i+1, if the i+1 number is less than the first, then the interchange.
2) Quick Sort
1 Select a base number (pivot) from the sequence
2 iterates through all the numbers in a sequence, on the right side of the base number larger than the baseline number, on its left side
Repeat step 1.2 until there is only one element in all the subsets.
4 merge sort
Summary of sorting algorithms in data structure