Download presentation
Presentation is loading. Please wait.
Published byJulie Long Modified over 9 years ago
1
Better way for sorting – heap sort 401410013,401410082, Department of Computer Science and Information Engineering, Chung Cheng University, Chayi, Taiwan 19081 Introduction Heap sort is one of the common sorting algorithms that performs in O(n*log(n)) in the worst case. It can be viewed as a progress of selection sort. Like selection sort, it separates its inputs into a sorted and an unsorted region, and it compresses the unsorted region by picking the smallest component and moving that to the sorted region. Heap sort is an in-place algorithm ; however, it is not a stable sort. Using a heap data structure extremely improves the search time for finding the minimum or maximum.A heap is a complete binary tree,that all its levels are full, except the last nodes. If it is a max heap, all its parents are greater than their children ; otherwise, it is a min heap. Materials and methods Assume that we sort integers in E[n] with n elements and output a max heap. We assign the data to a complete binary tree A in order. Heapify Build heap Heap sort Conclusions Heap sort is not only improve selection sort but also perform better than other sorts in worst case. Although it is an unstable sort, it is still more convenient for dealing with big data. Literature cited Williams, J. W. J. (1964), Algorithm 232 - Heapsort, Communications of the ACM 7 (6): 347–348 Floyd, Robert W. (1964), Algorithm 245 - Treesort 3, Communications of the ACM 7 (12): 701, doi:10.1145/355588.365103 Knuth, Donald(1997), "§5.2.3, Sorting by Selection", Sorting and Searching, The Art of Computer Programming 3 (third ed.), Addison-Wesley, pp. 144– 155, ISBN 0-201-89685-0 Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0- 262-03293-7. Chapters 6 and 7 Respectively: Heapsort and Priority Queues Acknowledgments We thank for embedded system laboratory assistance, Selina Lin for advices, Allen Chen for help, and Jovy Cheng for concerned. Funding for this project was provided by the Chung Cheng University Department of Computer Science and Information Engineering, a summer stipend, and my professor. For further information Please contact abc123@gmail.com.More information on this and related projects can be obtained at http://embedded.cs.ccu.edu.tw/ Figure 1. Photograph of a max heap. Figure 2. Photograph of a min heap. Results In the first place, we built a heap from data. The heap was built in an array that its output was a complete binary tree. Each element represented a node. Parent node could have at most two child nodes : left-child node and right-child node ; each node could extend to left child branch and right child branch. First Index in array stored in root node, and i represented current node. Followed the rules, we could express the nodes as: In the second place, we removed the largest index (the root)from the heap and inserted it into array repeatedly. Each move maintained and updated the heap. When we removed all indexes from the heap, we could obtain a sorted array. We divided array into two parts : sorted array and heap. The way to store heap in an array is showed in figure3. Heapify(A, parnet) { left_child <- left(parent) ri_child <- right(parent) if (left_child A[parent]) largest <- le_child else largest <- parent if (ri_child A[largest]) largest <- ri_child if (largest != parent) { exchange A[i] A[largest] Heapify(A, largest) } BuildHeap(A) { heapsize <- length(A) for i <- floor( length/2 ) downto 1 Heapify(A, i) } Heapsort(A) { BuildHeap(A) for i <- length(A) downto 2 { exchange A[1] A[i] heapsize <- heapsize -1 Heapify(A, 1) } iParent = floor((i-1) / 2) iLeftChild = 2*i + 1 iRightChild = 2*i + 2 Figure 3. Photograph of a heap in an array. Example Original array: [10,20,40,60,30,50] 1.Build heap 2.Sort Exchange 40 and 50 to maintain complete binary tree. Afterwards, exchange 10 and 50. Repeating this process, you will got a sorted array in the end Figure 4. Photograph of a heap in an array and its complete binary tree form. Figure 5. Photograph of exchange 60 and 40. Figure 6. Photograph of exchange 40 and 50. Afterwards, we exchange 40 and 50 to maintenance complete binary tree. Figure 6. Photograph of a sorted array. sortWorst case Average time stableExtra space Bubble sortO(n 2 ) stable O(1) Selection sort O(n 2 ) unstable O(1) Insertion sort O(n 2 ) stable O(1) Quick sortO(n 2 )O(nlog 2 n) unstable O(n)~ O(log n) Heap sortO(nlog 2 n) unstable O(1) Merge sortO(nlog 2 n) stable O(n) Figure1. The comparison with other sorts.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.