Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,

Similar presentations


Presentation on theme: "Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,"— Presentation transcript:

1 Lecture 2 Sorting

2 Sorting Problem Insertion Sort, Merge Sort e.g.,

3 Efficiency Running time from receiving the input to producing the output. Insertion Sort Merge Sort Running time

4 Is array a data structure?

5 No! A data structure is a standard part in construction of algorithms. What data structures do you know on array?

6 Is array a data structure? No! A data structure is a standard part in construction of algorithms. What data structures do you know on array? Stack, queue, list, …, heap.

7 Heapsort Heap, a data structure Max-Heapify procedure Building a heap Heapsort

8 A Data Structure Heap A heap is an array object that can be viewed as a nearly complete binary tree. 35 24 1 6 653241 1 2 3 45 6

9

10 Max-Heap

11 Min-Heap

12 Max-Heapify Max-Heapify(A,i) is a subroutine. When it is called, two subtrees rooted at Left(i) and Right(i) are max-heaps, but A[i] may not satisfy the max-heap property. Max-Heapify(A,i) makes the subtree rooted at A[i] become a max-heap by letting A[i] “float down”.

13 4 714 218 7 281 87 12 4 4

14

15 Running time

16 Building a Heap e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

17 4 3 1 16 10 7 2 9 14 8 Proof.

18 4 3 1 16 10 7 2 9 14 8 7

19 4 3 1 16 10 7 2 9 14 8

20 4 3 1 16 10 7 2 9 14 8

21 Building a Max-Heap e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

22 4 3 1 16 10 7 2 9 14 8

23 4 3 1 16 10 7 2 9 14 8

24 4 3 1 16 10 7 14 9 28

25 4 3 1 16 10 7 14 9 28

26 4 1 16 3 7 14 9 28 10

27 4 1 16 3 7 14 9 28 10

28 4 16 1 3 7 14 9 28 10

29 4 16 7 3 1 14 9 28 10

30 16 4 7 3 1 14 9 28 10

31 16 14 7 3 1 4 9 28 10

32 16 14 7 3 1 8 9 24 10

33 Analysis

34 16 14 7 3 1 8 9 24 10

35 Running time

36 Heapsort

37 16 14 7 3 1 8 9 24 10 Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7. Build a max-heap 16, 14, 10, 8, 7, 9, 3, 2, 4, 1.

38 1 14 7 3 16 8 9 24 10

39 1 14 7 3 16 8 9 24 10 1, 14, 10, 8, 7, 9, 3, 2, 4, 16.

40 14 8 7 3 16 4 9 21 10

41 14 8 7 3 16 4 9 21 10 14, 8, 10, 4, 7, 9, 3, 2, 1, 16.

42 1 8 7 3 16 4 9 214 10

43 1 8 7 3 16 4 9 214 10 1, 8, 10, 4, 7, 9, 3, 2, 14, 16.

44 10 8 7 3 16 4 1 214 9

45 10 8 7 3 16 4 1 214 9 10, 8, 9, 4, 7, 1, 3, 2, 14, 16.

46 2 8 7 3 16 4 1 10 14 9

47 2 8 7 3 16 4 1 10 14 9 2, 8, 9, 4, 7, 1, 3, 10, 14, 16.

48 9 8 7 2 16 4 1 10 14 3

49 9 8 7 2 16 4 1 10 14 3 9, 8, 3, 4, 7, 1, 2, 10, 14, 16.

50 2 8 7 9 16 4 1 10 14 3

51 8 7 2 9 16 4 1 10 14 3

52 1 7 2 9 16 4 8 10 14 3

53 7 4 2 9 16 1 8 10 14 3

54 2 4 7 9 16 1 8 10 14 3

55 4 2 7 9 16 1 8 10 14 3

56 1 2 7 9 16 48 10 14 3

57 3 2 7 9 16 48 10 14 1

58 1 2 7 9 16 48 10 14 3

59 2 1 7 9 16 48 10 14 3

60 1 2 7 9 16 48 10 14 3

61 Running Time O(lg n) O(n)

62 Quicksort Worst-case running time Expected running time The best practical choice (why?)

63 Divide and Conquer Divide the problem into subproblems. Conquer the subproblems by solving them recursively. Combine the solutions to subproblems into the solution for original problem.

64 Idea of Quicksort

65 Example

66 Quicksort

67 How to find such a partition? Such a partition may not exist. e.g., 5, 4, 3, 2, 1. Hence, we may need to make such a partition. Take a A[i]. Classify other A[j] by comparing it with A[i].

68

69

70

71 Expected Partition

72 Expected Running Time

73

74 Randomized Quicksort

75

76 What we learnt in this lecture? What is heap, max-heap and min-heap? Heapsort and Quicksort. What is expected running time?


Download ppt "Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,"

Similar presentations


Ads by Google