An efficient sorting algorithm — Ultimate Heapsort(UHS), Exercises of Computer science

Ultimate heap sorting is a comparison-basedsorting algorithm. Its best, worst, and average running time are O(n ∗ logn). Heapsort is anin-place ...

Typology: Exercises

2022/2023

Uploaded on 03/01/2023

lana87
lana87 🇺🇸

4.4

(18)

317 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHUANGXINBAN JOURNAL OF COMPUTING, MAR 2018 1
An efficient sorting algorithm Ultimate
Heapsort(UHS)
Feiyang Chen, Nan Chen, Hanyang Mao, Hanlin Hu
Abstract—Motivated by the development of computer theory, sorting algorithm is emerging in an endless stream. Inspired by decrease
and conquer method, we propose a brand new sorting algorithmUltimately Heapsort. The algorithm consists of two parts: building a
heap and adjusting a heap. Through the asymptotic analysis and experimental analysis of the algorithm, the time complexity of our
algorithm can reach O(nlogn)under any condition.Moreover, its space complexity is only O(1). It can be seen that our algorithm is
superior to all previous algorithms.
Index Terms—Sort algorithm, Min-heap, Max-heap, Asymptotic analysis, Experimental analysis
F
1 INTRODUCTION
SORT ING algorithm is one of the most important re-
search areas in computer science. It has a wide range
of applications in computer graphics, computer-aided de-
sign, robotics, pattern recognition, and statistics. At present,
quicksort is generally considered as the best choice in
practical sorting applications. However, when we need to
dynamically add and delete data during the sorting, we
find that the quicksort can’t exert its performance. There-
fore, we design an efficient sorting algorithm—the ultimate
heapsort(UHS), which offers us a better solution to this type
of problem.
2 ULTI MATE HEAPSORT(UHS)
2.1 Preliminary
We use heap data structure to implement our ultimate
heapsort algorithm. The heap is a complete binary tree, and
it is divided into a max-heap and a min-heap. The max-heap
requires that the value of the parent node is greater than or
equal to the value of the child node, and the min-heap is the
opposite. According to the characteristic of the max-heap,
we can know that the maximum value must be at the top
of the heap, that is, the root node. With this, we can build
an array into a max-heap. Here we take the max-heap as an
example. The min-heap is similar. For our UHS algorithm,
we use max-heaps.
2.2 Main idea
Here we will describe our UHS algorithm’s main idea in
detail. Firstly, we build the array as a max-heap, which is
the initial heap. At this point we know that the top element
of the heap is the maximum, that is, the first element of the
array is the maximum value. Secondly, we swap the first
element of the array with the last one, then the last one will
be the maximum value, and then we update the heap with
the last element removed, ensuring that the first one is at
its maximum in the new heap. Finally, we repeat the above
operation until only one element left.
2.3 Design of the UHS
2.3.1 MAX-HEAPLFY
In order to maintain the max-heap property, we call the
procedure MAX-HEAPIFY. Its inputs are an array Aand an
index iinto the array. When it is called, MAX- HEAPIFY
assumes that the binary trees rooted at LEF T (i)and
RIGH T (i)are max- heaps, but that A[i] might be smaller
than its children, thus violating the max-heap property.
MAX-HEAPIFY lets the value at A[i]”float down” in the
max-heap so that the subtree rooted at index iobeys the
max-heap property.
At each step of MAX-HEAPLFY, the largest of the ele-
ments A[i],A[LEF T (i)], and A[RI GHT (i)] is determined,
and its index is stored in largest. IfA[i]is largest, then the
subtree rooted at node iis already a max-heap and the pro-
cedure terminates. Otherwise, one of the two children has
the largest element, and A[i]is swapped with A[largest],
which causes node iand its children to satisfy the max-
heap property. The node indexed by largest, however, now
has the original value A[i], and thus the subtree rooted at
largest might violate the max-heap property. Consequently,
we call MAX-HEAPIFY recursively on that subtree.
2.3.2 Building a heap
We can use the procedure MAX-HEAPIFY in a bottom-up
manner to convert an array A[1..n], where n=A.length,
arXiv:1902.00257v1 [cs.DS] 1 Feb 2019
pf3

Partial preview of the text

Download An efficient sorting algorithm — Ultimate Heapsort(UHS) and more Exercises Computer science in PDF only on Docsity!

An efficient sorting algorithm — Ultimate

Heapsort(UHS)

Feiyang Chen, Nan Chen, Hanyang Mao, Hanlin Hu

Abstract —Motivated by the development of computer theory, sorting algorithm is emerging in an endless stream. Inspired by decrease and conquer method, we propose a brand new sorting algorithmUltimately Heapsort. The algorithm consists of two parts: building a heap and adjusting a heap. Through the asymptotic analysis and experimental analysis of the algorithm, the time complexity of our algorithm can reach O(nlogn) under any condition.Moreover, its space complexity is only O(1). It can be seen that our algorithm is superior to all previous algorithms.

Index Terms —Sort algorithm, Min-heap, Max-heap, Asymptotic analysis, Experimental analysis

F

1 INTRODUCTION

S

ORTING algorithm is one of the most important re- search areas in computer science. It has a wide range of applications in computer graphics, computer-aided de- sign, robotics, pattern recognition, and statistics. At present, quicksort is generally considered as the best choice in practical sorting applications. However, when we need to dynamically add and delete data during the sorting, we find that the quicksort can’t exert its performance. There- fore, we design an efficient sorting algorithm—the ultimate heapsort(UHS), which offers us a better solution to this type of problem.

2 ULTIMATE HEAPSORT(UHS)

2.1 Preliminary We use heap data structure to implement our ultimate heapsort algorithm. The heap is a complete binary tree, and it is divided into a max-heap and a min-heap. The max-heap requires that the value of the parent node is greater than or equal to the value of the child node, and the min-heap is the opposite. According to the characteristic of the max-heap, we can know that the maximum value must be at the top of the heap, that is, the root node. With this, we can build an array into a max-heap. Here we take the max-heap as an example. The min-heap is similar. For our UHS algorithm, we use max-heaps.

2.2 Main idea Here we will describe our UHS algorithm’s main idea in detail. Firstly, we build the array as a max-heap, which is the initial heap. At this point we know that the top element of the heap is the maximum, that is, the first element of the array is the maximum value. Secondly, we swap the first element of the array with the last one, then the last one will be the maximum value, and then we update the heap with the last element removed, ensuring that the first one is at its maximum in the new heap. Finally, we repeat the above operation until only one element left.

2.3 Design of the UHS 2.3.1 MAX-HEAPLFY In order to maintain the max-heap property, we call the procedure MAX-HEAPIFY. Its inputs are an array A and an index i into the array. When it is called, MAX- HEAPIFY assumes that the binary trees rooted at LEF T (i) and RIGHT (i) are max- heaps, but that A[i] might be smaller than its children, thus violating the max-heap property. MAX-HEAPIFY lets the value at A[i] ”float down” in the max-heap so that the subtree rooted at index i obeys the max-heap property.

At each step of MAX-HEAPLFY, the largest of the ele- ments A[i], A[LEF T (i)], and A[RIGHT (i)] is determined, and its index is stored in largest. IfA[i]is largest, then the subtree rooted at node i is already a max-heap and the pro- cedure terminates. Otherwise, one of the two children has the largest element, and A[i] is swapped with A[largest], which causes node i and its children to satisfy the max- heap property. The node indexed by largest, however, now has the original value A[i], and thus the subtree rooted at largest might violate the max-heap property. Consequently, we call MAX-HEAPIFY recursively on that subtree.

2.3.2 Building a heap We can use the procedure MAX-HEAPIFY in a bottom-up manner to convert an array A[1..n], where n = A.length,

arXiv:1902.00257v1 [cs.DS] 1 Feb 2019

into a max-heap. The elements in the subarray A((

n 2

1)..n) are all leaves of the tree, and so each is a 1 -element heap to begin with. The procedure BUILD-MAX-HEAP goes through the remaining nodes of the tree and runs MAX- HEAPIFY on each one.

2.3.3 Implementation

The heapsort algorithm starts by using BUILD-MAX-HEAP to build a max-heap on the input array A[1..n], where n = A.length. Since the maximum element of the array is stored at the root A[1], we can put it into its correct final position by exchanging it with A[n]. If we now discard node n from the heap-and we can do so by simply decrementing A.heap-size-we observe that the children of the root remain max-heaps, but the new root element might violate the max-heap property. All we need to do to restore the max- heap property, however, is call MAX-HEAPIFY(A, 1 ), which leaves a max-heap in A[1..n]. The heapsort algorithm then repeats this process for the max-heap of size n − 1 down to a heap of size 2.

3 ANALYSIS

3.1 Running time analysis

We have used mathematical methods to analyze the running time of UHS. The maximum number of nodes in each layer during the build process is n1 = ceil(n/(2h+1)), where n and h represent the number of nodes and the number of layers in the heap, respectively.

∑^ ∞

h=

x − 1

∑^ ∞

h=

h ∗ xh−^1 ∗ x^2 =

(1 − x)^2

∗ x^2 (2)

∑^ ∞

h=

h 2 h+^

logn∑

h=

n 1 ∗ O(h) = O(n ∗

h∑=

logn

h 2 h+^ ) = O(n) (4)

Since the running time of resuming the heap is O(logN ) each time, a total of N − 1 times to restore the heap operation, plus the N/ 2 times downward adjustment when the previous stack was built, the running time of each ad- justment time is also O(logN ). The sum of the two operation times is also O(N ∗ logN ). Therefore, the time complexity of heap sorting is O(N ∗ logN )

TABLE 1 Running time analyze of 7 algorithms

Worst-case Average-case/expected Algorithm running time running time Insertion sort Θ(n^2 ) Θ(n^2 ) Merge sort Θ(nlogn) Θ(nlogn) Quicksort Θ(n^2 ) Θ(nlogn) (expected) Bucket sort Θ(n^2 ) Θ(n) (average-case) Radix sort Θ(d(n + k)) Θ(d(n + k)) Bubble sort Θ(n^2 ) Θ(n^2 ) Heapsort Θ(nlogn) Θ(nlogn)

For the commonly used seven sorting algorithms, we have analyzed and compared the running time, including the worst-running time and expected running time. The comparison results are shown in the table 1.

3.2 Space complexity analysis At the same time, we have also analyzed the space complex- ity of these seven sorting methods.

TABLE 2 Space complexity analysis of 7 algorithms

Algorithm Space complexty Insertion sort O(1) Merge sort O(n) Quicksort O(nlogn) Bucket sort O(n) Radix sort O(n + k) Bubble sort O(1) Heapsort O(1)

Table 2 shows the space complexity for each methods. Heap sort is in-sequence sorting algorithms, which means only a constant additional memory space is required in addition to the input array. Thus, UHS? performance was one of the best among all algorithms. The results suggested that UHS can increase the sorting speed without taking up more space.

3.3 Stability analysis The structure of the heap is that the children of node i are nodes 2 ∗i and 2 ∗i+1. The max heap requires that the parent node is greater than or equal to its 2 child nodes, and the min heap requires that the parent node is less than or equal to its 2 child nodes. In a sequence of length n, the process of heap sorting is to choose the largest (max heap) or smallest (min heap) values from the first n/ 2 and the total of 3 values of its sub-nodes. The choice between these 3 elements won?t affect the stability. However, when elements are selected for the parent nodes n/ 2 − 1 , n/ 2 − 2 , ... 1 , stability is destroyed. It is possible that the n/ 2 th parent node exchange swaps the next one, and the n/ 2 − 1 th parent does not swap the