Sorting algorithm (2)--selection sort & heap sort

Continue the exchange --bubble sort & quick sort , this article introduces selection sort and heap sort
 
One, selection sort
Very simple and intuitive, each time you find the smallest or largest value and store it, continue to find the remaining values ​​and store them until the last element.
  1. Find the smallest value from arr[0]~arr[N] and put it in arr[0], at this time arr[0] has been sorted
  2. Find the smallest value from arr[1]~arr[N], put it in arr[1],
  3. ....Find the smallest value from arr[i]~arr[N], put it in arr[i],
  4. 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.

A heap is actually a complete binary tree. Heaps are divided into big top heap and small top heap, also called max heap and min heap.
Big top heap: The value of the parent node is larger than that of the left and right nodes, which is used for ascending order.
Small top heap: The value of the parent node is smaller than that of the left and right nodes, which is used for descending order.
 
The large top heap and the small top heap are symmetrical relationships, and it is enough to understand one of them. This article will describe the ascending sort implemented by the big top heap in detail.
Heapsort time complexity is O(N*lgN).
 
 
1. Basic idea
(1) Initialize the heap: Construct the sequence a[0...n] into a maximum 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. Properties of binary heaps implemented by arrays
The correspondence between arrays and heaps is as follows:


 
There are three correspondences between arrays and heaps:
(1), the index of the left child of index i is (2*i+1);
(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
 
For example, 30, the index is 1, the left child is 40, the index is 3, and the right child is 70, the index is 4.
 
3. Example demonstration
(1), initialize the heap

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.

 
 4. Code
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
	}
}
 
 
 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326226098&siteId=291194637