Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3503 FALL 2012 Shayan Javed Lecture 16

Similar presentations


Presentation on theme: "COP 3503 FALL 2012 Shayan Javed Lecture 16"— Presentation transcript:

1 COP 3503 FALL 2012 Shayan Javed Lecture 16
Programming Fundamentals using Java

2 Sorting Another crucial problem. Used everywhere:
Sorting numbers (prices/grades/ratings/etc..) Names Dates Rating

3 Sorting When shopping online (Amazon for ex.):

4 Sorting We designed a RedBox system where you can sort by different criteria

5 Sorting We designed a RedBox system where you can sort by different criteria How can you sort in Java?

6 Sorting We designed a RedBox system where you can sort by different criteria How can you sort in Java? Arrays.sort(..) – but how does it work?

7 Sorting We designed a RedBox system where you can sort by different criteria How can you sort in Java? Arrays.sort(..) – but how does it work? Going to look at a few different algorithms.

8 Sorting Let’s see if we can come up with one on our own...

9 Sorting Let’s see if we can come up with one on our own...
array A (N = A.length): sorted array A: i 1 2 3 4 5 A 55 19 100 45 87 33 i 1 2 3 4 5 A 19 33 45 55 87 100

10 Sorting Compare values at indices 0 and 1 first. i 1 2 3 4 5 A 55 19
1 2 3 4 5 A 55 19 100 45 87 33

11 Sorting Compare values at indices 0 and 1 first. i 1 2 3 4 5 A 55 19
1 2 3 4 5 A 55 19 100 45 87 33

12 Sorting Compare values at indices 0 and 1 first.
Swap if A[1] < A[0]: i 1 2 3 4 5 A 55 19 100 45 87 33 i 1 2 3 4 5 A 19 55 100 45 87 33

13 Sorting Compare values at indices 1 and 2. i 1 2 3 4 5 A 19 55 100 45
1 2 3 4 5 A 19 55 100 45 87 33

14 Sorting Compare values at indices 1 and 2. Is A[2] < A[1]? No: i 1
1 2 3 4 5 A 19 55 100 45 87 33 i 1 2 3 4 5 A 19 55 100 45 87 33

15 Sorting Compare values at indices 2 and 3. i 1 2 3 4 5 A 19 55 100 45
1 2 3 4 5 A 19 55 100 45 87 33

16 Sorting Compare values at indices 2 and 3.
Is A[3] < A[2]? Yes! Swap. i 1 2 3 4 5 A 19 55 100 45 87 33 i 1 2 3 4 5 A 19 55 45 100 87 33

17 Sorting Keep repeating this process until you reach the end of the array.

18 Sorting Keep repeating this process until you reach the end of the array. Result: i 1 2 3 4 5 A 19 55 45 87 33 100

19 Sorting Largest value in the array is now at the end of the array.
Result: i 1 2 3 4 5 A 19 55 45 87 33 100

20 Sorting What do we do next? i 1 2 3 4 5 A 19 55 45 87 33 100

21 Sorting What do we do next? Repeat the process. i 1 2 3 4 5 A 19 55 45
1 2 3 4 5 A 19 55 45 87 33 100

22 Sorting What do we do next? Repeat the process. Result: i 1 2 3 4 5 A
1 2 3 4 5 A 19 45 55 33 87 100

23 Sorting Have the second largest value at index N - 2 Result: i 1 2 3 4
1 2 3 4 5 A 19 45 55 33 87 100

24 Sorting Have the second largest value at index N - 2
How many more times to sort? Result: i 1 2 3 4 5 A 19 45 55 33 87 100

25 Sorting Have the second largest value at index N - 2
How many more times to sort? Total of N times Result: i 1 2 3 4 5 A 19 45 55 33 87 100

26 Sorting Have the second largest value at index N - 2
How many more times to sort? Total of N times Result: i 1 2 3 4 5 A 19 33 45 55 87 100 i 1 2 3 4 5 A 19 55 45 33 87 100

27 Bubble Sort This algorithm is known as “Bubble Sort”

28 Bubble Sort This algorithm is known as “Bubble Sort”
The max value “bubbles” to the end of the list at each pass.

29 Bubble Sort This algorithm is known as “Bubble Sort”
The max value “bubbles” to the end of the list at each pass. Simplest sorting algorithm.

30 Bubble Sort Let’s write the code for it:

31 Bubble Sort Let’s write the code for it:
static void bubbleSort(int[] A, int N) { for(int i = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; }

32 Bubble Sort Let’s write the code for it:
static void bubbleSort(int[] A, int N) { for(int i = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; }

33 Bubble Sort Let’s write the code for it:
static void bubbleSort(int[] A, int N) { for(int i = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; }

34 Bubble Sort Let’s write the code for it:
static void bubbleSort(int[] A, int N) { for(int i = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; }

35 Bubble Sort Let’s write the code for it:
static void bubbleSort(int[] A, int N) { for(int i = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; }

36 Bubble Sort Efficiency
How efficient is it?

37 Bubble Sort Efficiency
How efficient is it? What is the O(..) of the algorithm?

38 Bubble Sort Efficiency
Let’s look at one pass.

39 Bubble Sort Efficiency
Let’s look at one pass. i 1 2 3 4 5 A 55 19 100 45 87 33

40 Bubble Sort Efficiency
Let’s look at one pass. To: i 1 2 3 4 5 A 55 19 100 45 87 33 i 1 2 3 4 5 A 19 55 45 87 33 100

41 Bubble Sort Efficiency
Let’s look at one pass. To: What’s the efficiency of this pass? i 1 2 3 4 5 A 55 19 100 45 87 33 i 1 2 3 4 5 A 19 55 45 87 33 100

42 Bubble Sort Efficiency
Let’s look at one pass. To: What’s the efficiency of this pass? O(N) i 1 2 3 4 5 A 55 19 100 45 87 33 i 1 2 3 4 5 A 19 55 45 87 33 100

43 Bubble Sort Efficiency
How many of these passes do we have?

44 Bubble Sort Efficiency
How many of these passes do we have? N passes.

45 Bubble Sort Efficiency
How many of these passes do we have? N passes. Efficiency: O(N) * N = O(N2)

46 Bubble Sort Efficiency
How can we make the algorithm a little more efficient/faster?

47 Bubble Sort Efficiency
static void bubbleSort(int[] A, int N) { for(int i = 0; i < N; i++) { for (int j = 1; j < (N-i); j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; }

48 Bubble Sort Efficiency
static void bubbleSort(int[] A, int N) { for(int i = 0; i < N; i++) { for (int j = 1; j < (N-i); j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; } We know the end keeps getting sorted properly

49 Bubble Sort Efficiency
But efficiency is still O(N2)

50 Bubble Sort Efficiency
But efficiency is still O(N2) Thus bubble sort isn’t really a good sorting algorithm...

51 Bubble Sort Efficiency
But efficiency is still O(N2) Thus bubble sort isn’t really a good sorting algorithm... Going to look at other alternatives.

52 Find the minimum in an array
How will you find the minimum number in an array? i 1 2 3 4 5 A 55 19 100 45 87 33

53 Find the minimum in an array
min = A[0] for i = 1 to A.length – 1: if A[i] < min: min = A[i]

54 Find the minimum in an array
What can we do with the minimum value?

55 Find the minimum in an array
What can we do with the minimum value? Swap it with the value at index 0

56 Find the minimum in an array
What can we do with the minimum value? Swap it with the value at index 0 Repeat for the rest of the indices...

57 Find the minimum in an array
What can we do with the minimum value? Swap it with the value at index 0 Repeat for the rest of the indices... Result: Sorted array!

58 Selection Sort static void selectionSort(int[] A) { for (int i = 0; i < A.length; i++) { int min = i; // index of minimum value for(int j = i; j <= A.length; j++) { if ( A[j] < A[min]) min = j; } // swap if (j != i) swap(A, i, j);

59 Selection Sort static void selectionSort(int[] A) { for (int i = 0; i < A.length - 1; i++) { int min = i; // index of minimum value for(int j = i; j <= A.length; j++) { if ( A[j] < A[min]) min = j; } // swap if (j != i) swap(A, i, j);

60 Selection Sort static void selectionSort(int[] A) { for (int i = 0; i < A.length - 1; i++) { int min = i; // index of minimum value for(int j = i; j <= A.length; j++) { if ( A[j] < A[min]) min = j; } // swap if (j != i) swap(A, i, j);

61 Selection Sort static void selectionSort(int[] A) { for (int i = 0; i < A.length - 1; i++) { int min = i; // index of minimum value for(int j = i + 1; j < A.length; j++) { if ( A[j] < A[min]) min = j; } // swap if (j != i) swap(A, i, j);

62 Selection Sort static void selectionSort(int[] A) { for (int i = 0; i < A.length - 1; i++) { int min = i; // index of minimum value for(int j = i + 1; j < A.length; j++) { if ( A[j] < A[min]) min = j; } // swap if (j != i) swap(A, i, j);

63 Selection Sort static void selectionSort(int[] A) { for (int i = 0; i < A.length - 1; i++) { int min = i; // index of minimum value for(int j = i + 1; j < A.length; j++) { if ( A[j] < A[min]) min = j; } // swap if (min != i) swap(A, i, min);

64 Selection Sort Efficiency

65 Selection Sort Efficiency
Better than Bubble Sort

66 Selection Sort Efficiency
Better than Bubble Sort But rarely used

67 Insertion Sort Concept:
Go through every element, insert it in it’s correct position in the sub-array before it

68 Insertion Sort array A (N = A.length): i 1 2 3 4 5 A 55 19 100 45 87
1 2 3 4 5 A 55 19 100 45 87 33

69 Insertion Sort array A (N = A.length): Value = 55
Look at sub-array between indices 0 and 0 i 1 2 3 4 5 A 55 19 100 45 87 33

70 Insertion Sort array A (N = A.length): Value = 55
Look at sub-array between indices 0 and 0 Already sorted, let’s look at next index i 1 2 3 4 5 A 55 19 100 45 87 33

71 Insertion Sort array A (N = A.length): Value = 19
Look at sub-array between indices 0 and 1 i 1 2 3 4 5 A 55 19 100 45 87 33

72 Insertion Sort array A (N = A.length): Value = 19
Look at sub-array between indices 0 and 1 Is Value < A[0]? Yes! i 1 2 3 4 5 A 55 19 100 45 87 33

73 Insertion Sort array A (N = A.length): Value = 19
Look at sub-array between indices 0 and 1 Is Value < A[0]? Yes! Move 55 forward by 1 index. i 1 2 3 4 5 A 55 100 45 87 33

74 Insertion Sort array A (N = A.length): Value = 19
Look at sub-array between indices 0 and 1 Is Value < A[0]? Yes! Move 55 forward by 1 index. Put 19 at index 0 (reached the beginning) i 1 2 3 4 5 A 19 55 100 45 87 33

75 Insertion Sort array A (N = A.length): Value = 100
Look at sub-array between indices 0 and 2 i 1 2 3 4 5 A 19 55 100 45 87 33

76 Insertion Sort array A (N = A.length): Value = 100
Look at sub-array between indices 0 and 2 Is Value < A[1]? No. i 1 2 3 4 5 A 19 55 100 45 87 33

77 Insertion Sort array A (N = A.length): Value = 100
Look at sub-array between indices 0 and 2 Is Value < A[1]? No. Continue i 1 2 3 4 5 A 19 55 100 45 87 33

78 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 i 1 2 3 4 5 A 19 55 100 45 87 33

79 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[2]? Yes. i 1 2 3 4 5 A 19 55 100 45 87 33

80 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[2]? Yes. Move A[2] forward by one position i 1 2 3 4 5 A 19 55 100 87 33

81 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[1]? Yes. i 1 2 3 4 5 A 19 55 100 87 33

82 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[1]? Yes. i 1 2 3 4 5 A 19 55 100 87 33

83 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[1]? Yes. Move A[1] forward by one position i 1 2 3 4 5 A 19 55 100 87 33

84 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[0]? No. i 1 2 3 4 5 A 19 55 100 87 33

85 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[0]? No. i 1 2 3 4 5 A 19 55 100 87 33

86 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[0]? No. STOP! i 1 2 3 4 5 A 19 55 100 87 33

87 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Is Value < A[0]? No. STOP! Put Value at index 1 (where we stopped) i 1 2 3 4 5 A 19 45 55 100 87 33

88 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Note how this sub-array is sorted. i 1 2 3 4 5 A 19 45 55 100 87 33

89 Insertion Sort array A (N = A.length): Value = 45
Look at sub-array between indices 0 and 3 Note how this sub-array is sorted. Repeat for Value = 87 & sub-array between 0 and 4 i 1 2 3 4 5 A 19 45 55 100 87 33

90 Insertion Sort Exercise: Write the code by yourselves.

91 Insertion Sort Exercise: Write the code by yourselves.
(Without looking it up online of course...) Try to challenge yourself

92 Insertion Sort Efficiency
Also O(N2)

93 Insertion Sort Efficiency
Also O(N2) But:

94 Insertion Sort Efficiency
Also O(N2) But: Very fast for sorted/partially sorted arrays.

95 Insertion Sort Efficiency
Also O(N2) But: Very fast for sorted/partially sorted arrays. Efficient for small data sets

96 Insertion Sort Efficiency
Also O(N2) But: Very fast for sorted/partially sorted arrays. Efficient for small data sets Online: Sort a list as it receives input

97 Insertion Sort Efficiency
Also O(N2) But: Very fast for sorted/partially sorted arrays. Efficient for small data sets Online: Sort a list as it receives input Better than Bubble and Selection Sort

98 Sorting None of the algorithms seen so far are “good enough”

99 Sorting None of the algorithms seen so far are “good enough”
Especially Bubble and Selection

100 Sorting None of the algorithms seen so far are “good enough”
Especially Bubble and Selection Merge Sort and QuickSort are the best algorithms

101 Sorting None of the algorithms seen so far are “good enough”
Especially Bubble and Selection Merge Sort and QuickSort are the best algorithms Going to look at Merge Sort

102 Merge Sort Concept: Divide a list equally into two

103 Merge Sort Concept: Divide a list equally into two Sort the two lists

104 Merge Sort Concept: Divide a list equally into two Sort the two lists
Merge them

105 Merge Sort Concept: Divide a list equally into two Sort the two lists
Merge them Repeat process recursively

106 Merge Sort i 1 2 3 4 5 A 55 19 100 45 87 33

107 Merge Sort Split i 1 2 3 4 5 A 55 19 100 45 87 33 1 2 55 19 100 3 4 5 45 87 33

108 Merge Sort Split i 1 2 3 4 5 A 55 19 100 45 87 33 1 2 55 19 100 3 4 5 45 87 33 55 1 2 19 100

109 Merge Sort Split i 1 2 3 4 5 A 55 19 100 45 87 33 1 2 55 19 100 3 4 5 45 87 33 55 1 2 19 100 3 45 4 5 87 33

110 Merge Sort Split Now Sort and Merge i 1 2 3 4 5 A 55 19 100 45 87 33 1
1 2 3 4 5 A 55 19 100 45 87 33 1 2 55 19 100 3 4 5 45 87 33 55 1 2 19 100 3 45 4 5 87 33

111 Merge Sort 55 1 2 19 100

112 Merge Sort Already sorted Already Sorted 55 1 2 19 100

113 Merge Sort Already sorted Already Sorted
Merge the two in the proper order: 55 1 2 19 100

114 Merge Sort Already sorted Already Sorted
Merge the two in the proper order: s Sorted! 55 1 2 19 100 1 2 19 55 100

115 Merge Sort 3 45 55 4 5 87 33 1 2 19 100

116 Merge Sort Already sorted Sort it: 3 45 55 4 5 87 33 1 2 19 100

117 Merge Sort Already sorted Sort it: 3 45 55 4 5 87 33 1 2 19 100 4 5 33
55 4 5 87 33 1 2 19 100 4 5 33 87

118 Merge Sort Already sorted Sort it: Now Merge: 3 45 55 4 5 87 33 1 2 19
55 4 5 87 33 1 2 19 100 4 5 33 87 3 4 5 33 45 87

119 Merge Sort 1 2 19 55 100 3 4 5 33 45 87

120 Merge Sort Both already sorted. 1 2 19 55 100 3 4 5 33 45 87

121 Merge Sort Both already sorted. Merge: 1 2 19 55 100 3 4 5 33 45 87 i
1 2 19 55 100 3 4 5 33 45 87 i 1 2 3 4 5 A 19 33 45 55 87 100

122 Merge Sort mergeSort(A[]): if size(A) <= 1: return A // split into two lists middleIndex = size(A)/2 left[] = A[0…middleIndex-1] right[] = A[middleIndex…size(A)-1] left = mergeSort(left) right = mergeSort(right) result[] = merge(left, right) return result

123 Merge Sort mergeSort(A[]): if size(A) <= 1: return A // split into two lists middleIndex = size(A)/2 left[] = A[0…middleIndex-1] right[] = A[middleIndex…size(A)-1] left = mergeSort(left) right = mergeSort(right) result[] = merge(left, right) return result

124 Merge Sort mergeSort(A[]): if size(A) <= 1: return A // split into two lists middleIndex = size(A)/2 left[] = A[0…middleIndex-1] right[] = A[middleIndex…size(A)-1] left = mergeSort(left) right = mergeSort(right) result[] = merge(left, right) return result

125 Merge Sort mergeSort(A[]): if size(A) <= 1: return A // split into two lists middleIndex = size(A)/2 left[] = A[0…middleIndex-1] right[] = A[middleIndex…size(A)-1] left = mergeSort(left) right = mergeSort(right) result[] = merge(left, right) return result

126 Merge Sort mergeSort(A[]): if size(A) <= 1: return A // split into two lists middleIndex = size(A)/2 left[] = A[0…middleIndex-1] right[] = A[middleIndex…size(A)-1] left = mergeSort(left) right = mergeSort(right) result[] = merge(left, right) return result

127 Merge Sort mergeSort(A[]): if size(A) <= 1: return A // split into two lists middleIndex = size(A)/2 left[] = A[0…middleIndex-1] right[] = A[middleIndex…size(A)-1] left = mergeSort(left) right = mergeSort(right) result[] = merge(left, right) return result

128 Merge Sort mergeSort(A[]): if size(A) <= 1: return A // split into two lists middleIndex = size(A)/2 left[] = A[0…middleIndex-1] right[] = A[middleIndex…size(A)-1] left = mergeSort(left) right = mergeSort(right) result[] = merge(left, right) return result

129 Merge Sort mergeSort(A[]): if size(A) <= 1: return A // split into two lists middleIndex = size(A)/2 left[] = A[0…middleIndex-1] right[] = A[middleIndex…size(A)-1] left = mergeSort(left) right = mergeSort(right) result[] = merge(left, right) return result

130 Merge Sort How to merge two sorted lists?

131 Merge Sort How to merge two sorted lists? Figure that out yourself

132 Merge Sort How to merge two sorted lists? Figure that out yourself
Try to to do it on paper first

133 Merge Sort How to merge two sorted lists? Figure that out yourself
Try to to do it on paper first Exercise: Implement Merge Sort Can it be done iteratively?

134 Merge Sort Efficiency O(NlogN)

135 Merge Sort Efficiency O(NlogN)
Much faster than the previous algorithms

136 Merge Sort Efficiency O(NlogN)
Much faster than the previous algorithms Used in java.util.Arrays.sort(…)

137 Summary Sorting is a very important problem.
Bubble, Selection and Insertion Sort. Insertion Sort great for small arrays. Merge Sort much faster than others


Download ppt "COP 3503 FALL 2012 Shayan Javed Lecture 16"

Similar presentations


Ads by Google