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
the priority queue, each insertion of an element is O (Logn), take out O (1), so that you can achieve their own priority queueCode:#include #include #include using namespace STD;Const intN =1001000;intA[n];voidBalance (intXintN) {intL = x+x;intr = x+x+1;intmid = x;if(Lif(Rif(X!=mid) {swap (a[x],a[mid]); Balance (Mid,n); }}voidHeap_sort (intN) { for(inti=n/2-1; i>=0; i--)//Adjust the heap firstBalance (I,n); for(inti=n-1; i>=0; i--) {Swap (a
and the complete C language code
A demonstration of the heap sorting algorithm. First, the elements are re-queued to match the conditions of the heap. The structure of the heap tree is simply plotted before the sorting process in the diagram.Complete C-Language implementation code:
#include #includ
value is itself - if(HeapSize b)101 {102Max =A;103 }104 Else if(HeapSize >=b heapsize c) the {106 if(Arr[a] Arr[b])107 {108Max =b;109 } the }111 Else the {113 if(Arr[a] >Arr[b]) the { theMax =A; the }117 Else118 {119Max =b; - }121 if(Arr[max] Arr[c])122 {123Max =C;124 } the
[k+ +] = nums[i++]; } //move the remaining number in the right side into the array while(J High ) {Temp[k+ +] = nums[j++]; } //overwrites the number in the new array with the Nums array for(intK2 = 0; K2 ) {Nums[k2+ Low] =TEMP[K2]; } }Second, heap sorting algorithm1. Basic ideas:Heap sorting is a sort of tree selection, which is an effective improvement on direct selection s
++] = Nums[j++];} //for ( Span style= "COLOR: #0000ff" >int k2 = 0; K2 ) {nums[k2 + low] = TEMP[K2];} Second, heap sorting algorithm1. Basic ideas:Heap sorting is a sort of tree selection, which is an effective improvement on direct selection sorting.Heap definition: A sequence with n elements (h1,h2,..., HN), when and only if satisfied (hi>=h2i,hi>=2i+1) or (hiThought: Initially, the sequence of number
elements labeled 1~n in a set of arrays sorted by a heap sorting algorithm $ */Panax Notoginseng voidHeap_sort (Type array[],intN) - { the inti; + time_t start,end; AStart =clock (); the for(i=n/2; i>=1; i--) + { -Sift (array,i,n);//Build the initial heap $ } $ for(i=n;i>=2; i--)//controlling the sorting process - { - type temp; thetemp
ObjectiveThe interview was asked a question (http://www.voidcn.com/blog/u010943214/article/p-3808842.html), and then relive the heap sort.Problem:Give you a doubly linked list, ordered output,Limit:Spatial complexity O1,Time complexity Nlogn, the worst can not degenerate N2Ideas:1 building a binary tree based on a doubly linked list2 heap sorting of two-fork treesDefinition of a heapProperties of the heapHo
Some basic definitions of heap ordering can be found in another blog post I reproduced. http://blog.csdn.net/u010275850/article/details/45311661In fact, in the learning heap when the careful classmate can be found, as long as the deletion of the data, you can get an orderly sequence. Heap sequencing is also taking advantage of such ideas.Algorithm implementation:
different sorting methods have different characteristics, some are fast, but not stable, some stable, but not in-situ sorting, some in-place sorting, but the worst case time complexity is not good. So is there a sort of order that can assemble all of the above requirements?Five concluding remarksThis paper introduces the two-fork heap, and the heap ordering base
This article mainly for you in detail the implementation of the PHP sorting heap sorting (heap sort) algorithm, with a certain reference value, interested in small partners can refer to
Algorithm Introduction:
Here I directly quoted in the "Big talk data structure" inside t
array
Buildmaxheap (A,n);
////The largest element in the array at root a[1], it can be exchanged with a[n] to achieve the final correct position
For (int i = n;i >= 2;i--) {
//Exchange
temp = A[i];
A[i] = a[1];
A[1] = temp;
//a[i] has reached the correct position, removed from the heap
n--;
//re-adjust to maintain maximum heap properties
Maxheap (A,1,n);
}
return 0;
elements actually stored in the heap, but not necessarily all of the data we need to build the maximum heap. Introduction to the algorithm to get the Zoozi node is 2xi but our array is counted from 0, so the Zoozi node becomes the 2xi+1,buildheap is to build a maximum heap, we go to 2 per cent of the length of the rea
Algorithm ideaHeap sorting utilizes the feature of the maximum (or minimum) keyword for the maximum heap (or small Gan) heap top record, making it easy to select the record of the largest (or smallest) keyword in the current unordered area. 1. The basic idea of using the maximum heap
= target * 2; // nChild points to left childIf (nChild + 1 {NChild ++; // nChild points to a child with a large keyword (check whether there are left children. If yes, compare the left and right children)}If (nTemp {Data [target] = data [nChild];Target = nChild; // reprocess the byte points of the adjusted Node}Else break;}Data [target] = nTemp; // put the final elements to be adjusted in the appropriate position}Overall implementation code:
[Cpp]*****************
only need to maxheapify and not need maxheapbuild
* This will start from n-1 to 2.
*/
void Maxheapsort (int a[], int maxSize, int tmp[]) {
Maxheapbuild (A, maxSize); //n
For (int i = maxSize; i > 0;) { //cycle n-1 times
Swap (A, a + i);
Tmp[i] = A[i];
i--;
if (i = = 0) {
Tmp[0] = a[0];
} Else {
Maxheapindex (A, 0, i); //logn executed n-2 times.
}
}
For (int i = 0; I
A[i] = Tmp[i];
}
}
iv. Complexity of
code for this step is simple:/* Heap Sorting, Idea: * First maxheapbuild, make the root node the maximum value * then the root node and the last node's value is exchanged * then the last node is disconnected (maxsize-=1) * and then maxheapify, Because this time the root node of the left and right sub-tree is still a large heap, so only need maxheapify and do not need maxheapbuild * So from n-1 start cycle
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.