Ads Sorting Heaps
Ads Sorting Heaps
Ads Sorting Heaps
ADC/ADS
Rom Langerak
E-mail: r.langerak@utwente.nl
© GfH&JPK&RL
Sorting and Heaps ADC/ADS
• Insertion sort
• Quicksort
• Mergesort
Heaps:
• What is a heap?
• How to build a heap?
• Priority queues, heapsort
© GfH&JPK&RL 1
Sorting and Heaps ADC/ADS
You know selection and bubble sort; here we study other algorithms
© GfH&JPK&RL 2
Sorting and Heaps ADC/ADS
0 12 17 17 19 8 25 3 6 69 26 4 2 13 34 41
© GfH&JPK&RL 3
Sorting and Heaps ADC/ADS
def insertionSort(E):
for j in range(1,len(E)):
v=E[j]
i=j-1 #now insert v into sorted E[0,j]
while i>=0 and E[i]>v:
E[i+1]=E[i]
i=i-1
E[i+1]=v
© GfH&JPK&RL 4
Sorting and Heaps ADC/ADS
This yields:
Pn−1 n·(n−1)
W (n) = i=0 (i) = 2 ∈ Θ(n2)
© GfH&JPK&RL 5
Sorting and Heaps ADC/ADS
Pn−1
A(n) = i=0 Xi
where Xi = expected # comparisons to find slot for element i
© GfH&JPK&RL 6
Sorting and Heaps ADC/ADS
Quicksort – Strategy
41 26 17 25 19 17 8 3 6 69 12 4 2 13 34 0
pivot partition
8 3 6 12 4 2 13 0 17 41 26 17 69 25 19 34
• Partition array in two parts: (i) smaller than and (ii) at least the pivot
several partitioning strategies do exist
© GfH&JPK&RL 7
Sorting and Heaps ADC/ADS
Quicksort – Algorithm
def quickSort(E,left,right):
if right>left:
i=partition(E,left,right) #i is split point
quickSort(E,left,i-1) #sort left part
quicksort(E, i+1,right) #sort right part
© GfH&JPK&RL 8
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 9
Sorting and Heaps ADC/ADS
8 6 17 25 19 0 4 3 13 2 12 26 69 41 34 17
left bound right bound pivot
search
8 6 17 25 19 0 4 3 13 2 12 26 69 41 34 17
left bound right bound
< pivot > pivot
swap
8 6 12 25 19 0 4 3 13 2 17 26 69 41 34 17
left bound
< pivot right bound > pivot
search
© GfH&JPK&RL 10
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 11
Sorting and Heaps ADC/ADS
}
E[left,i]<pivot, E[i, right+1]>pivot
© GfH&JPK&RL 12
Sorting and Heaps ADC/ADS
Recursive calls require storage of all the left and right parameters
© GfH&JPK&RL 13
Sorting and Heaps ADC/ADS
• the splitting into the smaller and larger part is as unbalanced as possible
• one part is empty, whereas the other part contains the remaining elements
• this appears e.g., when the array is already ascending (or descending)!
• this yields n−1 levels in the recursion tree
Pn−1 n·(n−1)
This yields: W (n) = i=0 (i) = 2 ∈ Θ(n2)
© GfH&JPK&RL 14
Sorting and Heaps ADC/ADS
It appears that this also holds for the average case: A(n) ∈ Θ(n log n)
© GfH&JPK&RL 15
Sorting and Heaps ADC/ADS
Mergesort – Strategy
41 26 17 25 19 17 8 3 6 69 12 4 2 13 34 0
3 8 17 17 19 25 26 41 0 2 4 6 12 13 34 69
merge
0 2 3 4 6 8 12 13 17 17 19 25 26 34 41 69
© GfH&JPK&RL 16
Sorting and Heaps ADC/ADS
Mergesort – Algorithm
def mergeSort(E,left,right):
if right>left:
mid=(right+left)//2
mergeSort(E,left,mid)
mergeSort(E,mid+1,right)
merge(E,left,mid,right)
© GfH&JPK&RL 17
Sorting and Heaps ADC/ADS
Mergesort – Analysis
© GfH&JPK&RL 18
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 19
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 20
Sorting and Heaps ADC/ADS
Trees in arrays
16
16 14 10 8 7 9 3 2 4 1
0 1 2 3 4 5 6 7 8 9
14 10
8 7 9 3
2 4 1
© GfH&JPK&RL 21
Sorting and Heaps ADC/ADS
Heaps
Such a special binary tree is a heap if for the keys in the nodes:
• each node key exceeds that of all its children, or this is a maxheap
• the keys of all its children exceed the key of the node. this is a minheap
© GfH&JPK&RL 22
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 23
Sorting and Heaps ADC/ADS
Heapify – Strategy
Consider E[i] and assume its left and right subtrees are heaps
Construct a heap from the left- and right subtrees and E[i], let the value
of E[i] “float down” in the structure such that the structure rooted at E[i]
is a heap
© GfH&JPK&RL 24
Sorting and Heaps ADC/ADS
Heapify – Algorithm
def heapify(E,i):
left,right=2*i+1,2*i+2
if left<E.heapsize and E[left]>E[i]:
max=left
else:
max=i
if right<E.heapsize and E[right]>E[max]:
max=right # max is index of max{E[i],E[left],E[right]}
if max!=i: #so not a heap
E[i],E[max]=E[max],E[i]
heapify(E,max) # heapify subtree with root max
© GfH&JPK&RL 25
Sorting and Heaps ADC/ADS
Heapify – Example
0 4 0
Heapify(E,0) 16 Heapify(E,1)
Heap 1 16 1 10 2
10 2 Heap 4
3 4 5 3 4 5 6
7 9 6 3 14 7 9 3
14
9 7 8 9
7 8 2 1
2 8 1 8
0 0 Heap
16 16
Heapify(E,3)
1 1 10 2
14 10 2 14
3 5 6 3 4 5 6
4 4 7 9 3 8 7 9 3
7 8 7 8 9
9 2 4 1
2 8 1
© GfH&JPK&RL 26
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 27
Sorting and Heaps ADC/ADS
0 4 0 4 Heapify(E,3)
Heapify(E,4)
1 3 2 1 3 2
1 1
3 4 16 5 6 10 3 4 16 5 6
2 9 2 9 10
7 8 9 7 8 9
14 8 7 14 8 7
4 1 3 2 16 9 10 14 8 7
10 elements
© GfH&JPK&RL 28
Sorting and Heaps ADC/ADS
0 4 Heapify(E,2) 0 4 Heapify(E,1)
1 1 10 2
1 3 2 1
3 4 16 5 6 3 4 16 5 6 3
14 9 10 14 9
7 8 9 7 8 9
2 8 7 2 8 7
0 4
recursion 0 4 Heapify(E,0)
Heapify(E,4)
1 16 10 2 1 16 10 2
3 4 1 5 6 3 3 4 5 6 3
14 9 14 7 9
7 8 9 7 8 9
2 8 7 2 8 1
© GfH&JPK&RL 29
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 30
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 31
Sorting and Heaps ADC/ADS
Priority queues
Consider objects that are equipped with a key (or, priority)
© GfH&JPK&RL 32
Sorting and Heaps ADC/ADS
tail
0 12 0 12 3 7
17 4 head 17 4 head 17 4 head 0 12 head
12 8 12 8 12 8 12 8
tail
tail tail
3 7
© GfH&JPK&RL 33
Sorting and Heaps ADC/ADS
tail
0 12 12 8 12 8 head
12 8 head 12 8 head 3 7 head 3 7
17 4 17 4 17 4
tail
tail tail
0 12 0 12
© GfH&JPK&RL 34
Sorting and Heaps ADC/ADS
∗
this includes shifting all elements “to the right” of k
© GfH&JPK&RL 35
Sorting and Heaps ADC/ADS
Heapsort – Algorithm
def heapSort(E):
buildHeap(E)
for i in range(len(E)-1,0,-1):
E[0],E[i]=E[i],E[0]
E.heapsize=heapsize-1
heapify(E,0)
© GfH&JPK&RL 36
Sorting and Heaps ADC/ADS
X
n−1 Z n
W (n) = 2·⌊log i⌋ 6 2 · (log e) ln x dx = 2·n· log n + c1·n + c2
i=1 1
© GfH&JPK&RL 37
Sorting and Heaps ADC/ADS
© GfH&JPK&RL 38