Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Sorting.

Similar presentations


Presentation on theme: "Lecture 2 Sorting."— Presentation transcript:

1 Lecture 2 Sorting

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

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

4 Quiz Sample Is array a data structure?

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

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

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

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 14 7 14 7 4 2 8 1 2 8 1 14 8 7 2 1 4

14

15 Running time

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

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

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

19 4 1 3 9 10 2 16 8 7 14

20 4 1 3 9 10 2 16 8 7 14

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

22 4 1 3 9 10 2 16 8 7 14

23 4 1 3 9 10 2 16 8 7 14

24 4 1 3 9 10 16 14 2 8 7

25 4 1 3 9 10 14 16 2 8 7

26 4 1 10 3 14 16 9 2 8 7

27 4 1 10 3 14 16 9 2 8 7

28 4 16 10 3 14 1 9 2 8 7

29 4 16 10 3 14 7 9 2 8 1

30 16 4 10 3 14 7 9 2 8 1

31 16 14 10 3 4 7 9 2 8 1

32 16 14 10 3 7 9 8 2 4 1

33 Analysis

34 16 14 10 3 7 9 8 2 4 1

35 Running time

36 Heapsort

37 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 10 3 7 9 8 2 4 16

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

40 14 8 10 3 7 9 4 2 1 16

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

42 1 8 10 3 7 9 4 2 14 16

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

44 10 8 9 3 7 1 4 2 14 16

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

46 2 8 9 1 3 7 4 14 16 10

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

48 9 8 3 1 2 7 4 14 16 10

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

50 2 8 3 1 9 7 4 14 16 10

51 8 7 3 9 2 1 4 14 16 10

52 1 7 3 9 2 8 4 14 16 10

53 7 4 3 9 2 8 1 14 16 10

54 2 4 3 9 7 8 1 14 16 10

55 4 2 3 9 7 8 1 14 16 10

56 1 2 3 9 4 7 8 14 16 10

57 3 2 1 9 4 7 8 14 16 10

58 1 2 3 9 4 7 8 14 16 10

59 2 1 3 9 4 7 8 14 16 10

60 1 2 3 9 4 7 8 14 16 10

61 Running Time O(n) O(lg 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 Randomized Quicksort

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

Similar presentations


Ads by Google