Access to heap sort heap nodes
The heap is usually implemented by a one-dimensional array. In the case where the array start position is 0:
The left child node of parent node I is in position (2*i+1);
The right child node of parent node I is in position (2*i+2);
The parent node of child node I is in position floor ((i-1)/2);
Operation of the Heap
In a heap's data structure, the maximum value in the heap is always at the root node (the minimum value in the heap is in the root node if the heap is used in the priority queue). The following actions are defined in the heap:
Maximum heap adjustment (max_heapify): Adjusts the end child nodes of the heap so that the child nodes are always smaller than the parent node
Create maximum heap (build_max_heap): Reorder all data in the heap. The maximum heap adjustment is called from the last parent node until the first parent node (root node)
Heap sort (heapsort): Removes bits at the root of the first data and makes the recursive operation of the maximum heap adjustment
Java
1 Packagetest;2 3 Public classSolution {4 5 Private voidMaxheapify (int[] Nums,intIndexintheapsize) {6 intIMax, ILeft, IRight;7 while(true) {8IMax =index;9ILeft = 2 * index + 1;TenIRight = 2 * index + 2; One A if(ILeft < heapsize && Nums[imax] <Nums[ileft]) { -IMax =ILeft; - } the if(IRight < heapsize && Nums[imax] <Nums[iright]) { -IMax =IRight; - } - + if(IMax! =index) { - intTMP =Nums[imax]; +Nums[imax] =Nums[index]; ANums[index] =tmp; atindex =IMax; -}Else { - Break; - } - } - } in - Private voidBuildmaxheap (int[] nums) { to intLastparent = (int) Math.floor ((nums.length-1)/2); + for(intI=lastparent; i>=0; i--) { - maxheapify (Nums, I, nums.length); the } * } $ Panax Notoginseng Public voidHeapsort (int[] nums) { - buildmaxheap (nums); the + for(intI=nums.length-1; i>=0; i--) { A intTMP = Nums[0]; theNums[0] =Nums[i]; +Nums[i] =tmp; -Maxheapify (nums, 0, i); $ } $ } - - Public Static voidMain (string[] args) { the //TODO auto-generated Method Stub - int[] Nums = {3,5,2,1,0,9,4,5,6,7,3,2,6};WuyiSolution s =Newsolution (); the S.heapsort (nums); - for(intnum:nums) { WuSystem.out.print (num + ""); - } About } $ -}
Reference
https://zh.wikipedia.org/wiki/Heap Sorting
http://bubkoo.com/2014/01/14/sort-algorithm/heap-sort/
Kth largest Element in an Array
Source: Https://leetcode.com/problems/kth-largest-element-in-an-array
Find the kth largest element in an unsorted array. Note that it was the kth largest element in the sorted order and not the kth distinct element.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
Note:
You may assume k are always valid, 1≤k≤array ' s length.
Using the idea of heap sequencing, create the largest heap, repeat the " remove bits at the root of the first data, and do the maximum heap adjustment recursive operation " This step, the K-Removed root node is the data of the K-large element.
can also take advantage of the idea of fast sequencing, see http://www.cnblogs.com/renzongxian/p/7465453.html
Java
1 classSolution {2 Private voidMaxheapify (int[] Nums,intIndexintheapsize) {3 intIMax, ILeft, IRight;4 while(true) {5IMax =index;6ILeft = 2 * index + 1;7IRight = 2 * index + 2;8 if(ILeft < heapsize && Nums[imax] <Nums[ileft]) {9IMax =ILeft;Ten } One if(IRight < heapsize && Nums[imax] <Nums[iright]) { AIMax =IRight; - } - the if(IMax! =index) { - intTMP =Nums[imax]; -Nums[imax] =Nums[index]; -Nums[index] =tmp; +index =IMax; -}Else { + Break; A } at } - } - - Private voidBuildmaxheap (int[] nums) { - intLastparent = (int) Math.floor ((nums.length-1)/2); - for(intI=lastparent; i>=0; i--) { in maxheapify (Nums, I, nums.length); - } to } + - Public intFindkthlargest (int[] Nums,intk) { the buildmaxheap (nums); * for(intI=nums.length-1; i>=nums.length-k; i--) { $ intTMP = Nums[0];Panax NotoginsengNums[0] =Nums[i]; -Nums[i] =tmp; theMaxheapify (nums, 0, i); + } A returnnums[nums.length-K]; the } +}//7 ms
Heap sort && Kth largest Element in an Array