880 likes | 1.08k Views
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 end Time : O ( n 2 ). 42. 77. "Bubbling Up" the Largest Element. Traverse a collection of elements Move from the front to the end
E N D
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 end Time:O(n2)
42 77 "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 Swap 1 2 3 4 5 6 101 12 42 35 5 77
35 77 "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 1 2 3 4 5 6 Swap 101 12 77 35 5 42
12 77 "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 1 2 3 4 5 6 Swap 101 12 35 77 5 42
"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 1 2 3 4 5 6 101 77 35 12 5 42 No need to swap
5 101 "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 1 2 3 4 5 6 Swap 101 77 35 12 5 42
"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 1 2 3 4 5 6 101 5 77 35 12 42 Largest value correctly placed
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)
42 77 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 Swap 1 2 3 4 5 6 101 12 42 35 5 77
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 Swap 1 2 3 4 5 6 35 42 101 12 77 35 5 42
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 Swap 1 2 3 4 5 6 35 12 42 35 101 12 77 5
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 1 2 3 4 5 6 12 42 35 101 77 5 No need to swap
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 Swap 1 2 3 4 5 6 12 5 42 35 12 101 77 5
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 1 2 3 4 5 6 5 42 35 12 101 77 Smallest value correctly placed
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)
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 12 42 35 5 77
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 12 77 35 5 42
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 12 77 35 5 42
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 12 77 35 5 42
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 12 35 77 5 42
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 12 42 77 5 35
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 12 42 77 5 35
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 77 42 12 5 35
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 77 12 42 5 35
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 77 35 42 5 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 77 35 42 5 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 77 35 42 5 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 77 35 42 5 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 101 77 35 42 5 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 5 101 77 35 42 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 77 101 5 35 42 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 77 101 42 35 5 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 77 101 42 5 35 12
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 77 101 42 12 35 5
Keeping Prefix Sorted Traverse a collection of elements • Move from the front to the end • Keep the prefix sorted throughout 1 2 3 4 5 6 77 101 42 12 35 5
Complexity The Time of all algorithms we saw: O(n2) Can we do better?
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]
Merging Pa, 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
Merging If Pa=n+1 and Pb < m+1 then for i=Pb to m do C[Pc] <- B[i] Pc <- Pc +1 end If Pb=n+1 and Pa < m+1 then for i=Pa to n do C[Pc] <- A[i] Pc <- Pc +1 end end Algorithm
Merging A: B: C:
Merging A: B: C:
Merging A: B: C:
Merging A: B: C:
Merging A: B: C:
Merging A: B: C:
Merging A: B: C:
Merging A: B: C:
Merging A: B: C: Time:O(n+m) Linear !!!
Merge Sort A 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)