the minimum heap is usually used to construct a priority queue.
2. Create a heap
In the heap sorting algorithm, you first need to build a random array into a heap, and then sort it based on the heap. Take the array a [7] = {,
Before looking at other algorithms and data structure of the book to find the heap sorting code more difficult to write, read the "Introduction to the algorithm" found that is not so difficult, so I started to write a. The principle is based on an introduction to the algorithm of the third version of the heap sorting pseudo code.
#include
The heap
In the third edition of the introduction to algorithms, " heap sorting is a sort of in-place, and if you try to introduce an array, you lose that advantage ." The base of the heap sort is to sort on the original heap, and the set
Before learning heap sequencing, first look at the characteristics of the binary heap:1. The value of the parent node of the binary heap is always greater than or equal to (or less than or equal to) its left and right children;2. The left and right subtree of each node is a two-fork heap of such a tree.If the parent no
exceed 1G. First step borrowingHashPre-processing of this batch of massive data: first(Maintain aKeyto beQueryString,Valuefor theQuerythe number of occurrences, i.e.Hashmap (Query,Value), each time you read aQueryif the string is notTable, the string is added, and theValuevalue is set to1, if the string is in theTable, add a count of the string. In the end weO (N)(Nto be1Be sure to iterate through the entire array once and for each of the statistics officesQueryThe number of occurrences) is use
, we start from the last branch node (10/2) with a value of 72, which is for each branch node, 36 45 adjustment (sinking ).
Program:/* build a heap based on the nature of the tree. The first half of the Tree node must be a branch node with children, so from here we start to adjust the initial heap */public static void adjust (List
(4) Maximum heap sorting
//
Basic idea:diagram: (88,85,83,73,72,60,57,48,42,6)Average Time complexity:O (Nlogn) due to the time complexity of each re-recovery heap is O (logn), a total of N-1 redo heap operations, coupled with the previous build heap n/2 times downward adjustment, each time the complexity
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
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
Heap sorting can be used as real-time sorting, is the original sort, does not require additional space, time complexity is O (NLGN), the best worst-case average is this, but is not stable ordering, the following is the C language implementation code://in order to simplify programming, the first position in a heap array
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
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
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
Prime + Heap is God's time to optimize a lot, by the way, the Heapsort to theDetailed ViewHeapsort uses the structure of the complete binary tree + large (small) top cone to exchange the elements of the cone and the end of the cone at the same time large (small) top cone elements-1, iterative n-1 secondary OKI'm here on a small-to-big shoot.1 //Heap Sort Time
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
]) // 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
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
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.