Download presentation
Presentation is loading. Please wait.
Published byCharity Howard Modified over 9 years ago
1
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 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
2
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 2 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.
3
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 3 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 call sort(A) returns: 179471022235 457910172223 457910172223
4
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 4 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, IntList L) { if (L == L.empty()) return false; else if (x == L.hd()) return true; else return find(x, L.tl()); }
5
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 5 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.
6
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 6 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.
7
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 7 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].
8
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 8 Selection sort (cont.) 179471022235 Starting state of A: After step 1: 491771022235 After step 2: 451771022239 After step 3: 457171022239 After step 4: 457910222317 After step 5: 457910222317 After step 6: 457910172322 After step 7: 457910172223
9
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 9 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 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] }
10
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 10 Selection sort (cont.) r 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); }
11
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 11 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; }
12
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 12 Insertion sort r 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.
13
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 13 Insertion sort (cont.) 179471022235 Starting state of A: After step 1: 917471022235 After step 2: 491771022235 After step 3: 479171022235 After step 4: 479101722235 After step 5: 479101722235 After step 6: 479101722235 After step 7: 457910172223
14
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 14 Insertion sort (cont.) 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] } r Basic algorithm: r As before, fill in the informal part with a method call:
15
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 15 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); }
16
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 16 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.