<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE article  PUBLIC "-//NLM//DTD Journal Publishing DTD v3.0 20080202//EN" "http://dtd.nlm.nih.gov/publishing/3.0/journalpublishing3.dtd"><article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="3.0" xml:lang="en" article-type="research article"><front><journal-meta><journal-id journal-id-type="publisher-id">JCC</journal-id><journal-title-group><journal-title>Journal of Computer and Communications</journal-title></journal-title-group><issn pub-type="epub">2327-5219</issn><publisher><publisher-name>Scientific Research Publishing</publisher-name></publisher></journal-meta><article-meta><article-id pub-id-type="doi">10.4236/jcc.2017.512006</article-id><article-id pub-id-type="publisher-id">JCC-79952</article-id><article-categories><subj-group subj-group-type="heading"><subject>Articles</subject></subj-group><subj-group subj-group-type="Discipline-v2"><subject>Computer Science&amp;Communications</subject></subj-group></article-categories><title-group><article-title>
 
 
  Heap Sorting Based on Array Sorting
 
</article-title></title-group><contrib-group><contrib contrib-type="author" xlink:type="simple"><name name-style="western"><surname>Haiming</surname><given-names>Li</given-names></name><xref ref-type="aff" rid="aff1"><sup>1</sup></xref><xref ref-type="corresp" rid="cor1"><sup>*</sup></xref></contrib><contrib contrib-type="author" xlink:type="simple"><name name-style="western"><surname>Ping</surname><given-names>Chen</given-names></name><xref ref-type="aff" rid="aff1"><sup>1</sup></xref></contrib><contrib contrib-type="author" xlink:type="simple"><name name-style="western"><surname>Yong</surname><given-names>Wang</given-names></name><xref ref-type="aff" rid="aff1"><sup>1</sup></xref></contrib></contrib-group><aff id="aff1"><addr-line>School of Computer and Information Engineering, Shanghai University of Electric Power, Shanghai, China</addr-line></aff><author-notes><corresp id="cor1">* E-mail:<email>zjxulhm@163.com(HL)</email>;</corresp></author-notes><pub-date pub-type="epub"><day>30</day><month>09</month><year>2017</year></pub-date><volume>05</volume><issue>12</issue><fpage>57</fpage><lpage>62</lpage><history><date date-type="received"><day>10,</day>	<month>October</month>	<year>2017</year></date><date date-type="rev-recd"><day>27,</day>	<month>October</month>	<year>2017</year>	</date><date date-type="accepted"><day>30,</day>	<month>October</month>	<year>2017</year></date></history><permissions><copyright-statement>&#169; Copyright  2014 by authors and Scientific Research Publishing Inc. </copyright-statement><copyright-year>2014</copyright-year><license><license-p>This work is licensed under the Creative Commons Attribution International License (CC BY). http://creativecommons.org/licenses/by/4.0/</license-p></license></permissions><abstract><p>
 
 
  A kind of heap sorting method based on array sorting was proposed. Some advantages and disadvantages of it were discussed. It was compared with the traditional method of direct application. In the method, the ordered keywords in the array are put into the heap one by one after building an empty heap. This method needs relatively less space and is fit for ordered sequence.
 
</p></abstract><kwd-group><kwd>Heap Sort</kwd><kwd> Array</kwd><kwd> Bottom-Up</kwd><kwd> Algorithm</kwd></kwd-group></article-meta></front><body><sec id="s1"><title>1. Introduction</title><p>In the field of computer algorithm design, sorting algorithm is one of the important methods which is used to process date. Heap sort algorithm’s time complexity is relatively low [<xref ref-type="bibr" rid="scirp.79952-ref1">1</xref>] [<xref ref-type="bibr" rid="scirp.79952-ref2">2</xref>] [<xref ref-type="bibr" rid="scirp.79952-ref3">3</xref>] [<xref ref-type="bibr" rid="scirp.79952-ref4">4</xref>] . So if we can understand its thought very well and use it flexibly, we will solve many problems in our life.</p><p>In computer science, heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, we can quickly locate the elements of the required index by using the characteristics of the array [<xref ref-type="bibr" rid="scirp.79952-ref5">5</xref>] [<xref ref-type="bibr" rid="scirp.79952-ref6">6</xref>] . Heap is a completely two binary tree that in ordered set, satisfies the following properties of the heap that the max key value of the key elements of every node in heaps is no more bigger than it in parent node (just in terms of maximum heap terms). Therefore, the largest element of the heap is stored in the root node (the minimum heap similarly, no longer). When we realize the stack with an array of H[1∙∙∙N], according to the order of top-down and from left to right in turns, we can store the elements of the heap in the array elements of H[<xref ref-type="bibr" rid="scirp.79952-ref1">1</xref>], H[<xref ref-type="bibr" rid="scirp.79952-ref2">2</xref>], ∙∙∙, H[n]. In general, the left-son node element of heap element of H[i] is H[2i]; the right-son node element is H[2i + 1]; the parent node element is H[[i2]]; then heap properties can be expressed as: H[[i2]] = H[i], I = 2 ~ n.</p><p>Heapsort was invented by J. W. J. Williams in 1964 [<xref ref-type="bibr" rid="scirp.79952-ref7">7</xref>] [<xref ref-type="bibr" rid="scirp.79952-ref8">8</xref>] . 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 reconstruction 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.</p><p>It is easy to find that there is a large amount of calculation in the process of reconstructing 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 [<xref ref-type="bibr" rid="scirp.79952-ref1">1</xref>] [<xref ref-type="bibr" rid="scirp.79952-ref2">2</xref>] makes the node elements along anyone 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).</p><p>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 rebuilt, the Heapad just is invoked n − 1 times. The total numbers of comparison should be no more than</p><p>t ( n ) = ∑ i = 2 n − 1 [ log 2 i ] = 2 &#215; [ 1 &#215; 2 + 2 &#215; 2 2 + 3 &#215; 2 3 + ⋯ + ( h − 1 ) &#215; 2 h − 1 + h &#215; ( n − 2 h ) ] = 2 n h − 2 h + 2 + 4 (1)</p><p>Therefore, the worst case is that we should do 2nlogn + O(n) comparison and nlogn O(n) times elements moving.</p><p>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 Edition). It improves the traditional sorting algorithm and reduces the complexity of the algorithm.</p></sec><sec id="s2"><title>2. Reference Knowledge</title><p>1) Heap: it can be defined as a two binary tree where each node has one key. There are some requirements:</p><p>a) The shape of a tree: every layer of the tree is full except the rightmost element on the last floor.</p><p>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).</p><p>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 <xref ref-type="fig" rid="fig1">Figure 1</xref>.</p><p>a) Large root heap sequences: (96, 83, 27, 38, 11, 09)</p><p>b) Small heap sequence: (12, 36, 24, 85, 47, 30, 53, 91)</p><p>Be careful:</p><p>1) Any sub tree in a heap is also a heap.</p><p>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.</p><p>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 minimum) keyword in the current unsorted state by the relationship between the parent node and child node in two binary tree algorithm (according to the sequence 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 maximum (or minimum) keyword in the unsorted state currently. This process is simpler.</p></sec><sec id="s3"><title>3. A Method of Classical Heap Construction-Bottom-Up Construction Reactor</title><p>In the initialization of a completely two forks tree which contains several nodes, the key is placed in the given order, and then heap the tree. The process is as follows: from the last parent node to the root node, checking the key whether meet the requirements. If it doesn’t meet the heap’s characteristic, we should exchange the position of the biggest key of its child nodes and the key value of the node. We repeat the same process for remaining element until to meet the</p><p>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.</p><p>Description of Bottom-up build heap algorithm:</p><p>method HeapBottomUp(H[1..n])</p><p>//Construct a heap from a known array by a bottom-up algorithm</p><p>//Input: a known arrayH[1..n]</p><p>//Output: a heapH[1..n]</p><p>for i←n/2 downto 1 do</p><p>k←i;v←H[k]</p><p>heap←false</p><p>while not heap and 2*k≤n do</p><p>j←2*k</p><p>if j</p><p>if H[j]</p><p>if v≥H[j]</p><p>heap←true</p><p>else H[k]←H[j];k←j</p><p>H[k]←v</p></sec><sec id="s4"><title>4. Array Build Heap</title><p>In the first step, the heap is built according to the given order in the classical stack construction method. In the second step, the parents and children nodes are exchanged until to meet the heap’s characteristic. Therefore there is some thought. Firstly, we can build the empty heap. Secondly, these key values which given by an array of known sequence stored in the array from large to small (or large) are arranged in the array. Thirdly, we insert the sorted sequence into a heap directly one by one. So you don’t have to adjust the key node in the heap.</p><p>The specific procession is that: first, constructing an empty two binary tree. Second, all the key value of nodes that removed are stored in an array. Then, these key are ranked with quick sorting according to the order from large to small (maximum heap).Besides, the one at the head is the parent node and the one at the back is the child node. Next, the values are sorted in turn into two binary tree according to the order of the top-down.</p><p>Description of array build heap algorithm:</p><p>method sort Byarray(H[1..n])</p><p>//A heap is constructed by array sorting</p><p>//Input: a known arrayH[1..n]</p><p>//Output: a heapH[1..n]</p><p>for i←n-1 downto 1 do</p><p>max←i</p><p>for j←n downto i+2 do</p><p>if H[j]&gt;H[max] max←j</p><p>swap H[i] and H[max]</p><p>for i←1 downto n do</p><p>heap←H[i]</p></sec><sec id="s5"><title>5. Algorithm Comparison between Classical Heap Construction Method and Array Heap Build Method</title><p>1) Time complexity:</p><p>Classical algorithm:</p><p>Time needed to build a heap: O(n)</p><p>The time needed to take the top element and adjustment: O(nlog(2<sup>n</sup>))</p><p>Several elements need to repeat this action several times, and the time each action required is related to height. However, height can be considered invariable by rough calculations, so that the time complexity is: nlog(2^n)。</p><p>Worst case: Mean complexity</p><p>Best case: O(1)</p><p>Array algorithm:</p><p>In an ideal situation, every time the array to be sorted will be divided into two parts as long as each other, and it needs log(2,n) times division. In the worst case that an array has ordered or roughly ordered, only one element of each partitioning can be divided every time. The lower bound of time complexity is O(nlog(2,n) and the worst case is O(n^2). In actually, the average value of time complexity is O(nlog(2,n).</p><p>2) Space complexity:</p><p>Classical algorithm: O(1)</p><p>Array algorithm:</p><p>Best case: O(log(2,n)</p><p>Worst case: O(n)</p><p>3) Stability:</p><p>The stability of two methods is caused by the disagreement of its relationship between father and son nodes with its subscript rules. But the stability can be promised if exchanging their conditions which are comprised. Besides, the two elements which have the same keyword may be belong to different parent nodes, so stability is not confirmed.</p></sec><sec id="s6"><title>6. Conclusion</title><p>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 algorithm is especially suitable for the realization of priority queue.</p></sec><sec id="s7"><title>Acknowledgements</title><p>This work is supported by Shanghai University of Electric Power Smart Grid Collaborative Innovation Project (A-0009-17-002-05) and Shanghai Science and Technology Innovation Fund for Small and Medium Enterprises (1601H1E2600).</p></sec><sec id="s8"><title>Cite this paper</title><p>Li, H.M., Chen, P. and Wang, Y. (2017) Heap Sorting Based on Array Sorting. Journal of Computer and Communications, 5, 57-62. https://doi.org/10.4236/jcc.2017.512006</p></sec></body><back><ref-list><title>References</title><ref id="scirp.79952-ref1"><label>1</label><mixed-citation publication-type="other" xlink:type="simple">Pazy, A. (1983) Semigroups of Linear Operators and Applications to Partial Differential Equations. Springer-Verlag, Berlin. https://doi.org/10.1007/978-1-4612-5561-1</mixed-citation></ref><ref id="scirp.79952-ref2"><label>2</label><mixed-citation publication-type="journal" xlink:type="simple"><name name-style="western"><surname>Huo</surname><given-names> H.W. </given-names></name>,<etal>et al</etal>. (<year>2002</year>)<article-title>Research of Fast Sorting Algorithm</article-title><source> Microelectronic and Computers</source><volume> 19</volume>,<fpage> 6</fpage>-<lpage>9</lpage>.<pub-id pub-id-type="doi"></pub-id></mixed-citation></ref><ref id="scirp.79952-ref3"><label>3</label><mixed-citation publication-type="journal" xlink:type="simple"><name name-style="western"><surname>Wu</surname><given-names> S.Z. </given-names></name>,<etal>et al</etal>. (<year>2002</year>)<article-title>The Analysis of Improved Heap Sorting Algorithm and Its Complexity</article-title><source> Journal of Northwest Normal University (Natural Science Edition)</source><volume> 38</volume>,<fpage> 24</fpage>-<lpage>26</lpage>.<pub-id pub-id-type="doi"></pub-id></mixed-citation></ref><ref id="scirp.79952-ref4"><label>4</label><mixed-citation publication-type="journal" xlink:type="simple"><name name-style="western"><surname>Liu</surname><given-names> M.Q. </given-names></name>,<etal>et al</etal>. (<year>2012</year>)<article-title>Study of Sorting Algorithm Time Complexity</article-title><source> Software Tribune</source><volume> 11</volume>,<fpage> 35</fpage>-<lpage>37</lpage>.<pub-id pub-id-type="doi"></pub-id></mixed-citation></ref><ref id="scirp.79952-ref5"><label>5</label><mixed-citation publication-type="other" xlink:type="simple">Cook, C. and Kim, D. (1980) Best Sorting Algorithm for Nearly Sorted Lists. CACM, 23, 620-626. https://doi.org/10.1145/359024.359026</mixed-citation></ref><ref id="scirp.79952-ref6"><label>6</label><mixed-citation publication-type="other" xlink:type="simple">Mehlhorn, K. (1984) Data Structures and Algorithms. Vol. 1, Springer-Verlag, Berlin.</mixed-citation></ref><ref id="scirp.79952-ref7"><label>7</label><mixed-citation publication-type="other" xlink:type="simple">Schaffer, R. and Sedgewick, R. (1991) The Analysis of Heapsort. Technical Report CS-TR-330-91, Princeton University, Princeton, NJ.</mixed-citation></ref><ref id="scirp.79952-ref8"><label>8</label><mixed-citation publication-type="other" xlink:type="simple">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</mixed-citation></ref></ref-list></back></article>