# HEAPS

## HEAPS

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. HEAPS Amihood Amir Bar Ilan University 2014

2. 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)

3. 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

4. 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

5. 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

6. "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

7. 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

8. "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

9. 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)

10. 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

11. 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

12. 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

13. 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

14. 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

15. 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

16. 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)

17. 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

18. 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

19. 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

20. 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

21. 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

22. 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

23. 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

24. 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

25. 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

26. 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

27. 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

28. 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

29. 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

30. 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

31. 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

32. 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

33. 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

34. 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

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 77 101 42 12 35 5

36. 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

37. Complexity The Time of all algorithms we saw: O(n2) Can we do better?

38. 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]

39. 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

40. 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

41. Merging A: B: C:

42. Merging A: B: C:

43. Merging A: B: C:

44. Merging A: B: C:

45. Merging A: B: C:

46. Merging A: B: C:

47. Merging A: B: C:

48. Merging A: B: C:

49. Merging A: B: C: Time:O(n+m) Linear !!!

50. 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)