Google Searching


Selasa, 26 Januari 2010

Heap Sort

The tree diagram in Heap Sort
Understanding Heap is a data structure that meets the tree-shaped nature of the heap that is, if B is a child of A, then the value stored at node A is greater than or equal to the value stored at node B. This causes the element with the largest value is always located at the root position, and the heap is called a max heap. (If the comparison is diterbalikkan smallest element always at the root node, the heap is called the min heap). Therefore, the heap used to implement the priority queue. Operations that are used for the heap are:

• Delete-delete-max or min: remove the root node of a max or min heap.
• Increase or decrease-key-key: change the value stored in a node.
• Insert: add a value into the heap.
• Merge: merge two heap to form a new heap which contains all the elements forming the heap.

.2 Heap Types

.2.1 Binary heap
is the heap created by using binary trees.

.2.2 Binomial heap
is the heap created by using the binomial tree.

Binomial tree is defined recursively if is:
• A binomial tree with height 0 is a single node
• A binomial tree with height k has a root node whose children are the roots of the binomial trees.

.2.3 Fibonacci Heap
Fibonacci heap is a collection of trees that form the minimum heap.
Trees in this data structure does not have a specific shape and in extreme cases this heap contains all the elements in a different tree or a single tree with a height advantage of
Fibonacci heap is a heap enough when combined with the combining of two lists of trees.

Heap Sort is a sorting algorithm based on comparative data, and selection are among the sort. Although slower than quick sort in most machines, but the heap sort has the advantages of the complexity of the algorithm in the worst case is n log n.
Heap sorting algorithm of this sort to sort the contents of an array of inputs by looking at the array input as a Complete Binary Tree (CBT). After the Complete Binary Tree (CBT) can be converted into a heap tree. After the Complete Binary Tree (CBT) is changed into a priority queue.
Heap sorting algorithm starts from building a heap of data collection to be ordered, and then delete the data that has the highest value and placing it in the end of the array which has been ordered. After moving the data with the largest value, the next process is to rebuild the heap and move the greatest value on the heap and put it in last place in the sorted array of other data not specified. This process is repeated until no more data left in the heap and the sorted array is full. In our implementations require two arrays - one to store the heap and one for storing data that is sorted. But for memory optimization, we can use only one array only. That is the way to exchange the contents of the root with the last element in the heap tree. If the memory is not a problem it can be fixed using two rows of input array and the array results.
Heap Sort enter input data into a heap data structure. Greatest value (the max-heap) or the smallest value (in min-heap) is taken one by one until the end, the value is taken in the ordered sequence.
The algorithm for heap sort:
heapSort function (a, count) is
Input: an array is not sorted a long length with
(first place in a max-heap) heapify (a, count)
end: = count -1
0 do" onmouseover="'#ebeff9'" onmouseout="'#fff'">while end> 0 do
remove ()
reheapify ()
end: = end - 1

Algorithm Heapify
Heapify algorithm is to build a heap from the bottom up, successively changing down to build the heap. The first problem we must consider the operation of the heapify is where we must begin. When we try to roots heapify operations will occur runut-up operations like bubble sort algorithm that would cause the complexity of the existing time will double.
A different version is to build a heap in a top-down and turns up to be conceptually simpler to handle. This version begins with an empty heap and successively enter the data. Another version is to form a tree-lined heap heap-subtree starting from the bottom subtree. If the subtree-subtree of a node is formed from the heap then the tree node is easily made with a flow heap tree down.
Once tested, the most efficient idea is the final version, the complexity of the algorithm in the worst case is O (n), while the forming heap-heap tree tree from top to bottom its complexity O (n log n) Thus, the main algorithm heapify is doing an internal iteration starting from the bottom right node (the representation of the array, the elements in the largest index) to the root, then towards the left and climbed into the top level, and so on until reaching the root (as an array [0 .. N -1]). Therefore, the iteration is started from j = N / 2 and less one-one until it reaches j = 0. At the internal node, the examination is only done on the direct child node (not on the other levels below it). At the time iteration in a higher level, is always already formed subtreesubtree heap. Thus, the case will flow towards the bottom node. Thus, this version heapify do as much as N / 2 times iteration, and in the worst case would do as much iteration log (N) times.

Algorithm Remove
This algorithm remove the root switch (which contains the maximum value) of the heap with the last element. Logically, the nodes that are most kanabawah transferred to the roots to replace the root node to be taken.

Algorithm Reheapify
Reheapify algorithm is doing a remake the heap from top to bottom as well as the last iteration of the algorithm heapify methods. The difference between the methods heapify method on iteration reheapify by both the algorithm. The algorithm method was only doing reheapify last iteration of the algorithm heapify. This is because both the left subtree and right subtree is a heap, so no need for such a complete iteration algorithm heapify. And after reheapify the node that will be reduced next diiterasikan one.

Characteristics of the heap sorting algorithm is that the sort heap sort implementation using heap tree can be solved in order to heap sort. Therefore, to implement the sort heap sorting algorithm in an application program requires a dynamic allocation using a tree data structure (tree). The basic principles of tree data structure used to realize the heap tree is as follows:
a. Nodes are interconnected by using a pointer.
In this tree data structure is used at least two pointers in each node, each branch to point to the left and right branches of the tree. For example in C language, tree data structure is declared as follows:
Class BinaryTreeSimpul (
keyType key;
infoType info;
BinaryTreeSimpul Left,
Right; / / methods

b. Left and Right value NULL if no more branches in the corresponding direction.

c. The structure of the binary tree, including the relationships between nodes, are explicitly represented by the Left and Right. If required search up (backtrack), then it can be done with a scan of the root, the use of algorithms that are recursive, or use of the stack.

d. Another alternative is to add a pointer to the parent.
However, this will result in increasing the number of stages in the processes of addition / removal of nodes

COMPARISON WITH OTHER sorting algorithm
Heapsort almost equivalent to a quick sort, other data sorting algorithm based on a highly efficient comparison. Quick sort a bit faster, because the cache and other factors, but in the worst case
complexity O (n), which is very slow for data that are very large. Then because the heap sort has (N log N) is a system that requires strict security usually wear a heap sort algorithm pengurutannya. Heap sort is often compared with merge sort, which mempunyaikompleksitas the same algorithm, but its space complexity (n) larger than the heap sort. Heap sort is also faster on the machine data dengancache small or slow.

Taking advantage of the tree data structure, we can get the data sorting algorithm mangkus which can be used to build a good application programs. Sort heap sorting algorithm can be incorporated into the divide and conquer algorithm that caused the division performed by first applying the algorithm as an initialization heapify method to transform a tree into the heap tree, and at every stage of the algorithm is applied reheapify method to reorder the heap tree


Related Posts Plugin for WordPress, Blogger...

Mobi Info


Lazada Promo