Summarize the commonly used sorting algorithm, convenient for later inspection.
Common sorting algorithms: Bubble sort, select sort, insert sort, shell (shell) sort, merge sort, quick sort, heap sort.
The factors to consider when choosing the right algorithm are: execution time, storage space, and programming effort.
1. Select sort
With two order of growth of equations, nearly suitable for small list sorting.
Scan through the list repeatedly, select an item for each scan, and then move the item to the correct location in the list.
Select Sort Total Comparison number = (n-1) + (n-2) + (n-3) +...+3+2+1
=n (n-1)/2
N (n-1)/2 is the level of O (n^2) order, so the choice of sorting is the Order O (n^2) algorithm.
// Outer loop control line (cycle compare number) for (int i = 0; i < arr.length-1; i++) { // Inner Loop control column, front and back two compare for ( int j = i+1; J < Arr.length; J ++ ) {if(arr[i]>arr[j] ) {int temp=arr[i]; Arr[i]=Arr[j]; ARR[J]=temp;}} }
2. Bubble algorithm
The next two elements are compared, and the position is swapped if the condition is met.
Total number of bubble sort comparisons = (n-1) + (n-2) + (n-3) +...+3+2+1
=n (n-1)/2
N (n-1)/2 is the level of O (n^2) order, so the bubble sort is the order O (n^2) algorithm.
// The last element does not need to be compared for (int i = 0; i < arr.length-1; i++) { /-I: Reduce the element for each comparison;-1 avoid subscript crossing, that is, when j=4, J+1 crosses the border. for (int j = 0; J < Arr.length-i-1; J + +) { if(Arr[j]>arr [J+1]) { int temp=arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; } }
}
3. Quick Sort
Basic idea:
(1) First take a number from the series as the base number (the first number of the general interval)
(2) To distinguish the process, the number is larger than the total on its right, less than or equal to its number on its left.
(3) Repeat the second step for each interval until there is only one number for each interval.
Here's an example of how we use a pit-filling to illustrate:
Again to a[0-4] and A[6-9] These two sub-zones repeat the above steps can be, first looking forward, and then looking backwards.
To summarize the number of digging pits:
1. I=l,j=r, the base number is dug up to form the first pit a[i];
2. j--from the back forward to find a smaller number than it, find and dig out this number, fill the former pit a[i];
3. i++ from the front to find a larger number than it, find and dig out the number of times to fill the previous pit a[j].
4. Repeat 2, 32 steps until I==j, and fill in the base number into a[i].
voidQuick_sort (intS[],intLintR) { if(l<R) { inti=l,j=r,x=S[l]; while(i<j) { while(i<j&&s[j]>=x) {j--;}//find the first number less than x from right to left if(I<J) {s[i++]=s[j];} while(i<j&&s[i]<x) {i++;}//find the first number greater than or equal to x from left to right if(I<J) {s[j--]=s[j];} } S[i]=x; Quick_sort (S,l,i-1);//RecursiveQuick_sort (s,i-1, R); } }
4. Insert Sort
With two order of growth, only for small list sorting.
If the list that you want to sort is almost sorted, the insertion sort is more efficient than bubbling sorting and selecting sort.
Inserting a sort performs a different number of comparisons, depending on the initial element order, and when the element is already in the sort order, the insertion order requires very little comparison.
You need to divide the list into two sub-lists, that is, sort and unordered.
Sorted list not sorted list
for (int i = 0; i < arr.length; i++) {// assume that the first element is placed in the correct position, so that only the 1~n-1 will be traversed int j=i; int target=arr[i]; while (J>0&&target<arr[j-1]) { arr[j]=arr[j-1]; J--; } ARR[J]=target; }
Best use case efficiency: O (n), when the list is already sorted, produces the best use case.
Worst case efficiency: O (n^2), which produces the worst case when the list is in reverse order.
5. Shell (shell) sorting
The insertion sorting algorithm is improved by forming multiple sub-lists separating elements from several locations and comparing them. When you apply an insert sort to each sub-list, the element moves toward its correct position, reducing the number of comparisons.
-------Select the distance between the elements in the separator group to form multiple sub-lists. (Here is 3)
-------apply an Insert sort to each sub-list so that the element moves toward its correct position.
6. Heap Sequencing
Heap sorting, like quick sort and merge sort, are some common sorts of time complexity O (NLOGN).
If the heap is treated as a complete binary tree, the meaning of the heap is: The full binary tree has non-leaf nodes (RI) are not greater than (after not less than) its left child's value (R (2) i), the right child's value (R (2i+1)).
The heap definition is: N keyword sequence k1,k2,k3,... kn is called a heap when and only if the sequence satisfies the following properties:
(1) ki<=k (2i) and ki<=k (2i+1) or
(2) Ki>=k (2i) and Ki>=k (2i+1) (1<=I<=[N/2])
That is, the child knot point >= Parents node or child node <= parents node
eg:{6,8,7,9,01,3,2,4,5} describes the process of sorting using the heap sorting method.
Output: 9,8,7,6,5,4,3,2,1,0
7. Merge sort
Several sorted ordered tables are merged into an ordered table several times. Merging two tables directly into a two-way merge.
EG:{18,2,20,34,12,32,6,16} describes the process by which the merge sort method is sorted.
Time Characteristic Description:
Time complexity O (NLOGN): Fast, heap, merge sort. Fastest and fastest, when n is larger, the merge is faster than the heap.
Time complexity is O (n^2): Insert, Bubble, select Sort. When inserting is most commonly used, especially in basic order, the minimum number of records to move is selected.
When sorting records are ordered: Insert and bubble can reach O (n), quickly degenerate to O (n^2).
The time characteristics of the selection, heap, and merge sorts are not changed with the distribution of the keywords.
Summary of the sorting and algorithm of illustrations