160 likes | 268 Views
Class 14 - Review: Sorting & searching. What are sorting and searching? Simple sorting algorithms Selection sort Insertion sort “Asymptotic” efficiency of algorithms Faster sorting algorithms Quicksort Mergesort. Sorting.
E N D
Class 14 - Review:Sorting & searching • What are sorting and searching? • Simple sorting algorithms • Selection sort • Insertion sort • “Asymptotic” efficiency of algorithms • Faster sorting algorithms • Quicksort • Mergesort
Sorting • Given array or list of values, and an ordering on the elements (e.g. “<“ on numbers, alphabetical ordering on string), put the elements of the array or list into increasing order. • In-place sorting: rearrange the elements without allocating a new array • Non-destructive sorting: create new array or list with the reordered elements.
Sorting (cont.) • In-place sort: void sort (double[] A)Suppose A = After calling sort(A),A = • Non-destructive sort:double[] sort (double[] A)If A is as above before call, then callsort(A) returns: 17 9 4 7 10 22 23 5 4 5 7 9 10 17 22 23 4 5 7 9 10 17 22 23
Searching • Given a set of elements (possibly with associated attributes, e.g. names with associated phone numbers), and given an element, find the element in the set. • E.g. we know how to find an element in a list: boolean find (int x, IntList L) { if (L == L.empty()) return false; else if (x == L.hd()) return true; else return find(x, L.tl()); }
Searching (cont.) • However, above method is not efficient enough for some applications (such as finding a phone number in an entire phone book). • Searching can be made more efficient by using different data structures to store the data. We will not talk about searching in today’s class.
Sorting algorithms • Many algorithms are known for sorting elements in a list or array. They include: • Selection sort • Insertion sort • Quicksort • Mergesort • Efficiency - that is, run time - can vary dramatically from one to another.
Selection sort • Idea is simple and intuitive, and easy to program. (We will assume we are sorting an array of length n, although the method could also be applied to sorting lists.) • Find smallest element among A[0]...A[n-1]. Suppose it is A[i]. Swap A[0] and A[i]. • Find smallest element among A[1]...A[n-1]. Suppose it is A[j]. Swap A[1] and A[j]. • Continue up to A[n-2]...A[n-1].
17 9 4 7 10 22 23 5 Selection sort (cont.) Starting state of A: After step 1: 4 9 17 7 10 22 23 5 After step 2: 4 5 17 7 10 22 23 9 After step 3: 4 5 7 17 10 22 23 9 After step 4: 4 5 7 9 10 22 23 17 After step 5: 4 5 7 9 10 22 23 17 After step 6: 4 5 7 9 10 17 23 22 After step 7: 4 5 7 9 10 17 22 23
Selection sort (cont.) • As with any array-processing method, can use iteration or recursion. For the moment, we will use iteration. Basic algorithm: void selectionSort (double[] A) { for (i=0; i<n-1; i++) { // Invariant: A[0]..A[i-1] is sorted, and // elements in A[0]..A[i-1] are all less than // elements in A[i]..A[n-1] minloc = ... location of smallest value in A[i]...A[n-1] ... swap A[i] with A[minloc] } }
Selection sort (cont.) • Fill in specific parts by making method calls; then define those methods. void selectionSort (double[] A) { int i, minloc; int n = A.length; for (i=0; i<n-1; i++) { // Invariant: A[0]..A[i-1] is sorted, and // elements in A[0]..A[i-1] are all less than // elements in A[i]..A[n-1] minloc = findMin(A, i); swap(A, i, minloc); } }
Selection sort (cont.) int findMin (double[] A, int i) { int j, min = i; for (j=i+1; j<A.length; j++) // A[min] <= all elements in A[i]..A[j-1] if (A[j] < A[min]) min = j; return min; } void swap (double[] A, int i, int j) { double temp = A[i]; A[i] = A[j]; A[j] = temp; }
Insertion sort • Like selection sort, insertion sort is an easy algorithm to understand and program. • Swap A[0] and A[1], if necessary, so that A[0]...A[1] is sorted. • Move A[2], if necessary, so that A[0]...A[2] is sorted. • Move A[3], if necessary, so that A[0]...A[3] is sorted. • Continue, finally moving A[n-1], if necessary, so that entire array is sorted.
Insertion sort (cont.) Starting state of A: 17 9 4 7 10 22 23 5 After step 1: 9 17 4 7 10 22 23 5 After step 2: 4 9 17 7 10 22 23 5 After step 3: 4 7 9 17 10 22 23 5 After step 4: 4 7 9 10 17 22 23 5 After step 5: 4 7 9 10 17 22 23 5 After step 6: 4 7 9 10 17 22 23 5 After step 7: 4 5 7 9 10 17 22 23
Insertion sort (cont.) • Basic algorithm: • As before, fill in the informal part with a method call: void insertionSort (double[] A) { for (i=1; i<n; i++) { // Invariant: A[0]..A[i-1] is sorted ... insert A[i] into correct location in A[0]... A[i] } }
Insertion sort (cont.) void insertionSort (double[] A) { int i, n = A.length; for (i=1; i<n; i++) { // Invariant: A[0]..A[i-1] is sorted insertInOrder(A[i], A, i); } }
Insertion sort (cont.) void insertInOrder(double x, double[] A, int hi) { int j = hi; while (j > 0 && x < A[j-1]) { // Invariant: the elements originally in // A[0]..A[hi-1] are now in A[0]..A[j-1] and // A[j+1]..A[hi], and x < A[j+1] A[j] = A[j-1]; j--; } A[j] = x; }