- Find the smallest value from arr[0]~arr[N] and put it in arr[0], at this time arr[0] has been sorted
- Find the smallest value from arr[1]~arr[N], put it in arr[1],
- ....Find the smallest value from arr[i]~arr[N], put it in arr[i],
- If i==N is found, the sorting is completed
public void sort(int[] arr) { for(int i=0;i<arr.length;i++){ int min = arr [i]; int index = i; for(int j=i+1;j<arr.length;j++){ if(min > arr[j]){ min = arr[j]; index = j; } } int temp = arr[i]; arr[i] = arr[index]; arr[index] = temp; } }
Second, heap sort
Heap Sort refers to a sorting algorithm designed using the data structure of the heap.
(2) Swap data: Swap a[0] and a[n] so that a[n] is the maximum value in a[0...n]; then replace a[0...n-1] Resize to max heap. Next, swap a[1] and a[n-1] so that a[n-1] is the maximum value in a[1...n-1]; then replace a[1...n-2 ] to rescale to the maximum value. And so on, until the entire sequence is sorted.
(2), the index of the left child of index i is (2*i+2);
(3), the index of i The index of the parent node is floor((i-1)/2); that is, rounding
In the heap sort algorithm, the array to be sorted is first converted into a binary heap.
The following demonstrates the steps to convert the array {20, 30, 90, 40, 70, 110, 60, 10, 100, 50, 80} to a max heap {110, 100, 90, 40, 80, 20, 60, 10, 30, 50, 70} .
1.1 i=array length/2-1=11/2-1, i.e. i=4
The above is the maxheap_down(a, 4, 10) adjustment process. The role of maxheap_down(a, 4, 10) is to downgrade a[4...10]; the left child of a[4] is a[9], and the right child is a[10]. When adjusting, choose the larger of the left and right children (i.e. a[10]) and swap with a[4].
1.2 i=3
The above is the maxheap_down(a, 3, 10) adjustment process. The role of maxheap_down(a, 3, 10) is to downgrade a[3...10]; the left child of a[3] is a[7], and the right child is a[8]. When adjusting, choose the larger of the left and right children (i.e. a[8]) and swap with a[4].
1.3 i=2
The above is the maxheap_down(a, 2, 10) adjustment process. The role of maxheap_down(a, 2, 10) is to downgrade a[2...10]; the left child of a[2] is a[5], and the right child is a[6]. When adjusting, choose the larger of the left and right children (i.e. a[5]) and swap with a[2].
1.4 i=1
The above is the maxheap_down(a, 1, 10) adjustment process. The role of maxheap_down(a, 1, 10) is to downgrade a[1...10]; the left child of a[1] is a[3], and the right child is a[4]. When adjusting, choose the larger of the left and right children (i.e. a[3]) and swap with a[1].
After the swap, a[3] is 30, which is larger than its right child a[8], and then they are swapped.
1.5 i=0
The above is the maxheap_down(a, 0, 10) adjustment process. The function of maxheap_down(a, 0, 10) is to downgrade a[0...10]; the left child of a[0] is a[1], and the right child is a[2]. When adjusting, choose the larger of the left and right children (i.e. a[2]) and swap with a[0].
After the swap, a[2] is 20, which is larger than its left and right children, choose the larger child (ie, the left child) and swap with a[2].
After the adjustment is completed, the maximum heap is obtained. At this point, the array {20,30,90,40,70,110,60,10,100,50,80} also becomes {110,100,90,40,80,20,60,10,30,50,70}.
(2), exchange data
After converting the array to a max heap, the data is exchanged, making the array a true ordered array.
The exchange data part is relatively simple, and the following only shows the schematic diagram of placing the maximum value at the end of the array.
The above is a schematic diagram of exchanging data when n=10.
When n=10, first swap a[0] and a[10] so that a[10] is the maximum value between a[0...10]; then, adjust a[0...9] so that It's called max heap. After swapping: a[10] is ordered.
When n=9, first swap a[0] and a[9] so that a[9] is the maximum value between a[0...9]; then, adjust a[0...8] so that It's called max heap. After swapping: a[9...10] is ordered.
...
and so on until a[0...10] is ordered.
private void maxHeapDown(int arr[],int start,int end){ int parentIndex = start;//1.1 node int left = 2*parentIndex+1;//1.2 Find the left node first while(left<=end){ int maxIndex =left;//2. Nodes with large data in the left and right nodes if((left+1) <= end && arr[left]<arr[left+1]){ maxIndex = left+1; } int parentValue = arr[parentIndex]; if(parentValue > arr[maxIndex]){ break; } //3, exchange node arr[parentIndex] = arr[maxIndex]; arr[maxIndex] = parentValue; //4. Continue processing parentIndex = maxIndex; left = 2*parentIndex+1; } } public void heapSort(int arr[]){ // Iterate from (n/2-1) --> 0 one by one. After traversing, the resulting array is actually a (maximum) binary heap. for(int i=arr.length/2-1; i>=0; i--){ maxHeapDown(arr, i, arr.length-1); } for(int i=arr.length-1;i>0;i--){ int temp = arr[i]; arr[i] = arr[0]; arr[0] = temp; //arr[0] and arr[i] are exchanged. After the exchange, arr[i] is the largest of arr[0]~arr[i-1] maxHeapDown(arr, 0, i-1);//Adjust the remaining stack } }