node until the root element is filtered. This method is called "filtering" and requires a circular filtering of N/2 elements.But we can also learn from the idea of "nothing". We can view the first element as a heap and then add the new element to it continuously. This method is called "Insert Method" and requires a circular insertion of (n-1) elements.Because of the different ways of filtering and inserting methods, the same data, the
Heap Sort HeapSort is a tree-like sorting method. The characteristic of heap sorting is that during the sorting process, R [l .. n] As a Complete Binary Tree ordered storage structure, using the internal relationship between the parent node and the child node in the full Binary Tree, select the maximum (or minimum) keyword in the current unordered Area).
The wors
-sequences, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence * time complexity is O (nlogn) * Stable sorting method *@paramNums Array to sort *@returnOutput an ordered array*/PublicStaticInt[] Sort (Int[] Nums,int Low,IntHigh) {int mid = (low + high)/2;if (Low High) {//LeftSort (nums, low, mid);//Right
Heap Concept:is a complete binary tree structure, divided into Dagen and small Gan, each non-leaf node is greater than (Dagen is greater than, small Gan thing less than) its two child nodes.Heap Sort: The steps are divided into three stages:1: Initialize large Gan (Keng Gen can also, this article takes Dagen as an example)2: Swap heap top Max and array last,3: La
Algorithm idea Simple description:
Heap ordering is a sort of tree selection, which is an effective improvement of direct selection sort.
The heap is defined as follows: A sequence with n elements (h1,h2,..., HN), which is called a heap only if it satisfies (hi>=h2i,hi>=2i
interchange, for each non-terminal node, in fact, two times the comparison and interchange operations, so the entire build heap time complexity is O (n).In the formal order, the first time to fetch the heap top record to rebuild the heap needs O (logi) time (a complete binary tree node to the root node distance of log
Legend Reference http://www.cnblogs.com/mengdd/archive/2012/11/30/2796845.htmlOriginal reference http://blog.csdn.net/eseaqyq/article/details/7497575The following is an adjustment for the heapvoid Heapajust (int data[],int i,int length){int nchild;int ntemp;for (Ntemp=data[i];2*i+1{nchild=2*i+1;if (Nchild{nchild++;}if (Ntemp{Data[i]=data[nchild];Data[nchild]=ntemp;}else//if it's bigger than the biggest kid, it doesn't trade.Break}}Heap Sortvoid heapso
recursively heapifyHeapdown (smallest); }3. Adjust the code upAdjust the sort upward starting from position IPrivate voidHeapup (inti) {if(i = = 0)//if it is a heap top, return it, no need to adjust the return; while(i > 0) {//is not at the top of the heap, and the value of the current node i is smaller than the parent node, continue to adjust upward
, so the stack top and tail elements are exchanged to //The largest element is saved at the tail and does not participate in the subsequent adjustments varSwap =Elements[i]; Elements[i]= Elements[0]; elements[0] =swap; //Adjust the maximum) element to the top of the heapHeadadjust (Elements, 0, i); }}varelements = [3, 1, 5, 7, 2, 4, 9, 6, 10, 8];console.log (' Before: ' +elements); sort (elements); Console.log (' After: ' + elements);Efficiency:
Heap sort explanation and code (C ++)
Two steps are involved in heap sorting.:
Step 1:YesDading heap(Sorted in ascending order) orSmall top heap(Sorting from small to large ),Create from bottom up; For example, when a heap is cre
1. Heap SequencingA. Definition of a heapThe n element sequence {k1,k2,..., kn} is called a heap when and only if the following relationships are met.Ki1 (Keng Gen) ki>=k2i and ki>=k2i+1 (Dagen)The following is for the maximum heapB. Nature of the maintenance heapMax-heapify the sub-tree with the root node of I as the maximum heap property by letting the value o
To learn heap sorting, first you need to understand the concept of the heap, which is an array. Can be approximated as an array storage method of a complete binary tree. But with him there are other properties, which are similar to binary sort trees. With the maximum heap and the minimum
, Len); Wu } - } About $ //constructs a maximum heap from an unordered array of input data with a time complexity of O (n) - intBuild_max_heap (int*a,intlen) - { - intStart =PARENT (len); A for(inti = start; I >=0; i--) + { the max_heapify (A, I, Len); - } $ return 0; the } the the intHeapsort (int*a,intlen) the { - intLength =Len; in int*res =New int[Len] {0};//record
packageorg.rev.algorithm;/*** Heap ordering, time complexity is O (Nlogn), is a sort of selection using the nature of the heap. ** The Big Top heap is a fully binary tree, all the parent nodes are greater than or equal to its left and right child nodes, or a[i]>=a[2i+1]a[i]>
with I as the root node to make it the largest heap
Maxheap (A,i,n);
}
return 0;
}
A array
16
7
3
20
17
8
Initial heap:Bottom-up adjustment starts from the last non-leaf node:(a) (b) (c) (d)Each adjustment is from the parent node, the left child node, the right child node three of the largest selection of the parent node to Exchange (after the Exchange may cause the exchange of the child n
comparisons, comparison performed repeatedly latter sorted. heap saves some results by using structure, which reduces comparisons. worst complexity o (nlogn). average performance closer performance. appropriate files fewer records due required build initial heap. @authorKPP **/ Public classHeapsort { Public Static voidMain (string[] args) {//TODO auto-generated
Heap Sort
Heap ordering is a sort of tree selection, which is an effective improvement of direct selection sort.Basic idea:The heap is defined as follows: A sequence with n elements (k1,k2,..., kn), if and only if satisfied
Case 1:ki Case 2:ki >= k2i and Ki >= k2i+1 (maximi
1. Quick SortFast sequencing is an unstable sorting algorithm with an average time complexity O (NLGN). The quick sort is sorted by using partition () . partition () has two implementations, (1) uses two pointers, a head pointer, and a tail pointer, sorted by the number of digits indicated by the exchange of the tail-end pointer ; ( 2 The first and last two pointers traverse from left to right, and if the
, test scores are the sort of key words. In practice, the attribute used by each datasheet as a sort key depends on the specific application needs.
There are many ways to sort, and in terms of their overall performance it is difficult to propose a method that is considered the best, each with its own advantages and disadvantages, and suitable for use in differen
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.