Heap sorting is a sort algorithm that sets the advantages of inserting sort and merging sort, that is, there is a good upper limit of the asymptotic operation, and it does not occupy additional running space. To put it simply, the idea of sorting it is as follows: Select the largest number from an array, and then selec
The idea of heap sequencingThe use of large top heap (small top heap) heap top record is the maximum keyword (minimum keyword) This feature, making it easy to select the maximum record (minimum record) from the disorder every time.Its basic idea is (Big Top heap):1) The init
elements to the bottom of the heapHeapup (size);//then adjust upward from the last position}Else { if(Ele.compareto (data.get (0)) > 0) {//Update if greater than the top element of the heapData.set (0, ele);//Replace top elementHeapdown (0);//adjust heap ordering from top to bottom } } }2. Adjust the code downwardAdjust the sort downward from the top of a heapPrivate void he
Case:Arrange a set of data from large to small 10, 16, 18, 12, 11, 13, 15, 17, 14, 19size=10Step 1: Initialize the heap according to the arrayStep 2. From the last root node (subscript (Size-1-1)/2), start traversing to the first root node, and sequentially order each of the little tree trees to construct a small heap:Step 3: Sort the heap by exchanging the data
Heap is an important structure in data structure, understand the concept and operation of "heap", can grasp heap sort quickly.
The concept of a heapThe heap is a special kind of complete binary tree (complete binary). If the value of all nodes of a completely binary tree is
Access to heap sort heap nodesThe 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
Heap sort Ah, actually is a data structure, binary tree, binary tree is divided into two fork tree and complete binary tree. A depth of k, and there are 2k-1 nodes called full two fork tree, complete binary tree: The depth of k, there are n nodes of the two-tree, when and only if each of its nodes with a depth of k in the full two-tree with a number of 1 to n nodes corresponding to the node, called a comple
Heap ordering, as the name implies, is a sort algorithm using data structure heap to sort.It doesn't make any sense to study the heap without regularity. The special heap has the largest heap (the parent node value is greater than
Original: Step by step Write algorithm (heap sort)"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "Heap sorting is another common recursive sort. Because heap sorting has excellent sorting performance,
Learning reference: Heap Sorting heap sort, sort six-heap sortHeap structure: A completely binary tree. Dagen:k[i] This paper test data: "Yan Granny data Structure" P281Because I am busy with my studies, I did not write code to visualize the tree structure. Readers, please f
]) // The comparison between the winner and the father in the left and right children break // take your child to the top of the knot, then the next round of screening with the location of the child's node H[start]= H[i]; Start = I;} h[start]= temp; // Insert the first discordant element } (2) Sorting the heap The sort begins with the first output of the heap
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 log2i+1), and need to take n-1
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
This is a creation in
Article, where the information may have evolved or changed.
For the algorithm of heap sorting, you can refer to my last article, "Heap sort". The article is about setting up a small top heap to carry out the sort, which is said to build a large top
Definition of heap:
N-keyword sequences kl,k2,...,kn called heaps, when and only if the sequence satisfies the following properties (referred to as heap properties):
(1) Ki≤k2i and Ki≤k2i+1 or (2) ki≥k2i and ki≥k2i+1 (1≤i≤)
If the vector stored by this sequence is R[1..N] as a storage structure of a complete binary tree, the heap is essentially a complete bina
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
Quick row:
The quick sort uses two points of thought, each time selects a datum element, the element which is compared to the datum element is placed in front of the datum element, is placed behind the datum element than the element of the datum element, and so on, and so on recursively subdivide, completes the sort. C + + code implementation:
void QuickSort (int a[], int l, int r) {
int temp = a[l];
1. The concept of heapsThe data structure of a heap is an array object, and the heap can be regarded as a complete binary tree (where each layer of the tree is filled and the last layer may be excepted); Each node in the tree corresponds to the element in the array that holds the value of the node;The heap can be divided into two categories:A) Maximum
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
Read a lot of bloggers wrote the principle of heap sorting, are explained quite clear, is the code implementation (mainly the Java language) some people dazzling. I have re-organized the code implementation of the heap sort (Java).What are the problems and irregularities, but also hope that the partners remind me to correct them in time. Thank!!
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.