PackageTestpackage;Importjava.util.Arrays; Public classHeap {//build a big Top pile Public Static voidBuildmaxheap (int[] a) { for(intI= (A.LENGTH/2) -1;i>=0;i--) {Adjustdown (a,i,a.length); } } //Downward Adjustment Public Static voidAdjustdown (int[] A,intIintLen) { inttemp,j; Temp=A[i]; for(j=2*i+1;j//J is the child node of the current I, the default is the left node if(J+1//If the right node is large, select the right nodeJ + +; if(a[j
Package Com.juxuny.heap;import Java.lang.reflect.array;import Java.util.arraylist;import java.util.List;/** * Created by Juxuny on 2015/2/10. */public class Heap {private int MAX = 2 Import Com.juxuny.heap.heap;import com.juxuny.heap.node;import java.util.random;/** * Created by Juxuny on 2015/2/10. */public class Main {public static void Main (string[] args) { random random = new random (); He
!!!----------------------------------------------------------translation is as follows----------------------------------------------------------- -------Stacking, quick-row, and information entropyThere are many network articles for the comparison and analysis of the heap and the fast row.Most of them are of the view that "the average time efficiency of both algorithms is gradual in Nlogn, but in practical tests, the efficiency of a good fast row is u
Import Java.util.Arrays;Public class Heapsort {The array arr subscript 0 is not used, the number to be sorted is placed in the position labeled 1 ~ arr.length-1, and the elements on these positions are sortedPublic static void sort (int arr[]) { Heapsort hs = new Heapsort (); hs.buildmaxheap (arr, 1, arr.length-1); System. out.println ("buildmaxheap result:" + arrays.tostring (arr)); hs.adjustheap (arr, 1, arr.length-1); }Build Maximum Heappu
Data Structure --- implement heap sorting by c language --- sort by c heap
The process is shown in the figure below: (create a heap first, adjust, insert, and output)
Code:
// HeapSort // Yang Xin # include
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced with
This article illustrates the heap ordering of the summary of Java sorting algorithms. Share to everyone for your reference. The specific analysis is as follows:
1991 Computer Pioneer Laureate, Stanford University computer science professor Robert Floyd (Robert W.). Floyd) and Williams (J. Williams) jointly invented the famous heap sorting algorithm (Heap
Build heaps, and heap sortCode Listing 1:#include Code, 2: #include Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Build the heap, and sort the heap
, let's take a look at this article. Other algorithms that perform more efficiently, such as merge sorting, such as fast sorting, heap sorting, and so on.1. Merge sort Let's take a look at what the merge sort is and how the merge sort is implemented.Merge sort belongs to a
Text descriptionIn the heap sort, the data to be sorted can also be represented by a complete binary tree, and the values of all non-terminal nodes of the complete binary tree are not greater than (or less than) the values of their left and right child nodes. Thus, if the sequence {k1, K2, ..., kn} is a heap, then the top element of the
;}voidBuild_heap (intArray[],intN) { inti; /*the implementation of step 2, starting from the last non-leaf node to adjust the heap until the root node*/ for(i = (n-2)/2; I >=0; i--) {adjust_heap (array, n, i); } return;}voidHeap_sort (intArray[],intN) { intI, temp; Build_heap (array, n); /*the implementation of step 4, each cycle of the first element of the exchange heap and the last element
1 /**2 * Heap sequencing ideas: O (NLOGN)3 * Use the maximum heap, pass in an array, first use the number to build the heap, maintain the nature of the heap4 * Replace the first number with the last number in the heap, because the first number is the largest5 * Reduce the size of the
root node.Like such a group of numbers a[]={16,7,3,20,17,8}, we build heapsAnd then, in turn, adjustSuch a heap was built up. The rest of it is easy.After the heap we can easily find that the heap has a very good nature, that is, the root node value of the lock all the values are large (small), then we first select the root node, put the array "n-i+1", and then
spacewhile (Iif (r[i].key{R1[k]=r[i];i++;k++;}else//elements in the 2nd paragraph are put into R1{R1[K]=R[J];j++;k++;}while (I{R1[k]=r[i];i++;k++;}while (J{R1[K]=R[J];j++;k++;}for (k=0,i=low;iR[I]=R1[K];Free (R1);}An algorithm for merging a tripvoid Merpass (RecType r[],int length,int N)//The entire sort sequence is merged{int i;for (i=0;i+2*length-1Merge (r,i,i+length-1,i+2*length-1);if (i+length-1Merge (r,i,i+length-1,n-1); Merge these two sub-tabl
The implementation of the sort heap and the use of heaps to sort table of Contents
A few basic definitions
Heapy: Adjusting the heap
Build: Build a heap
Insert: Inserting a new element
Removetop: Remove the top of the
basic concept of the heap and related knowledge, now we discuss the sort of Dagen .For a large heap, the root node is the largest element, selecting the largest element and swapping it with the last element of the sequence so that the largest element succeeds in homing. But at this point the structure of the heaphas been compromised, the element's position need
A simple choice of sorting1, thought: Each time the traversal remembers the current smallest (large) elements of the position, and finally only need a single exchange operation can be placed in the appropriate location. Compared with bubble sort, the number of mobile data is few, save time, performance is better than bubble sort.2. Complexity of Time:Best: O (N2), positive sequenceWorst: O (N2), reverse ord
For example , array a:[0:5,1:2,2:4,3:3,4:1]The results are as follows: [1, 2, 3, 4, 5], but sometimes there is a need to keep the pre-order position in a single array, as in the preceding example: [4, 1, 3, 2, 0], so a separate array ordering process is implemented using heap sorting.The code is as follows:functionArraykeys (arr) {vari = 0, Len=arr.length, Keys= []; while(I Len) {Keys.push (i++); } returnkeys; } //Determine if the vari
/** Select sort of heap sort* Set up a fully binary tree according to the sequential storage of the complete binary tree* If Dagen: L (i) >=l (2*i), L (i) >=l (2*i+1)* If small Gan: L (i) * At this point, the initial heap is built and the next job is to make adjustments* adjusted is from [n/2]~1.* For Dagen: For the [N
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.