/*************************************** *********************
Function: heap sorting
Author: glq2000 [glq2000@126.com]
Date: Tues, 2010-8-3
Note:
Pay attention to two issues when writing the heap sorting Function
1. How to Create a heap from an unordered sequence?
2. How can I adjust the remaining element to become a new hea
1#include 2 3 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/4 //Karllen5 //46 7 voidQuickSort (int*a,intLowintHigh);//Quick Sort8 voidHeapsort (int*a,intn);//Heap Sort9 voidEleswap (inta,intAMP;B);//Exchanging DataTen intLeftindex (intindex);//returns the subscript of the left child node One intRightindex (intindex);//returns the subscript of the right child node A voidMakeheapmax (int*a,intlength);
Hill sort (by Donald Shell):Suppose you give an array that needs to be sorted and sort by 5-interval. The drawings are as followsWe slowly began to sort by the smaller intervals (and finally the interval was only 1).Heap Sort:First look at the insertion sortvoidSelection_sort (ElementType a[],intN) { for(i =0; i )
node of the I node is labeled (i–1)/2. The index of the left and right sub-nodes is 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node of the left and right sub-nodes subscript 1 and 2 respectively.Heap operation--Insert DeleteThe following gives the "data structure C + + language description" of the minimum heap of the establishment of the insertion and deletion of the plot, and then give my implementation code, it is best to look at the e
Heap sorting is a sort of selection, with a time complexity of O (Nlogn).Definition of a heapThe sequence of n elements, {k1,k2,...,kn}, is called a heap when and only if one of the following relationships is met.Case 1:ki minimized heap or small top heap )Case 2:ki >= k2i
Heap sorting is a sort of selection, with a time complexity of O (Nlogn).Definition of a heapThe sequence of n elements, {k1,k2,...,kn}, is called a heap when and only if one of the following relationships is met.Case 1:ki minimized heap or small top heap )Case 2:ki >= k2i a
of the array), and then maintain the properties of the maximum heap by layer-by-level heapsort::insertmaxheap (int a) {/* if (M_PA = = NULL) return; if (m_nheapsize = = 0) return; */M_nheapsize + = 1; M_pa[m_nheapsize-1]=a; int index=m_nheapsize-1; while (index>0) {if (M_pa[index]>m_pa[parent (index))) {swap (M_pa[index], m_pa[parent (Ind ex)]); Index=parent (index); } else index=0;//note here. A layer already satis
Heap SortHeap sorting is a sort of selection that takes advantage of the nature of the heap. Let's discuss the heap.1. HeapThe heap is actually a completely binary tree, whatever a non-leaf node satisfies the nature:KEY[I]That is, no matter what a non-leaf node keyword is no
I node is (I-1)/2. Its subnode subscript is 2 * I + 1 and 2 * I + 2 respectively. For example, the subnode subscripts of the first 0th nodes are 1 and 2 respectively.
Heap operation-insert and delete
The following describes how to create, insert, and delete the smallest heap in "Data Structure C ++ language description Description", and then shows the implementation code. It is best to first clarify the di
I node is (I-1)/2. Its subnode subscript is 2 * I + 1 and 2 * I + 2 respectively. For example, the subnode subscripts of the first 0th nodes are 1 and 2 respectively.
Heap operation-insert and delete
The following describes how to create, insert, and delete the smallest heap in "Data Structure C ++ language description Description", and then shows the implementation code. It is best to first clarify the di
Heap Sort and quick sort , merge sort are the common sort methods of time complexity O (N*LOGN). Before learning heap sequencing, let's first explain what is a two-fork heap in the data
and right sub-nodes are labeled 2*i+1 and 2*i+2.The 0 location will not be used in this article.3. Downward adjustmentThe downward adjustment is to maintain the nature of the heap. Its input is the array a and subscript I. It first looks at whether the node is larger than the value of its left and right child nodes, and if not, the larger values in the left and right child nodes are exchanged. The next level of h
node of the I node is labeled (i–1)/2. The index of the left and right sub-nodes is 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node of the left and right sub-nodes subscript 1 and 2 respectively.Heap operation--Insert DeleteThe following gives the "data structure C + + language description" of the minimum heap of the establishment of the insertion and deletion of the plot, and then give my implementation code, it is best to look at the e
node of the I node is labeled (i–1)/2. The index of the left and right sub-nodes is 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node of the left and right sub-nodes subscript 1 and 2 respectively.Heap operation--Insert DeleteThe following gives the "data structure C + + language description" of the minimum heap of the establishment of the insertion and deletion of the plot, and then give my implementation code, it is best to look at the e
node of the I node is labeled (i–1)/2. The index of the left and right sub-nodes is 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node of the left and right sub-nodes subscript 1 and 2 respectively.Heap operation--Insert DeleteThe following gives the "data structure C + + language description" of the minimum heap of the establishment of the insertion and deletion of the plot, and then give my implementation code, it is best to look at the e
that, the subtree of each node is then filtered forward, making it a heap until the root node.2. After removing the root node, rebuild the heapSwap the last one with the root node. In the way of building the heap (only the first number is to be built to sort the heap), the number of the first n-1 is built.#include /*1
I node is (I-1)/2. Its subnode subscript is 2 * I + 1 and 2 * I + 2 respectively. For example, the subnode subscripts of the first 0th nodes are 1 and 2 respectively.
Heap operation-insert and delete
The following describes how to create, insert, and delete the smallest heap in "Data Structure C ++ language description Description", and then shows the implementation code. It is best to first clarify the di
] with the last element of the unordered zone, resulting in a new unordered area (R1,R2 ...). Rn-2) and the new ordered area (RN-1,RN). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is complete.The procedure is as follows:1) Initialize the heap: R[1..N] is constructed as a heap;2) Swap the top element of the current unordered area R[1] with
the worst case, the run time is O (h) (H is the height of the node to be adjusted, the base height is 0, and the heap top height is floor (logn)).Time complexity of the heap adjustment: O (LOGN)Time complexity of building a heap: O (n)Analysis of time complexity: reference http://www.cnblogs.com/zabery/archive/2011/07/26/2117103.htmlThird, how to
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.