Class 14 Review Sorting searching r What are

  • Slides: 16
Download presentation
Class 14 - Review: Sorting & searching r What are sorting and searching? r

Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms m Selection sort m Insertion sort r “Asymptotic” efficiency of algorithms r Faster sorting algorithms m Quicksort m Mergesort 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 1

Sorting r Given array or list of values, and an ordering on the elements

Sorting r 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. m In-place sorting: rearrange the elements without allocating a new array m Non-destructive sorting: create new array or list with the reordered elements. 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 2

Sorting (cont. ) r In-place sort: void sort (double[] A) Suppose A = 17

Sorting (cont. ) r In-place sort: void sort (double[] A) Suppose A = 17 9 4 7 10 22 23 5 After calling sort(A), A= 4 5 7 9 10 17 22 23 r Non-destructive sort: double[] sort (double[] A) If A is as above before call, then call sort(A) returns: 4 5 7 9 10 17 22 23 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 3

Searching r Given a set of elements (possibly with associated attributes, e. g. names

Searching r 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. r E. g. we know how to find an element in a list: boolean find (int x, Int. List L) { if (L == L. empty()) return false; else if (x == L. hd()) return true; else return find(x, L. tl()); } 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 4

Searching (cont. ) r However, above method is not efficient enough for some applications

Searching (cont. ) r However, above method is not efficient enough for some applications (such as finding a phone number in an entire phone book). r 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. 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 5

Sorting algorithms r Many algorithms are known for sorting elements in a list or

Sorting algorithms r Many algorithms are known for sorting elements in a list or array. They include: m Selection sort m Insertion sort m Quicksort m Mergesort r Efficiency - that is, run time - can vary dramatically from one to another. 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 6

Selection sort r Idea is simple and intuitive, and easy to program. (We will

Selection sort r 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]. 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 7

Selection sort (cont. ) Starting state of A: 17 9 4 7 10 22

Selection sort (cont. ) Starting state of A: 17 9 4 7 10 22 23 5 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 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 8

Selection sort (cont. ) r As with any array-processing method, can use iteration or

Selection sort (cont. ) r As with any array-processing method, can use iteration or recursion. For the moment, we will use iteration. Basic algorithm: void selection. Sort (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] } } 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 9

Selection sort (cont. ) r Fill in specific parts by making method calls; then

Selection sort (cont. ) r Fill in specific parts by making method calls; then define those methods. void selection. Sort (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 = find. Min(A, i); swap(A, i, minloc); } } 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 10

Selection sort (cont. ) int find. Min (double[] A, int i) { int j,

Selection sort (cont. ) int find. Min (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; } 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 11

Insertion sort r Like selection sort, insertion sort is an easy algorithm to understand

Insertion sort r Like selection sort, insertion sort is an easy algorithm to understand 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. 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 12

Insertion sort (cont. ) Starting state of A: 17 9 4 7 10 22

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 4 5 7 9 10 17 22 23 After step 7: 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 13

Insertion sort (cont. ) r Basic algorithm: void insertion. Sort (double[] A) { for

Insertion sort (cont. ) r Basic algorithm: void insertion. Sort (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] } } r As before, fill in the informal part with a method call: 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 14

Insertion sort (cont. ) void insertion. Sort (double[] A) { int i, n =

Insertion sort (cont. ) void insertion. Sort (double[] A) { int i, n = A. length; for (i=1; i<n; i++) { // Invariant: A[0]. . A[i-1] is sorted insert. In. Order(A[i], A, i); } } 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 15

Insertion sort (cont. ) void insert. In. Order(double x, double[] A, int hi) {

Insertion sort (cont. ) void insert. In. Order(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; } 21/3/00 SEM 107 - © Kamin & Reddy Class 14 - Sorting - 16