
11/27/12 Lecture outline bogo sort bubble sort selection sort CS202 Fall 2012 Lecture 11/27 insertion sort merge sort Sorting quick sort Prof. Tanya Berger-Wolf the quick select problem 1 2 Sorting Sorting = putting objects in order in array/list one of the fundamental problems in computer science comparison-based sorting: must determine order through comparison operations on the input data: <, >, compareTo, … 1. Bogo sort 15 2 8 1 17 10 12 5 0 1 2 3 4 5 6 7 4 1 11/27/12 Bogo sort Bogo sort code bogo sort: orders a list of values by repetitively shuffling them and public static void bogoSort(int[] a) { while (!isSorted(a)) { checking if they are sorted shuffle(a); } more specifically: } scan the list, seeing if it is sorted // Returns true if array a's elements if not, shuffle the values in the list and repeat // are in sorted order. public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { This sorting algorithm has terrible performance! if (a[i] > a[i+1]) { Can we deduce its runtime? return false; } } return true; } 5 6 Bogo sort code, helpers Bogo sort runtime // Shuffles an array of ints by randomly swapping each // element with an element ahead of it in the array. How long should we expect bogo sort to take? public static void shuffle(int[] a) { related to probability of shuffling into sorted order for (int i = 0; i < a.length - 1; i++) { // pick random number in [i+1, a.length-1] assuming shuffling code is fair, probability of having sorted equals inclusive int range = a.length-1 - (i + 1) + 1; 1 / (number of permutations of n elements) int j = (int)(Math.random() * range + (i + 1)); swap(a, i, j); } } // Swaps a[i] with a[j]. private static void swap(int[] a, int i, int j) { if (i == j) return; int temp = a[i]; bogo sort takes roughly factorial time to run a[i] = a[j]; – note that if array is initially sorted, bogo finishes quickly! a[j] = temp; } it should be clear that this is not satisfactory... 7 8 2 11/27/12 Bubble sort bubble sort: orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary more specifically: scan the list, exchanging adjacent elements if they are not in 2. Bubble sort relative order; this bubbles the highest value to the top scan the list again, bubbling up the second highest value repeat until all elements have been placed in their proper order 9 10 "Bubbling" largest element "Bubbling" largest element Traverse a collection of elements Traverse a collection of elements Move from the front to the end Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons "Bubble" the largest value to the end using pair-wise comparisons and swapping and swapping 1 2 3 4 5 6 1 2 3 4 5 6 Swap 7742 4277 35 12 101 5 42 7735 Swap 3577 12 101 5 11 12 3 11/27/12 "Bubbling" largest element "Bubbling" largest element Traverse a collection of elements Traverse a collection of elements Move from the front to the end Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons "Bubble" the largest value to the end using pair-wise comparisons and swapping and swapping 1 2 3 4 5 6 1 2 3 4 5 6 Swap 42 35 7712 1277 101 5 42 35 12 77 101 5 No need to swap 13 14 "Bubbling" largest element "Bubbling" largest element Traverse a collection of elements Traverse a collection of elements Move from the front to the end Move from the front to the end "Bubble" the largest value to the end using pair-wise comparisons "Bubble" the largest value to the end using pair-wise comparisons and swapping and swapping 1 2 3 4 5 6 1 2 3 4 5 6 42 35 12 77 1015 Swap 101 5 42 35 12 77 5 101 Largest value correctly placed 15 16 4 11/27/12 Bubble sort code Bubble sort runtime public static void bubbleSort(int[] a) { Running time (# comparisons) for input size n: for (int i = 0; i < a.length; i++) { for (int j = 1; j < a.length - i; j++) { // swap adjacent out-of-order elements if (a[j-1] > a[j]) { swap(a, j-1, j); } } } } number of actual swaps performed depends on the data; out-of- order data performs many swaps 17 18 Selection sort selection sort: orders a list of values by repetitively putting a particular value into its final position more specifically: find the smallest value in the list 3. Selection sort switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places 19 20 5 11/27/12 Selection sort example Selection sort example 2 Index 0 1 2 3 4 5 6 7 Value 27 63 1 72 64 58 14 9 1st pass 1 63 27 72 64 58 14 9 2nd pass 1 9 27 72 64 58 14 63 3rd pass 1 9 14 72 64 58 27 63 … 21 22 Selection sort code Selection sort runtime public static void selectionSort(int[] a) { Running time for input size n: for (int i = 0; i < a.length; i++) { // find index of smallest element in practice, a bit faster than bubble sort. Why? int min = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } } // swap smallest element with a[i] swap(a, i, min); } } 23 24 6 11/27/12 Insertion sort insertion sort: orders a list of values by repetitively inserting a particular value into a sorted subset of the list more specifically: consider the first item to be a sorted sublist of length 1 4. Insertion sort insert the second item into the sorted sublist, shifting the first item if needed insert the third item into the sorted sublist, shifting the other items as needed repeat until all values have been inserted into their proper positions 25 26 Insertion sort Insertion sort example Simple sorting algorithm. n-1 passes over the array At the end of pass i, the elements that occupied A[0]…A[i] originally are still in those spots and in sorted order. 2 15 8 1 17 10 12 5 0 1 2 3 4 5 6 7 after 2 8 15 1 17 10 12 5 pass 2 0 1 2 3 4 5 6 7 1 2 8 15 17 10 12 5 after 0 1 2 3 4 5 6 7 pass 3 27 28 7 11/27/12 Insertion sort example 2 Insertion sort example 2 Names: Fred, Alice, David, Bill, and Carol 29 30 Insertion sort example 2 Worst-case for insertion sort 31 32 8 11/27/12 Insertion sort code Insertion sort runtime public static void insertionSort(int[] a) { worst case: reverse-ordered elements in array. for (int i = 1; i < a.length; i++) { int temp = a[i]; // slide elements down to make room for a[i] int j = i; while (j > 0 && a[j - 1] > temp) { best case: array is in sorted ascending order. a[j] = a[j - 1]; j--; } a[j] = temp; } average case: each element is about halfway in order. } 33 34 Average case analysis Comparing sorts We've seen "simple" sorting algorithms so far, such as: Given an array A of elements, an inversion is an ordered pair (i, j) such selection sort that i < j, but insertion sort A[i] > A[j]. (out of order elements) Assume no duplicate elements. comparisons swaps Theorem: The average number of inversions in an array of n distinct selection n2/2 n elements is n (n - 1) / 4. Corollary: Any algorithm that sorts by exchanging adjacent elements worst: n2/2 worst: n2/2 requires O(n2) time on average. insertion best: n best: 0 They all use nested loops and perform approximately n2 comparisons They are relatively inefficient 35 36 9 11/27/12 CS202 Fall 2012 5. Merge sort Lecture Sorting Prof. Tanya Berger-Wolf 38 http://xkcd.com/584/ 37 Merge sort Merge sort merge sort: orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining Merge sort idea: Invented by John von Neumann in 1945 Divide the array into two halves. Recursively sort the two halves (using merge sort). Use merge to combine the two arrays. more specifically: divide the list into two roughly equal parts mergeSort(0, n/2-1) mergeSort(n/2, n-1) recursively divide each part in half, continuing until a part contains only one element merge the two parts into one sorted list sort sort continue to merge parts as the recursion unfolds merge(0, n/2, n-1) This is a "divide and conquer" algorithm. 39 40 10 11/27/12 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 41 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 98 23 43 44 11 11/27/12 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 98 23 98 23 23 Merge Merge 45 46 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 98 23 98 23 45 14 23 98 23 98 Merge 47 48 12 11/27/12 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 98 23 45 14 98 23 45 14 23 98 23 98 14 Merge Merge 49 50 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 98 23 45 14 98 23 45 14 23 98 14 45 23 98 14 45 Merge Merge 51 52 13 11/27/12 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 98 23 45 14 98 23 45 14 23 98 14 45 23 98 14 45 14 14 23 Merge Merge 53 54 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 98 23 45 14 98 23 45 14 23 98 14 45 23 98 14 45 14 23 45 14 23 45 98 Merge Merge 55 56 14 11/27/12 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 6 67 23 98 14 45 23 98 14 45 14 23 45 98 14 23 45 98 57 58 98 23 45 14 6
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages27 Page
-
File Size-