Download 1 - postech

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Quadtree wikipedia , lookup

B-tree wikipedia , lookup

Binary search tree wikipedia , lookup

Array data structure wikipedia , lookup

Transcript
CSED233: Data Structures (2014F)
Lecture16: Heap Sort
Bohyung Han
CSE, POSTECH
[email protected]
Adding a Node to (Min) Heap
Takes 𝑶 log 𝒏 steps when there are 𝒏 nodes.
• Step 1: Add node at the end
• Step 2: Make swap if parent is
bigger
• Step 3: Continue to swap if
parent is bigger
2
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Delete a Node from (Min) Heap
Takes 𝑶 log 𝒏 steps when there are 𝒏 nodes.
• Step 1: Remove root node
• Step 2: Move last node to root
• Step 3: Make swap if child is
smaller
• Step 4: Make swap if child is
smaller
3
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Priority Queue Sort
• We can use a priority queue to sort a set of elements.
 Insert the elements one by one with a series of insert operations.
 Remove the elements in sorted order with a series of deleteMax
operations.
Algorithm PQ‐Sort(S, C)
Input sequence S, comparator C for the elements of S
Output sequence S sorted in increasing order according to C
P  priority queue with comparator C
while  S.isEmpty ()
e  S.removeFirst ()
P.insert (e)
while  P.isEmpty()
e  P.removeMax().getKey()
S.addLast(e)
4
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Priority Queue Sort using a Heap
public void heapSort(int[] A)
{
Heap H = new Heap();
for (int i = 0; i < A.length; i++)
H.insert(A[i]);
𝑂 𝑛 log 𝑛
for (int i = 0; i < A.length; i++)
A[i] = H.deleteMax();
𝑂 𝑛 log 𝑛
}
5
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Convert an unordered array into a heap
6
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Shift up from the root if necessary
7
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
8
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
9
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
10
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
11
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
12
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
13
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
14
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
15
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
16
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
17
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
18
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
19
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
20
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
21
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Heapifying of an Array
• Continue to shift up until the last node
22
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort
• Given: an unsorted array
• Objective: sort the array
 Make unsorted array a heap
 Easy to find the maximum (minimum) element in heap
 Discard the maximum (minimum) element, easy to find the maximum
(minimum) element in the remaining heap
 Repeat until all elements are sorted.
• In‐place algorithm
23
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort Procedure
• Step 1: Make array a heap.
24
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort Procedure
• Step 1: Make array a heap.
25
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort Procedure
• Step 2: Swap the maximum and last number in the heap.
26
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort Procedure
• Step 3: Discard the last item in the heap and heapify the
remaining array.
27
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort Procedure
• Step 2: Swap the maximum and last number in the heap.
28
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort Procedure
• Step 3: Discard the last item in the heap and heapify the
remaining array.
29
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort Procedure
• Repeat swap and heapify operations.
30
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort Procedure
• Repeat swap and heapify operations.
31
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Heap‐Sort
• Consider a priority queue with 𝑛 items implemented by
means of a heap
 Space complexity: 𝑂 𝑛
 Methods insert and deleteMax: 𝑂 log 𝑛
 Methods size, isEmpty, and max: 𝑂 1
• Using a heap‐based priority queue,
 We can sort a sequence of 𝑛 elements 𝑂 𝑛 log 𝑛 time.
 The resulting algorithm is called heap‐sort.
 Heap‐sort is much faster than quadratic sorting algorithms, such as
insertion‐sort and selection‐sort.
32
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
A Faster Heap‐Sort
• Time complexity of insertion
 Insert 𝑛 keys one by one taking 𝑂 𝑛 log 𝑛 times.
 If we know all keys in advance, we can save the construction to 𝑂 𝑛
times by bottom up construction
• Bottom‐up construction
 We can construct a heap storing 𝑛 given keys log 𝑛 with phases.
 In phase 𝑖, a pair of heaps with 2𝑖 − 1 keys are merged into a heap
with 2𝑖+1 − 1 keys.
2i -1
2i+1-1
2i -1
33
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Merging Two Heaps
• Procedure
 Given two heaps and a key 𝑘, we create a new heap with the root nod
e storing 𝑘 and with the two heaps as subtrees.
 We perform downheap to restore the heap-order property
34
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Analysis of Merging
• Shift‐down with a proxy path
 Goes right first and then repeatedly goes left until the leaf of the heap
 Note: This path may differ from the actual shift‐down path.
 Each edge is traversed by at most one proxy path:
• Time complexity
 Successive insertions: 𝑂 𝑛 log 𝑛
 Bottom‐up construction: 𝑂 𝑛
35
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Time Complexity Comparison
• Comparison to other sorting algorithms
36
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
37