COP 3503 FALL 2012 Shayan Javed Lecture 16 Programming Fundamentals using Java
Sorting Another crucial problem. Used everywhere: Sorting numbers (prices/grades/ratings/etc..) Names Dates Rating
Sorting When shopping online (Amazon for ex.):
Sorting We designed a RedBox system where you can sort by different criteria
Sorting We designed a RedBox system where you can sort by different criteria How can you sort in Java?
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?
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.
Sorting Let’s see if we can come up with one on our own...
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
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
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
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
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
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
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
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
Sorting Keep repeating this process until you reach the end of the array.
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
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
Sorting What do we do next? i 1 2 3 4 5 A 19 55 45 87 33 100
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
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
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
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
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
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
Bubble Sort This algorithm is known as “Bubble Sort”
Bubble Sort This algorithm is known as “Bubble Sort” The max value “bubbles” to the end of the list at each pass.
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.
Bubble Sort Let’s write the code for it:
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; }
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; }
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; }
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; }
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; }
Bubble Sort Efficiency How efficient is it?
Bubble Sort Efficiency How efficient is it? What is the O(..) of the algorithm?
Bubble Sort Efficiency Let’s look at one pass.
Bubble Sort Efficiency Let’s look at one pass. i 1 2 3 4 5 A 55 19 100 45 87 33
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
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
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
Bubble Sort Efficiency How many of these passes do we have?
Bubble Sort Efficiency How many of these passes do we have? N passes.
Bubble Sort Efficiency How many of these passes do we have? N passes. Efficiency: O(N) * N = O(N2)
Bubble Sort Efficiency How can we make the algorithm a little more efficient/faster?
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; }
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
Bubble Sort Efficiency But efficiency is still O(N2)
Bubble Sort Efficiency But efficiency is still O(N2) Thus bubble sort isn’t really a good sorting algorithm...
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.
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
Find the minimum in an array min = A[0] for i = 1 to A.length – 1: if A[i] < min: min = A[i]
Find the minimum in an array What can we do with the minimum value?
Find the minimum in an array What can we do with the minimum value? Swap it with the value at index 0
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...
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!
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);
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);
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);
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);
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);
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);
Selection Sort Efficiency
Selection Sort Efficiency Better than Bubble Sort
Selection Sort Efficiency Better than Bubble Sort But rarely used
Insertion Sort Concept: Go through every element, insert it in it’s correct position in the sub-array before it
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Insertion Sort Exercise: Write the code by yourselves.
Insertion Sort Exercise: Write the code by yourselves. (Without looking it up online of course...) Try to challenge yourself
Insertion Sort Efficiency Also O(N2)
Insertion Sort Efficiency Also O(N2) But:
Insertion Sort Efficiency Also O(N2) But: Very fast for sorted/partially sorted arrays.
Insertion Sort Efficiency Also O(N2) But: Very fast for sorted/partially sorted arrays. Efficient for small data sets
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
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
Sorting None of the algorithms seen so far are “good enough”
Sorting None of the algorithms seen so far are “good enough” Especially Bubble and Selection
Sorting None of the algorithms seen so far are “good enough” Especially Bubble and Selection Merge Sort and QuickSort are the best algorithms
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
Merge Sort Concept: Divide a list equally into two
Merge Sort Concept: Divide a list equally into two Sort the two lists
Merge Sort Concept: Divide a list equally into two Sort the two lists Merge them
Merge Sort Concept: Divide a list equally into two Sort the two lists Merge them Repeat process recursively
Merge Sort i 1 2 3 4 5 A 55 19 100 45 87 33
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
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
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
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
Merge Sort 55 1 2 19 100
Merge Sort Already sorted Already Sorted 55 1 2 19 100
Merge Sort Already sorted Already Sorted Merge the two in the proper order: 55 1 2 19 100
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
Merge Sort 3 45 55 4 5 87 33 1 2 19 100
Merge Sort Already sorted Sort it: 3 45 55 4 5 87 33 1 2 19 100
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
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
Merge Sort 1 2 19 55 100 3 4 5 33 45 87
Merge Sort Both already sorted. 1 2 19 55 100 3 4 5 33 45 87
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
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
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
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
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
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
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
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
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
Merge Sort How to merge two sorted lists?
Merge Sort How to merge two sorted lists? Figure that out yourself
Merge Sort How to merge two sorted lists? Figure that out yourself Try to to do it on paper first
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?
Merge Sort Efficiency O(NlogN)
Merge Sort Efficiency O(NlogN) Much faster than the previous algorithms
Merge Sort Efficiency O(NlogN) Much faster than the previous algorithms Used in java.util.Arrays.sort(…)
Summary Sorting is a very important problem. Bubble, Selection and Insertion Sort. Insertion Sort great for small arrays. Merge Sort much faster than others