HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1...
-
Upload
dayna-mccormick -
Category
Documents
-
view
217 -
download
0
Transcript of HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1...
![Page 1: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/1.jpg)
HEAPS
Amihood Amir
Bar Ilan University2014
![Page 2: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/2.jpg)
Sorting
Bubblesort:Until no exchanges:
For i=1 to n-1
if A[i]>A[i+1] then exchange their values
end
end
Time: O(n2)
![Page 3: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/3.jpg)
"Bubbling Up" the Largest Element
Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end
using pair-wise comparisons and swapping
512354277 101
1 2 3 4 5 6
Swap42 77
![Page 4: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/4.jpg)
"Bubbling Up" the Largest Element
Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end
using pair-wise comparisons and swapping
512357742 101
1 2 3 4 5 6
Swap35 77
![Page 5: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/5.jpg)
"Bubbling Up" the Largest Element
Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end
using pair-wise comparisons and swapping
512773542 101
1 2 3 4 5 6
Swap12 77
![Page 6: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/6.jpg)
"Bubbling Up" the Largest Element
Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end
using pair-wise comparisons and swapping
577123542 101
1 2 3 4 5 6
No need to swap
![Page 7: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/7.jpg)
"Bubbling Up" the Largest Element
Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end
using pair-wise comparisons and swapping
577123542 101
1 2 3 4 5 6
Swap5 101
![Page 8: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/8.jpg)
"Bubbling Up" the Largest Element
Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end
using pair-wise comparisons and swapping
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
![Page 9: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/9.jpg)
Sorting
Selection sort:For i=1 to n do
For j=i+1 to n do
If A[i]>A[j] then exchange them
end
end
Time: = O(n2)
n
i
i1
![Page 10: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/10.jpg)
Selecting the Smallest Element
Traverse a collection of elements– Move from the front to the end– Select the smallest value using pair-wise
comparisons and swapping
512354277 101
1 2 3 4 5 6
Swap42 77
![Page 11: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/11.jpg)
Selecting the Smallest Element
Traverse a collection of elements– Move from the front to the end– Select the smallest value using pair-wise
comparisons and swapping
512357742 101
1 2 3 4 5 6
Swap
35 42
![Page 12: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/12.jpg)
Selecting the Smallest Element
Traverse a collection of elements– Move from the front to the end– Select the smallest value using pair-wise
comparisons and swapping
51277 101
1 2 3 4 5 6
Swap
35 4212 35
![Page 13: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/13.jpg)
Selecting the Smallest Element
Traverse a collection of elements– Move from the front to the end– Select the smallest value using pair-wise
comparisons and swapping
577 101
1 2 3 4 5 6
4212 35
No need to swap
![Page 14: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/14.jpg)
Selecting the Smallest Element
Traverse a collection of elements– Move from the front to the end– Select the smallest value using pair-wise
comparisons and swapping
577 101
1 2 3 4 5 6
4212 35
Swap
5 12
![Page 15: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/15.jpg)
Selecting the Smallest Element
Traverse a collection of elements– Move from the front to the end– Select the smallest value using pair-wise
comparisons and swapping
77 101
1 2 3 4 5 6
42 355 12
Smallest value correctly placed
![Page 16: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/16.jpg)
Sorting
Insertion sort:
For i=1 to n do
Insert A[i+1] into appropriate (sorted)
position in A[1],…,A[i]
end
Time: = O(n2)
n
i
i1
![Page 17: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/17.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
512354277 101
1 2 3 4 5 6
![Page 18: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/18.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
512357742 101
1 2 3 4 5 6
![Page 19: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/19.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
512357742 101
1 2 3 4 5 6
![Page 20: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/20.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
512357742 101
1 2 3 4 5 6
![Page 21: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/21.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
512773542 101
1 2 3 4 5 6
![Page 22: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/22.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
512774235 101
1 2 3 4 5 6
![Page 23: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/23.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
512774235 101
1 2 3 4 5 6
![Page 24: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/24.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
577124235 101
1 2 3 4 5 6
![Page 25: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/25.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
577421235 101
1 2 3 4 5 6
![Page 26: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/26.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
577423512 101
1 2 3 4 5 6
![Page 27: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/27.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
577423512 101
1 2 3 4 5 6
![Page 28: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/28.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
577423512 101
1 2 3 4 5 6
![Page 29: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/29.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
577423512 101
1 2 3 4 5 6
![Page 30: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/30.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
577423512 101
1 2 3 4 5 6
![Page 31: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/31.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
10177423512 5
1 2 3 4 5 6
![Page 32: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/32.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
101 5423512 77
1 2 3 4 5 6
![Page 33: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/33.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
101 42 53512 77
1 2 3 4 5 6
![Page 34: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/34.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
101 42 35 512 77
1 2 3 4 5 6
![Page 35: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/35.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
101 42 35 12 5 77
1 2 3 4 5 6
![Page 36: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/36.jpg)
Keeping Prefix Sorted
Traverse a collection of elements– Move from the front to the end– Keep the prefix sorted throughout
101 42 35 12 5 77
1 2 3 4 5 6
![Page 37: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/37.jpg)
Complexity
The Time of all algorithms we saw:
O(n2)
Can we do better?
![Page 38: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/38.jpg)
Merge Sort
Based on the Merging operation.
Given two sorted arrays: A[1],…,A[n] and B[1],…,B[m].
merge them into one sorted array: C[1],…,C[n+m]
![Page 39: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/39.jpg)
MergingPa, Pb, Pc <- 1
While (Pa < n+1 and Pb < m+1)
If A[Pa] ≤ B[Pb] then C[Pc] <- A[Pa]
Pa <- Pa+1
Pc <- Pc+1
else C[Pc] <- B[Pb]
Pb <- Pb+1
Pc <- Pc+1
end
![Page 40: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/40.jpg)
MergingIf Pa=n+1 and Pb < m+1
then for i=Pb to m do
C[Pc] <- B[i]Pc <- Pc +1
endIf Pb=n+1 and Pa < m+1
then for i=Pa to n doC[Pc] <- A[i]Pc <- Pc +1
endend Algorithm
![Page 41: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/41.jpg)
Merging
3102354 152575A: B:
C:
![Page 42: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/42.jpg)
Merging
3102354 52575
1
A: B:
C:
![Page 43: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/43.jpg)
Merging
102354 52575
13
A: B:
C:
![Page 44: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/44.jpg)
Merging
102354 2575
135
A: B:
C:
![Page 45: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/45.jpg)
Merging
2354 2575
13510
A: B:
C:
![Page 46: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/46.jpg)
Merging
54 2575
1351023
A: B:
C:
![Page 47: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/47.jpg)
MergingMerging
54 75
135102325
A: B:
C:
![Page 48: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/48.jpg)
Merging
75
13510232554
A: B:
C:
![Page 49: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/49.jpg)
Merging
1351023255475
A: B:
C:
Time: O(n+m) Linear !!!
![Page 50: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/50.jpg)
Merge SortA recursive sorting algorithm:Mergesort(A)If n=1 then Return(A) else
Split A[1],…,A[n] to two length n/2 arrays:
A[1],…,A[n/2] and A[n/2+1],…,A[n]
Mergesort(A[1],…,A[n/2])
Mergesort(A[n/2+1],…,A[n])
Merge(A[1],…,A[n/2], A[n/2+1],…,A[n], B)
A <- B
Return(A)
![Page 51: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/51.jpg)
Merge Sort Example996861558358640
![Page 52: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/52.jpg)
Merge Sort Example996861558358640
9968615 58358640
![Page 53: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/53.jpg)
Merge Sort Example996861558358640
9968615 58358640
8615996 5835 8640
![Page 54: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/54.jpg)
Merge Sort Example996861558358640
9968615 58358640
8615996 5835 8640
99 6 86 15 58 35 86 40
![Page 55: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/55.jpg)
Merge Sort Example996861558358640
9968615 58358640
8615996 5835 8640
99 6 86 15 58 35 86 40
4 0
![Page 56: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/56.jpg)
Merge Sort Example
99 6 86 15 58 35 86 04
4 0Merge
![Page 57: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/57.jpg)
Merge Sort Example
1586699 5835 0486
99 6 86 15 58 35 86 04
Merge
![Page 58: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/58.jpg)
Merge Sort Example
6158699 04355886
1586699 5835 0486
Merge
![Page 59: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/59.jpg)
Merge Sort Example046153558868699
6158699 04355886
Merge
![Page 60: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/60.jpg)
Merge Sort Example046153558868699
![Page 61: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/61.jpg)
Merge Sort TimeThe recurrence:
TM(n)=2TM(n/2)+n
TM(1)=1
Closed Form of TM(n):
O(n log n)
![Page 62: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/62.jpg)
A Data Structures AlgorithmSort array A using AVL Trees:
For i=1 to n do:
Insert A[i] into AVL tree
For i=1 to n do:
Extract smallest element from AVL tree and put in A[i]
![Page 63: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/63.jpg)
Sorting Algorithm using AVLCan we extract the smallest element
from AVL tree?
-- Yes. Go all the way to the left.
Time: O(log n)Therefore:
Sorting Algorithm Time: O(n log n)
![Page 64: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/64.jpg)
Priority QueueWhat property of the AVL Tree did we
use?
Only that we can find the minimum fast.
A Priority Queue is a data structure that supports the following operations:
INSERT (Q,k)
EXTRACTMIN(Q,m)
![Page 65: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/65.jpg)
Priority Queue Implementation
Heap:A binary tree where every vertex has a
key.For every vertex v, key(v) ≤ key(w),
where w is a child of v.
Difference between heap and binary search tree:10 10
5 3040 30
Heap Binary search tree
![Page 66: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/66.jpg)
HeapAdditional Heap Property:All levels of the heap are full, except
possibly the last level, which is left-filled. 4
6
207
811
5
9
1214
15
2516
Example:
![Page 67: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/67.jpg)
Heap PropertiesThe keys on every path from the root to
a leaf are nondecreasing.
4
6
207
811
5
9
1214
15
2516
Example:
![Page 68: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/68.jpg)
Heap Properties
The height of a heap with n elements is O(log n).
)1(log n
4
6
207
811
5
9
1214
15
2516
Example:
1
0 0
12222h
i
h
i
hihi n h < log n ≤ h+1
![Page 69: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/69.jpg)
Heap Insertion
Insert 6
3
35232822
8201021
74
25 6
![Page 70: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/70.jpg)
Heap Insertion
Insert 6
3
35232822
8201021
74
25 620
6
![Page 71: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/71.jpg)
Heap Insertion
Insert 6
3
35232822
81021
74
25 20
6
6
7
OK
![Page 72: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/72.jpg)
Heap Deletion
Delete Min 3
3
35232822
81021
4
25 20
6
7
20
Now fix heap
![Page 73: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/73.jpg)
Heap Deletion
Delete Min 3
35232822
81021
4
25
6
7
20
20
4
![Page 74: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/74.jpg)
Heap Deletion
Delete Min 3
35232822
81021
25
6
720
20
4
10
OK
![Page 75: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/75.jpg)
Heap Operations Time
O(log n)
Conclude: Heap of n elements can be constructed in time:
O(n log n)
But: We can do a lot better!!!
![Page 76: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/76.jpg)
Build Heap
Put all elements in a tree where all levels are full and the last level is left-filled.
Construct heaps from leaves up
![Page 77: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/77.jpg)
Time Analysis
For trees in level h : 1For trees in level h-1 : 2
For trees in level h-i : i
For trees in level 0 : log n
![Page 78: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/78.jpg)
Time Analysis
Number of trees in level h: n/2Number of trees in level h-1: n/22
……Number of trees in level h-i:n/2i+1
…Number of trees in level 0 : 1
![Page 79: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/79.jpg)
Total Time
Calculate it: Note that n/2 trees do constant time work, and another quarter need to go to two levels. Only one tree needs to fix log n levels.
Formally:
Need to calculate this
n
i
n
iiin
ini
nn
nnnn log
1 1log22 22
log2
...32
22
12
![Page 80: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/80.jpg)
Total Time
We know: for x<1.
Differentiate both sides:
Multiply both sides by x:
1 1
1
i
i
xx
12
1
)1(
1
i
i
xix
12)1(i
i
x
xix
![Page 81: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/81.jpg)
Total Time
In our case: x = 1/2.
So total time: O(n) .
2)1( 4
1
21
12
21
21
21
n
i
ii
![Page 82: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/82.jpg)
In our case:
Look Ma No Pointers
Our tree is almost complete. All levels are full and the last one is left-filled.
So write down all keys sequentially from top to bottom and left to right.
![Page 83: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/83.jpg)
Getting rid of Pointers
Example:
3,4,6,21,10,7,8,22,28,23,35,25,20
3
35232822
871021
64
25 20
![Page 84: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/84.jpg)
Calculate Locationnode i in level j = location 2j-1+i in array.
Location i in array = node in level .
1logmod ii
3
35232822
871021
64
25 20
ilog
![Page 85: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/85.jpg)
Node j in row i is the 2i-1+j’s element.Its left son is the 2i+1-1+2(j-1)+1’s
element.
But 2i+1-1+2(j-1)+1 = 2i+1+2(j-1) = 2i+1+2j-2 = 2(2i-1+j).
Location of children
row i j
![Page 86: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/86.jpg)
Conclude:
Location of left son of i is 2i.Location of right son of i is 2i+1.Location of father of i is: i/2, if i is even (i-1)/2, if i is odd
Location of Children
![Page 87: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/87.jpg)
How?Given array A of n elements:Construct a heap HA in-place.For i=n downto 1 do
EXTRACTMIN(HA,v)Put v in location A[i]
Endfor
Time: O(n) for heap construction and O(nlog n) for loop.
Heap Sort
![Page 88: HEAPS Amihood Amir Bar Ilan University 2014. Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.](https://reader035.fdocuments.us/reader035/viewer/2022081513/5697bfd91a28abf838cafbd4/html5/thumbnails/88.jpg)
Note:Array A is now sorted in non-increasing
order.What do we do if we want to sort in
non-decreasing order?Options:• Reverse order of A in linear time in-
place.• Use MAX rather than MIN priority
Queue.
Heap Sort