Sorting algorithm:
Quick-line: O (NLOGN) O (1) unstable
Merge: O (NLOGN) o (n) stabilized
Base:
Bubble
Sleep
Noodles
Pancakes
1, Quicksort:
- Return condition: Start >=end
- private = A[START]+A[END]/2
- while (left <= right)
- while (left <= right && A[left] < Privot)
- while (left <= right && a[right] > Privot)
Public classSolution {/** * @paramA an integer array *@returnvoid*/ Public voidSortIntegers2 (int[] a) {QuickSort (A,0, A.length-1); } Private voidQuickSort (int[] A,intStartintend) { if(Start >=end) { return; } intleft = start, right =end; //key point 1:pivot are the value, not the index intPivot = a[(start + end)/2]; //key point 2:every time Compare left & right, it should be//Left <= right not left < right while(Left <=Right ) { //key point 3:a[left] < pivot not A[left] <= pivot while(left <= right && A[left] <pivot) { Left++; } //key point 3:a[right] > pivot not a[right] >= pivot while(left <= right && a[right] >pivot) { Right--; } if(Left <=Right ) { inttemp =A[left]; A[left]=A[right]; A[right]=temp; Left++; Right--; }} quickSort (A, start, right); QuickSort (A, left, end); }}
View Code
2, MergeSort: The thought of divided treatment O (n): Merge two arrays
Main function: Exception check
Create a new array
MergeSort (int[] temp, int start, int end,int[] A)
Return condition: Start > End {return}
Left row;
Right row;
Merge ()
Merge (int[] temp, int start, int end, int[] A) {
Merge sorted subarrays in A to temp array
while (left <= mid && right <= end) {
If A[start] < A[end] {temp[index++] = a[start++]}
else:
There's one remaining, left, right remaining.
while (left <= mid): temp[index++] = a[left++]
Copy temp back to A
The For loop assigns a value back to a
}
Public classSolution {/** * @paramA an integer array *@returnvoid*/ Public voidSortIntegers2 (int[] A) {//Use a shared temp array, the extra memory was O (n) at least int[] temp =New int[A.length]; MergeSort (A,0, A.length-1, temp); } Private voidMergeSort (int[] A,intStartintEndint[] temp) { if(Start >=end) { return; } intleft = start, right =end; intMid = (start + end)/2; MergeSort (A, start, Mid, temp); MergeSort (A, Mid+1, end, temp); Merge (A, start, Mid, end, temp); } Private voidMergeint[] A,intStartintMidintEndint[] temp) { intleft =start; intright = Mid+1; intindex =start; //merge sorted subarrays in A to temp array while(Left <= mid && right <=end) { if(A[left] <A[right]) {Temp[index+ +] = a[left++]; } Else{Temp[index+ +] = a[right++]; } } while(Left <=mid) {Temp[index+ +] = a[left++]; } while(Right <=end) {Temp[index+ +] = a[right++]; } //Copy temp back to A for(index = start; index <= end; index++) {A[index]=Temp[index]; } }}
View Code
3. Heap Sequencing
Re-understanding Heap ordering:
Heap Sort steps:
1) Generate small Gan (Dagen)
The complete binary tree is generated in the order of the array row (R[0...n-1] subscript), and then from the last non-leaf node (N/2-1), it is treated as a root node and is gradually adjusted forward (the root node is the smallest) to create a small Gan.
2) Heap Adjustment:
1. Remove the vertex value from the small heel heap and swap it with the nth (n=n) node in the heap, i.e. r[n-1].
2. At this time the Keng Gen is destroyed and the formation will be given a new disordered zone (R1,R2,...... RN-1) and the new ordered area (Rn), and satisfies the r[1,2...n-1]>=r[n]; because the new heap top r[1] may violate the nature of the heap, and therefore requires a current unordered zone (R1,R2,...... RN-1) adjusts to the new heap, then swaps the r[1] with the last element of the unordered zone, resulting in a new unordered area (R1,R2 ...). Rn-2) and the new ordered area (RN-1,RN). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is complete.
Minimum heap post-order generation: Reverse order: from large to small
Code:
Generate Small Gan
/**
* Adjust to the minimum heap
* @param arr Array
* @param top Heap position
* @param tail heap not in place
*/
public static void Fittominheap (int[] arr, int top, int tail) {
int i = top;
int temp = Arr[i];
Int J = 2 * i + 1;//find child nodes
while (J < tail) {
if (j + 1 < tail && arr[j + 1] < ARR[J])//Find the smallest of the left and right child nodes
j + +;
if (Arr[j] >= temp)//child nodes are larger than the parent node jumps out
Break
Arr[i] = arr[j];//Move child nodes up, move to parent node
i = j;
j = 2 * i + 1;
}
Arr[i] = temp;
}
Heap Sort
public static void Heapsort (int[] arr) {
int n = arr.length;
for (int j = N/2-1; J >= 0; j--)//start with the last non-leaf node, initialize the smallest heap represented by the generated array
Fittominheap (arr, J, N);
for (int i = n-1; i > 0; i--) {//each time the vertex value of the heap is taken out
Swap the vertices in the heap with the a[i] point
int temp = arr[0];
Arr[0] = Arr[i];
Arr[i] = temp;
Fittominheap (arr, 0, I);//The least heap of the first I nodes after swapping
}
}
8 Sorting Algorithms---I am familiar with 3 (merge sort/Quick sort/heap sort)