Heap Sorting Based On Array Sorting
Heap Sorting Based On Array Sorting
http://www.scirp.org/journal/jcc
ISSN Online: 2327-5227
ISSN Print: 2327-5219
School of Computer and Information Engineering, Shanghai University of Electric Power, Shanghai, China
Heapsort was invented by J. W. J. Williams in 1964 [7] [8]. This was also the
birth of the heap, presented already by Williams as a useful data structure in its
own right: in this algorithm, establishing the initial stack by O (n) time; then
continually exchanging the top element with the bottom element to reconstruc-
tion of the heap. Eventually, all the elements are in good order. In the same year,
R. W. Floyd published an improved version that could sort an array in-place,
continuing his earlier research into the treesort algorithm. It only needs one
record size of auxiliary space to use heap sort. And each record to be sorted only
takes up one storage space.
It is easy to find that there is a large amount of calculation in the process of re-
constructing the heap, and the efficiency of reconstruction depends on comparing
between the number of elements and moving of heap elements. The commonly
used reconstruction algorithm Heapify [1] [2] makes the node elements along an-
yone path down. Each layer needs to be compared for 2 times, and the left and
right son node elements compare for 1 time. Then the larger one does with the
parent node elements for another time. This process is repeated until the parent
element is no less than the son node elements (the maximum heap).
There is an example of depth h: Numbers of keywords comparison are at least
2 ( h − 1) times in the selection sort algorithm. When building the heap which
has n elements and h depth, the numbers should be less 4n for Formula (1). The
depth of a complete two fork tree is [ log 2 n ] + 1 . In the process of the heap re-
built, the Heapad just is invoked n − 1 times. The total numbers of comparison
should be no more than
n −1
t ( n ) = ∑ [ log 2 i ]
i =2
( )
=2 × 1 × 2 + 2 × 22 + 3 × 23 + + ( h − 1) × 2h−1 + h × n − 2h
(1)
= 2nh − 2h+2 + 4
Therefore, the worst case is that we should do 2nlogn + O(n) comparison and
nlogn O(n) times elements moving.
About the study of the heap sort, there are now many studies analyzing that
and putting forward the optimization plan of it based on different views for the
heap sort, such as some reference papers like Mr. Wu Shangzhi who published
the “heap sorting algorithm improvement and complexity analysis on the heap”
in 2002 at the Journal of Northwest Normal University (Natural Science Edi-
tion). It improves the traditional sorting algorithm and reduces the complexity
of the algorithm.
2. Reference Knowledge
1) Heap: it can be defined as a two binary tree where each node has one key.
There are some requirements:
a) The shape of a tree: every layer of the tree is full except the rightmost ele-
ment on the last floor.
b) Parent advantage (heap’s characteristic): the key of each node isn’t less
(more than in minimum heap) than its child’s key (for any leaf node, we think
this condition is automatically satisfied).
2) The large and small root heap: the key of root node (also known as the top
of the stack) in which heap is the largest of all node keyword and this heap called
root pile or maximum heap. Similarly, the minimum keyword root node in
which heap is called the small heap or the minimum heap. For example in Fig-
ure 1.
a) Large root heap sequences: (96, 83, 27, 38, 11, 09)
b) Small heap sequence: (12, 36, 24, 85, 47, 30, 53, 91)
Be careful:
1) Any sub tree in a heap is also a heap.
2) The heap discussed above is actually a two fork heap (Binary Heap). The K
fork heap can be defined like that. But it is not studied in this paper.
3) Heap sort is a tree selection sort algorithm. There are some characteristics:
in the sorting process, the H[l∙∙∙N] is regarded as a sequential storage structure
with a totally two fork tree. We can choose record of the maximum (or mini-
mum) keyword in the current unsorted state by the relationship between the
parent node and child node in two binary tree algorithm (according to the se-
quence storage structure of the two fork tree). Large root heap (or small root
heap) records maximum (or minimum) key, so the heapsort can get the maxi-
mum (or minimum) keyword in the unsorted state currently. This process is
simpler.
(a) (b)
requirements. For the subtree rooted at the current parents node, the algorithm
operate trend node of the node with the operation after complete the heaping.
After complete the operation of the tree node, the algorithm will be ended.
Description of Bottom-up build heap algorithm:
method HeapBottomUp(H[1..n])
//Construct a heap from a known array by a bottom-up algorithm
//Input: a known arrayH[1..n]
//Output: a heapH[1..n]
for i←n/2 downto 1 do
k←i;v←H[k]
heap←false
while not heap and 2*k≤n do
j←2*k
if j<n //There are two children
if H[j]<H[j+1]j←j+1
if v≥H[j]
heap←true
else H[k]←H[j];k←j
H[k]←v
6. Conclusion
This paper presents a heap sorting algorithm based on array and finishes the
comparison with the traditional method of direct application. In the larger array
sequence, the heap sorting algorithm is applied directly. Its time complexity is as
same as quick sort and merging sort. It can run with less storage space, so it’s fit
for ordered sequence sorting. The application of principle reflected that the al-
gorithm is especially suitable for the realization of priority queue.
Acknowledgements
This work is supported by Shanghai University of Electric Power Smart Grid
References
[1] Pazy, A. (1983) Semigroups of Linear Operators and Applications to Partial Diffe-
rential Equations. Springer-Verlag, Berlin.
https://doi.org/10.1007/978-1-4612-5561-1
[2] Huo, H.W. (2002) Research of Fast Sorting Algorithm. Microelectronic and Com-
puters, 19, 6-9.
[3] Wu, S.Z. (2002) The Analysis of Improved Heap Sorting Algorithm and Its Com-
plexity. Journal of Northwest Normal University (Natural Science Edition), 38,
24-26.
[4] Liu, M.Q. (2012) Study of Sorting Algorithm Time Complexity. Software Tribune,
11, 35-37.
[5] Cook, C. and Kim, D. (1980) Best Sorting Algorithm for Nearly Sorted Lists.
CACM, 23, 620-626. https://doi.org/10.1145/359024.359026
[6] Mehlhorn, K. (1984) Data Structures and Algorithms. Vol. 1, Springer-Verlag, Ber-
lin.
[7] Schaffer, R. and Sedgewick, R. (1991) The Analysis of Heapsort. Technical Report
CS-TR-330-91, Princeton University, Princeton, NJ.
[8] Hayward, R. and Mcdiarmid, C. (1991) Average Case Analysis of Heap Building by
Repeated Insertion. Journal of Algorithms, 12, 126-153.
https://doi.org/10.1016/0196-6774(91)90027-V