Structural Programming Course Content and Data Structures • Introduction • Vectors • Objects • Testing/Debugging Winter 2000 • Methods • Arrays • Tracing Programs • Searching CMPUT 102: Sorting • Object State • Files I/O • Sharing resources • Sorting Dr. Osmar R. Zaïane • Selection • Inheritance • Repetition • Recursion University of Alberta Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 1 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 2 2 Objectives of Lecture 23 Outline of Lecture 23 Sorting Sorting • The sorting problem • Introduce the problem of sorting collections; • Simple methods like bubble sort • Learn how to sort using a bubble sort • Selection sort example algorithm; • Selection sort code • Learn how to sort with the selection algorithm. • Complexity of selection sort Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 3 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 4 The Sort Problem Sorting Problem (con’t) • Given a container, with elements that can be • Given a container of n elements A[ 0 .. n-1 ] such compared, put it in increasing or decreasing that any elements x and y in the container A order. can be compared directly, either x<y, or x=y, or x>y. 0 1 2 3 4 5 6 7 8 9 25 50 10 95 75 30 70 55 60 80 • We want to permute the elements of A so that at the end A[ 0 ] ≤ A[ 1 ] ≤ … ≤ A[ n-1 ] (monotone non-decreasing), or A[ 0 ] ≥ A[ 1 ] ≥ … ≥ A[ n-1 ] 0 1 2 3 4 5 6 7 8 9 10 25 30 50 55 60 70 75 80 95 (monotone decreasing) Dr. Osmar R. Zaïane, 2000 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 5 Structural Programming and Data Structures University of Alberta 6 1
The Order of Things Sorting • Numbers • There is often a need to put data in order. � -99 < -34< -6< 0 < 1 < 9 < 23 < 999 • Sorting is among the most basic and • Characters universal of computational problems. � A < B < C < D < E < F < …< X < Y < Z • There are hundreds of algorithms and � a < b < c < d < e < f < …< x < y < z variations on algorithms. � a < z < A < Z • Variety of sorting methods: internal vs. • Strings external, sorting in place vs. sorting with � “Abacus” < “Alpha” < “Hello” < “Memorization” auxiliary structures, etc. < “Memorize” < “Memory” < “Zebra” Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 7 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 8 One simple sorting method 35 18 22 97 61 10 Given a list: Outline of Lecture 23 Iterate over the collection and permute neighbours if necessary repeat iteration until no permutation possible. • The sorting problem 18 35 22 97 61 10 18 22 35 61 10 97 • Simple methods like bubble sort … • Selection sort example 18 22 35 97 61 10 18 22 35 10 61 97 • Selection sort code … • Complexity of selection sort 18 22 35 97 61 10 10 18 22 35 61 97 … 18 22 35 61 97 10 10 18 22 35 61 97 ... Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 9 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 10 The Bubble Sort 35 18 22 97 61 10 Given a list: public static void bubble_sort( int data[] ) { First pass of (compare and exchange) will send the largest to the last position. // Sort the given Array with selection sort method //(Ascending order) 35 18 22 97 61 10 18 22 35 61 10 97 ... … 18 35 22 97 61 10 int current, last; 18 22 35 10 61 97 ... 18 22 35 97 61 10 for ( last = data.length-1; last >=1; last--) 18 22 35 10 61 97 for ( current = 0; current < last; current++ ) ... 18 22 35 97 61 10 … if ( data[current] > data[current+1] ) this.exchange( data, current, current+1 ); 18 22 35 61 97 10 18 22 10 35 61 97 } … 10 18 22 35 61 97 18 22 35 61 10 97 ... Dr. Osmar R. Zaïane, 2000 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 11 Structural Programming and Data Structures University of Alberta 12 2
Outline of Lecture 23 Selection Sort • The sorting problem • Look for the smallest element and exchange • Simple methods like bubble sort it with the element whose index is 0. • Selection sort example • Selection sort code 0 1 2 3 4 5 6 7 8 9 25 50 10 95 75 30 70 55 60 80 • Complexity of selection sort 0 1 2 3 4 5 6 7 8 9 10 50 25 95 75 30 70 55 60 80 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 13 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 14 Selection Sort (con’t) Selection Sort (con’t) • Look for the smallest element whose index • Look for the smallest element whose index is greater than or equal to 1 and exchange it is greater than or equal to 2 and exchange it with the element whose index is 1. with the element whose index is 2. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 50 25 95 75 30 70 55 60 80 10 25 50 95 75 30 70 55 60 80 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 25 30 95 75 50 70 55 60 80 10 25 50 95 75 30 70 55 60 80 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 15 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 16 Selection Sort (con’t) Selection Sort (con’t) • Look for the smallest element whose index is greater than or equal to k and exchange it 0 1 2 3 4 5 6 7 8 9 with the element whose index is k (for k = 10 25 30 50 75 95 70 55 60 80 3, 4, …, n-1) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 25 30 50 55 95 70 75 60 80 10 25 30 95 75 50 70 55 60 80 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 25 30 50 55 60 70 75 95 80 10 25 30 50 75 95 70 55 60 80 Dr. Osmar R. Zaïane, 2000 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 17 Structural Programming and Data Structures University of Alberta 18 3
Selection Sort (con’t) Outline of Lecture 23 • The sorting problem 0 1 2 3 4 5 6 7 8 9 10 25 30 50 55 60 70 75 95 80 • Simple methods like bubble sort • Selection sort example 0 1 2 3 4 5 6 7 8 9 • Selection sort code 10 25 30 50 55 60 70 75 95 80 • Complexity of selection sort 0 1 2 3 4 5 6 7 8 9 10 25 30 50 55 60 70 75 80 95 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 19 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 20 Selection Sort Algorithm Selection Sort Code INPUT : data: an array of int private void selectionSort(int anArray[]) { OUTPUT : data: sorted in ascending order // Sort the given Array with selection sort method (Ascending order) Method : int index; for ( first = 1; first < length - 1; first ++) { int smallIndex; find Smallest such that data[Smallest] is the for (index = 0; index < anArray.length - 1; index++) { smallest between data[first] and data[length-1]; smallIndex = this.getSmallest(anArray, index); permute Data[first] and Data[Smallest]; this.exchange(anArray, index, smallIndex); } } } Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 21 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 22 Code for method: getSmallest Code for method: exchange private int getSmallest(int anArray[], int start) { // Return the index of the smallest element private void exchange(int anArray[], int i, int j) { // of the given array whose index is greater // Exchange the elements of the array with // than or equal to the given start index. // the given two indexes. int smallestIndex; int index; int temp; smallestIndex = start; temp = anArray[i]; for (index = start + 1; index < anArray.length; index++) anArray[i] = anArray[j]; if (anArray[index] < anArray[smallestIndex]) anArray[j] = temp; smallestIndex = index; } return smallestIndex; } Dr. Osmar R. Zaïane, 2000 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 23 Structural Programming and Data Structures University of Alberta 24 4
Complexity of Selection Sort Outline of Lecture 23 • How many comparison operations are required for a selection sort of an n -element container? • The sorting problem • The sort method executes getSmallest for the • Simple methods like bubble sort indexes: 0, 1, … n-2 . • Selection sort example • Each time getSmallest is executed for an index, it does: ( n - index) comparisons. • Selection sort code • The total number of comparisons is: • Complexity of selection sort ( n-0 ) + ( n-1 ) + … + ( n- ( n-2 )) = ( 1 + 2 + …+ n ) - 1 = n ( n + 1 ) - 1 ≈ n 2 for large n . 2 O( n 2 ) � Quadratic time complexity Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 25 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 26 5
Recommend
More recommend