This paper describes the scheduling algorithm with time complexity of N*logn: merge sort, quick sort, heap sort, and hill sort principle, Java implementation and transform application.
First, merge sort
- Principle: Combine two ordered series into an ordered series. Recursive implementation is required.
- Java implementations:
1 Public int[] MergeSort (int[] A,intN)2 {3 returnDomergesort (A, n, 0, n-1);4 }5 Public int[] Domergesort (int[] A,intNintStartintend)6 {7 int[] res =New int[n];8 if(N <= 1)9 {TenRes[0] =A[start]; One returnRes; A } - //when n>=2, the left and right arrays are processed - intHalf = N/2; the intLeftend = start + half-1; - intRightStart = leftend + 1; - //Call this function recursively to get an ordered array of left and right arrays - int[] left =Domergesort (A, half, start, leftend); + int[] right = Domergesort (A, N-half, RightStart, end); - //merging left and right sequences + intK = 0, i = 0, j = 0; A while(I < half && J < n-half) at{//compare two sequences from the go, take a smaller value to the res, and after that the sequence moves backward to remove a value comparison - if(Left[i] <=Right[j]) - { -res[k++] = left[i++]; - } - Else in { -res[k++] = right[j++]; to } + } - //the rest is put directly into the while(I <half) * { $res[k++] = left[i++];Panax Notoginseng } - while(J < N-half) the { +res[k++] = right[j++]; A } the returnRes; +}second, quick sort
- Principle: Each time a number on the left of the number is all smaller than it, and the number of the right is all larger than its position, and then recursively call the function, the left and right sequence row. Here is a better understanding of the practice: in the left side of the array to maintain a less than interval, in the traversal, when the number is found smaller than the current time, will be, the amplification is less than the interval, and it is less than the interval, after the end of the current number is filled in less than the interval.
- Java implementations:
1 Public int[] QuickSort (int[] A,intN)2 {3Doquicksort (A, n, 0, n-1);4 returnA;5 }6 Public voidDoquicksort (int[] A,intNintStartintend)7 {8 if(N > 1)9 {Ten intCurrent =A[end]; One intMinlen = 0;//less than the length of the interval A inti =start; - for(; i < end; i++) - { the if(A[i] <Current ) -{//finds a number smaller than the current number, expands less than the interval - inttemp = A[start +Minlen]; -A[start + Minlen] =A[i]; +A[i] =temp; -minlen++; + } A } atA[end] = A[start +Minlen]; -A[start + Minlen] =Current ; - //the current position has been determined, the left and right sequence -Doquicksort (A, Minlen, start, start + minLen-1); -Doquicksort (A, n-minlen-1, start + Minlen + 1, end); - } in}
there is an array of integers consisting only of three elements of 0,1,2, using swap, sort in place instead of counting. Given an integer array A and its size that contains only 0,1,2, return the sorted array. Ensure that the array size is less than or equal to 500. Test Examples:
[0,1,1,0,2,2],6
return: [0,0,1,1,2,2]
Analysis: The principle of using the fast line. With the number one processing, in the array left and right maintain a less than interval and greater than the interval. |
third, heap sorting
- Principle: Maintain a large Gan (Keng Gen), that is, maintenance of a binary tree, the number of child nodes is always smaller than the parent node. Each time the root is removed in the large root heap, the root is the maximum number of rows to be placed at the end of the sequence, and then adjust the large root heap, repeat the appeal process.
- Java implementation: Bo Master is not very able to illustrations, about the size of the Gan adjustment details can be Baidu itself. The principle summary is to start with the last non-leaf node and adjust forward. Set the currently adjusted non-leaf node to N, elect N,n to the left, N of the right three nodes of the maximum value as the parent node. And each time the value of the front non-leaf node is adjusted, the following number of large heap rules may be broken and need to be adjusted again. Well, I don't think I am clear, Baidu look at the picture is good.
Public int[] Heapsort (int[] A,intN) { for(intLen = n; Len > 0; len--) {//Len is the size of the heap built for(inti = LEN/2-1; I >= 0; i--) {//To traverse a non-leaf node from backward forward while(2 * i + 1 <Len) { intj = 2 * i + 1;//left node ordinal if(j + 1 < len && A[j] < A[j + 1]) {J++; } if(A[i] <A[j]) { inttemp =A[j]; A[J]=A[i]; A[i]=temp; I=J; } Else { Break; } } } inttemp = a[len-1]; A[len-1] = A[0]; a[0] =temp; } returnA; }
- Deformation application: General applications such as finding the maximum number of 10 in the 1000+ number.
|
An almost ordered array is known, and almost orderly means that if the array is sorted in order, the distance of each element can be no more than K, and K is smaller relative to the array. Please select an appropriate sorting algorithm to sort this data. Given an int array A, given the size of a, n, and K in test instructions, return the sorted array.
Test sample: [2,1,4,3,6,5,8,7,10,9],10,2
return: [1,2,3,4,5,6,7,8,9,10]
Parsing: Maintenance of a small size of K Gan, each time after the small Gan, the minimum will appear in the first place, at this time remove the first bit, add after a number in, continue to adjust the small Gan can. You can imagine a sliding window that moves backwards from the past, a small Gan in the sliding window. |
Iv. sort of Hill
- Principle: Insert sort after deformation, each number is only compared with the position of the multiple of the fixed step in front of it. Such as: Step step, the current number with the front of it step,step*2,step*3 .... position to be compared and inserted into the appropriate position.
- Java implementations:
Public int[] Shellsort (int[] A,intN) {//Step Selection for(intk = 1, step = 10; Step > 1; k++) {Step= N/(2 *k); for(inti = step; I < n; i++) { if(A[i] < a[i-Step]) { inttemp =A[i]; A[i]= A[i-Step]; A[i-Step] =temp; intPre = i-step; while(Pre-step > 0) { if(A[pre] < a[pre-Step]) {Temp=A[pre]; A[pre]= A[pre-Step]; A[pre-Step] =temp; Pre-=step; } Else { Break; } } } } } returnA; }
Java implementation of eight sorting algorithms (Time Complexity O (N*LOGN))