Selection Class Sort - Simple Sort and Heap Sort

refer to:

https://download.csdn.net/download/qq_31567335/10356263

What is a heap?

The heap is a complete binary tree: leaf nodes can only appear in the lowest and next lower layers, and the nodes of the lowest layer are concentrated in the leftmost position of the binary tree. Moreover, the heap must also meet the following properties: the parent node is greater than or equal to the left and right child nodes (large heap), or the parent node is less than or equal to the left and right child nodes (small heap). Later, for the convenience of expression, only the large heap is discussed in the article.

It can be seen that the large heap has two important properties:

1. The element at the top of the heap must be the element with the largest value.

2. The left and right subtrees of the heap are still a heap.

 

What kind of data structure is used to store the heap?

The representation is surprisingly simple. From this picture:

Observation can be found: the subscript of the parent node of the i node is (i – 1) / 2. Its left and right child nodes have subscripts 2 * i + 1 and 2 * i + 2, respectively.

So, just store the heap in the array in top-to-bottom, left-to-right order. Use the formula summarized above to find the parent node or child node of a node.

 

How to construct a heap from primitive arrays?

Each node is judged in order from right to left and bottom to top. Make sure this node is larger than its left and right children. If it is not satisfied, find the largest of the left and right child nodes and exchange them. Of course, after the swap is completed, the child nodes may no longer satisfy the properties of the heap, so recursion is required until the leaf node is reached. Since the parent node coordinate of the last element is (n - 1) / 2, anything larger than this node is a leaf node. So you only need to judge from (n - 1) / 2.

Time complexity: n*lgn. (It seems that the limit can be calculated to be n, and nlgn can be considered a rough but correct result)

 

How to use the heap to sort?

After constructing the heap, swap the first element (the largest element) with the last element of the array, reducing the size of the heap by 1.

The first element is maintained once, and the time complexity is lgn.

Repeat this step until every element is sorted.

The total time complexity is nlgn.

 

 Code:

package dataStructureAndAlgorithms;

public class SelectSort {
	
	//Simple selection sort
	public static void simpleSelectSort(int[] array){
		int length = array.length;
		// subscript of the minimum value
		int minIndex = 0;
		//Select the element at the i-th position. Position 0 is the smallest, 1 is the second smallest... The element at the last position does not need to be selected
		for(int i=0;i<length-1;i++){
			minIndex = i;
			for(int j=i+1;j<length;j++){
				if(array[j]<array[minIndex]){
					minIndex = j;
				}
			}
			if(minIndex != i){
				int temp = array[minIndex];
				array[minIndex] = array[i];
				array[i] = temp;
			}
			System.out.println("After the "+ (i+1) + " round sorting: ");
			display(array);
		}
	}
	
	// heap sort
	public static void heapSort(int[] array){
		int maxIndex = array.length - 1;
		//construct heap
		for(int index = (maxIndex-1)/2;index>=0;index-- ){
			maxHeap(array, index, maxIndex);
		}
		
		System.out.println("Constructed heap: ");
		display(array);
		
		
		int count = 0;
		//sort
		for(int i=maxIndex;i>=1;i--){
			int temp = array[i];
			array[i] = array[0];
			array[0] = temp;
			maxHeap(array, 0, i-1);
			
			System.out.println("After the "+ (count++) + " round sorting: ");
			display(array);
		}
	}

	/**
	 *
	 * @param array
	 * @param index The node that needs to maintain the nature of the heap
	 * @param size heap size
	 */
	public static void maxHeap(int[] array,int index,int maxIndex){
		int left = 2 * index + 1;
		int right = 2 * index + 2;
		int largest = index;
		if(left <= maxIndex && array[largest] < array[left]){
			largest = left;
		}
		if(right <= maxIndex && array[largest] < array[right]){
			largest = right;
		}
		
		if(largest != index){
			int temp = array[index];
			array[index] = array[largest];
			array[largest] = temp;
			maxHeap(array, largest, maxIndex);
		}
	}
	
	public static void display(int[] array){
		for(int i=0;i<array.length;i++){
			System.out.print(array[i] + " ");
		}
		System.out.println();
	}
	
	public static void main(String args[]){
		int[] originArray = {4,2,8,9,5,7,6,1,3};
		int[] array = new int[originArray.length];

        System.arraycopy(originArray, 0, array, 0, originArray.length);
        System.out.println("\n\nThe unsorted array order is: ");
        display(array);
        System.out.println("-----------------------");
        simpleSelectSort(array);
        System.out.println("-----------------------");
        System.out.println("The array order after selection sort is: ");
        display(array);
	        
	        
        System.arraycopy(originArray, 0, array, 0, originArray.length);
        System.out.println("\n\nThe unsorted array order is: ");
        display(array);
        System.out.println("-----------------------");
        heapSort(array);
        System.out.println("-----------------------");
        System.out.println("The order of the array after sorting is: ");
        display(array);    
	        
	}
}

Output result:

The unsorted array order is:
4 2 8 9 5 7 6 1 3
-----------------------
After the first round of sorting:
1 2 8 9 5 7 6 4 3
After the second round of sorting:
1 2 8 9 5 7 6 4 3
After the 3rd round of sorting:
1 2 3 9 5 7 6 4 8
After the 4th round of sorting:
1 2 3 4 5 7 6 9 8
After the 5th round of sorting:
1 2 3 4 5 7 6 9 8
After the 6th round of sorting:
1 2 3 4 5 6 7 9 8
After the 7th round of sorting:
1 2 3 4 5 6 7 9 8
After the 8th round of sorting:
1 2 3 4 5 6 7 8 9
-----------------------
The array order after selection sort is:
1 2 3 4 5 6 7 8 9


The unsorted array order is:
4 2 8 9 5 7 6 1 3
-----------------------
Constructed heap:
9 5 8 3 4 7 6 1 2
After sorting round 0:
8 5 7 3 4 2 6 1 9
After the first round of sorting:
7 5 6 3 4 2 1 8 9
After the second round of sorting:
6 5 2 3 4 1 7 8 9
After the 3rd round of sorting:
5 4 2 3 1 6 7 8 9
After the 4th round of sorting:
4 3 2 1 5 6 7 8 9
After the 5th round of sorting:
3 1 2 4 5 6 7 8 9
After the 6th round of sorting:
2 1 3 4 5 6 7 8 9
After the 7th round of sorting:
1 2 3 4 5 6 7 8 9
-----------------------
The order of the array after heap sorting is:
1 2 3 4 5 6 7 8 9

 

 


Guess you like

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