Here is an example of how to implement a two fork heap and a heap ordering under Python. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
The heap is a special kind of tree structure, and the data storage in the
In many applications, we often need to handle processing objects in priority situations, such as first processing the highest-priority objects, and then processing the objects with the secondary height. One of the simplest examples is when you play a game on your phone, if you have a call, the system should prioritize the incoming calls.In this case, our data structure should provide two of the most basic operations, one is to return the highest priority object, and the other is to add a new obj
featuresHeapsort is a tree-based 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 inherent relationship between the parent and child nodes in the Complete Binary Tree [see the binary tree ordered storage structure ], select the record with the maximum or minimum keyword in the unordered area.
4. Differences between
Heap Sorting concepts:
First non-leaf node: part less than SIZE/2;
Range of non-leaf nodes: [0, SIZE/2]; (Note that left-close right-open)
Maximum heap: Satisfies parent nodehead, arr[head]
the subtree of a non-leaf node needs to be adjusted (the tree without the child nodes is adjusted)PS: the subscript used in full-text is starting from 0
Kids Learn data structure (11): Heap Sort (a) what is a heapThe heap is actually a completely binary tree, with any one of its non-leaf nodes satisfying the nature:KEY[I]KEY[I]>=KEY[2I+1]KEY>=KEY[2I+2],That is, any non-leaf node keyword is not greater than or less than the key of the child node.The heap is divided into
adjusted downward.Summarize this algorithm for creating heaps. To create a heap of n elements, first I can encode the n nodes from 1 to n in the top-down, left-to-right way. This allows the N nodes to be converted into a complete binary tree. Immediately starting from the last non-leaf node (node number N/2) to the root node (node number 1), scan all nodes one by one, adjust the current node downward as necessary until the subtree that is the root no
is 10 million, but if you remove the duplicates, no more than 3 million. The higher the repetition of a query string, the more users are queried for it, the more popular it is, please count the hottest 10 query strings, which requires no more than 1G of memory.Solution: Although there are 10 million query, but because of the high repetition, so in fact only 3 million of the query, each query255byte, (300w*255b
Hash statistics: This batch of massive data preprocessing (maintain a key fo
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
In many applications, we often need to handle processing objects in priority situations, such as first processing the highest-priority objects, and then processing the objects with the secondary height. One of the simplest examples is when you play a game on your phone, if you have a call, the system should prioritize the incoming calls.
In this case, our data structure should provide two of the most basic operations, one is to return the highest priority object, and the other is to add a new ob
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 the beginning:
In front of the simple selection sort
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 sort(1) First the initial file R[1.
Introduction
Note: Because no public editor is enabled, it is convenient: when the following time complexity is involved, its accesskey is replaced by the following symbol:
This article describes three algorithms that can sort n numbers in O (nlgn) Time: Merge Sorting, heap sorting, and quick sorting. Both algorithms are based on comparative sorting. The first two algorithms reach the upper bound in the w
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
million, but if you remove the duplicates, no more than 3 million. The higher the repetition of a query string, the more users are queried for it, the more popular it is, please count the hottest 10 query strings, which requires no more than 1G of memory.Solution: Although there are 10 million query, but because of the high repetition, so in fact only 3 million of the query, each query255byte, (300w*255b
Hash statistics: This batch of massive data preprocessing (maintain a key for the
maximum heap using the process max-heapify. The principle is very simple, that is, from the bottom 2nd level (for illustrative convenience, we call the root node 1th layer, its sub-node is called the 2nd layer, and so on) to start, called Max-heapfy method, until the root node. The algorithm is described as follows: We can prove the correctness of the above algorithm by using the cyclic invariant (see algorithm Introduction (1)) described previously
); }}Four-heap sorting algorithmInitially, the heap sorting algorithm uses BUILD-MAX-HEAP to build the largest heap of input array arr[0...n-1]. Because the largest element in the array is always in the root node arr[0], by swapping it with arr[n-1], we can put the element in the correct position. At this point, if
The heap can be regarded as a complete two-fork tree, with the exception of the lowest level, where each layer is full, which allows the heap to be represented by an array, with each node corresponding to an element in the array.
The relationship between the array and the heap:
Binary heaps are generally divided into two types: maximum
, 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.