1.37k likes | 1.55k Views
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.
E N D
COP 3503 FALL 2012ShayanJavedLecture 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:
Sorting • Compare values at indices 0 and 1 first.
Sorting • Compare values at indices 0 and 1 first.
Sorting • Compare values at indices 0 and 1 first. • Swap if A[1] < A[0]:
Sorting • Compare values at indices 1 and 2.
Sorting • Compare values at indices 1 and 2. • Is A[2] < A[1]? No:
Sorting • Compare values at indices 2 and 3.
Sorting • Compare values at indices 2 and 3. • Is A[3] < A[2]? Yes! Swap.
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:
Sorting • Largest value in the array is now at the end of the array. • Result:
Sorting • What do we do next?
Sorting • What do we do next? • Repeat the process.
Sorting • What do we do next? • Repeat the process. • Result:
Sorting • Have the second largest value at index N - 2 • Result:
Sorting • Have the second largest value at index N - 2 • How many more times to sort? • Result:
Sorting • Have the second largest value at index N - 2 • How many more times to sort? • Total of N times • Result:
Sorting • Have the second largest value at index N - 2 • How many more times to sort? • Total of Ntimes • Result:
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(inti = 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(inti = 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(inti = 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(inti = 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(inti = 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.
Bubble Sort Efficiency • Let’s look at one pass. • To:
Bubble Sort Efficiency • Let’s look at one pass. • To: • What’s the efficiency of this pass?
Bubble Sort Efficiency • Let’s look at one pass. • To: • What’s the efficiency of this pass? O(N)
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(inti = 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(inti = 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...