Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.

Similar presentations


Presentation on theme: "CS 1400 March 30, 2007 Chapter 8 Searching and Sorting."— Presentation transcript:

1 CS 1400 March 30, 2007 Chapter 8 Searching and Sorting

2 Linear Search… int LinearSearch (int list[], int size, int value) { bool found = false; int position = -1; for (int n=0; n<size && !found; n++) { if (list[n] == value) { position = n; found = true; } return position; }

3 Linear Search Advantages –easy to write –easy to understand –doesn’t require array elements to be in any particular order Disadvantages –very inefficient for large arrays (average search requires N/2 array comparisons)

4 Is there a better algorithm for searching? Binary SearchWhen array elements are in order, a Binary Search can be performed How do you search a phonebook for a given person’s listing?

5 Binary Search Pseudocode (assumes search item is in array) found = false position = -1 while search_item has not yet been found; set middle to halfway between the first and last index if array[middle] equals search_item item is at middle position!! else if array[middle] is less than search_item set first index to middle+1 else set last index to middle-1 end if end while

6 Binary Search function (assumes search item is in array) int BinarySearch (int array[ ], int first, int last, int search_item) { int middle; while (true) {middle = (first + last) / 2; if (array[middle] == search_item) return middle; else if (array[middle] < search_item) first = middle+1; else last = middle-1; }

7 Binary Search example: 134597.5 145686.2 197692.9 203476.5 248785.8 342273.4 455690.1 488793.1 512282.6 521996.0 int eaglenums[10]; float grades[10]; 01234567890123456789

8 cout << BinarySearch(eaglenums, 0, 9, 5122); cout << BinarySearch(eaglenums, 0, 9, 1456); cout << BinarySearch(eaglenums, 0, 9, 4556); int position = BinarySearch(eaglenums, 0, 9, 4556); cout << grades[position]; cout << BinarySearch(grades, 0, 9, 93.1); //???? What would be output?

9 Binary Search (1st refinement) Let’s move the return to the end of the function: int BinarySearch (int array[ ], int first, int last, int search_item) { bool found = false; int position = -1, middle; while (!found) {middle = (first + last) / 2; if (array[middle] == search_item) {position = middle; found = true; } else if (array[middle] < search_item) first = middle+1; else last = middle-1; } return position; }

10 Binary Search (2nd refinement) Let’s return -1 if the search_item isn’t found… int BinarySearch (int array[ ], int first, int last, int search_item) { bool found = false; int position = -1, middle; while (!found && first <= last) {middle = (first + last) / 2; if (array[middle] == search_item) {position = middle; found = true; } else if (array[middle] < search_item) first = middle+1; else last = middle-1; } return position; }

11 Binary Search (3rd refinement) Let’s simplify the parameters… int BinarySearch (int array[ ], int size, int search_item) { int first = 0, last = size-1; bool found = false; int position = -1, middle; while (!found && first <= last) {middle = (first + last) / 2; if (array[middle] == search_item) {position = middle; found = true; } else if (array[middle] < search_item) first = middle+1; else last = middle-1; } return position; }

12 Binary Search Advantages –very efficient (worst-cast search requires log 2 (N) array comparisons) Disadvantages –a bit more difficult to write and understand –requires the array to be ordered!

13 An overloaded BinarySearch() int BinarySearch (char array[ ][30], int size, char search_item[ ]) { int first = 0, last = size-1; bool found = false; int position = -1, middle; while (!found && first <= last) {middle = (first + last) / 2; if (strcmp(array[middle], search_item) == 0) {position = middle; found = true; } else if (strcmp(array[middle], search_item) < 0) first = middle+1; else last = middle-1; } return position; }

14 So, how does an array get ordered? void BubbleSort (int array[ ], int size); void BubbleSort (float array[ ], int size); void BubbleSort (char array[ ][30], int size);

15 Bubble sorting… Pseudocode: Repeatedly compare adjacent cells until array is in order a) if they are in order, do nothing b) if they are out of order, swap them

16 1 st Refinement an array is in order when no further swaps can be made: Set done to false While (not done) Set done to true Compare all adjacent cell pairs b) if they are out of order, swap them and set done to false end while

17 2 nd Refinement… bool done = false; while (!done) { done = true; Compare all adjacent cell pairs a) if they are out of order, swap them and set done to false }

18 3 rd Refinement If there are N cells in an array, there are N-1 adjacent pairs bool done = false; while (!done) { done = true; for (int n=0; n<N-1; n++) if (array[n] > array[n+1]) { Swap (array, n, n+1); done = false; }

19 As a function… void BubbleSort (int array[ ], int size) { bool done = false; while (!done) { done = true; for (int n=0; n<size-1; n++) if (array[n] > array[n+1]) { Swap (array, n, n+1); done = false; }

20 Swap() void Swap (int array[ ], int j, int k) { int temp; temp = array[j]; array[j] = array[k]; array[k] = temp; }

21 slight improvement… void BubbleSort (int array[ ], int size) { bool done = false; while (!done) { done = true; for (int n=0; n<size-1-n; n++) if (array[n] > array[n+1]) { Swap (array, n, n+1); done = false; } Each pass leaves the bottom value in the correct position

22 Selection Sort Advantages –somewhat more efficient than a Bubble Sort for large arrays Disadvantages –slightly more difficult to program and understand

23 Selection Sort pseudocode For (int n=0; n<N-1; n++) find the smallest value beginning at position n swap this value with element n

24 n=0 77 33 44 88 55 22 11 66 smallest 11 77 First Pass n=1 11 33 44 88 55 22 77 66 smallest 22 33 Second Pass

25 n=2 11 22 44 88 55 33 77 66 smallest 33 44 Third Pass n=3 11 22 33 88 55 44 77 66 smallest 44 88 Forth Pass

26 n=4 11 22 33 44 55 88 77 66 smallest Fifth Pass n=5 11 22 33 44 55 88 77 66 smallest 88 66 Sixth Pass

27 n=6 11 22 33 44 55 66 77 88 smallest Seventh Pass Since there are 8 elements in this array, only 8-1 passes are required.

28 Selection Sort void SelectionSort (int array[ ], int size) { int smallest_position; for (int n=0; n<size-1; n++) { // find the smallest in array beginning at position n smallest_position = FindSmallest (array, n, size); // swap the elements at positions n and smallest_position Swap (array, n, smallest_position); }

29 This is the same swapping function used for BubbleSort() void Swap (int array[ ], int j, int k) { int temp; temp = array[j]; array[j] = array[k]; array[k] = temp; }

30 int FindSmallest (int array[ ], int start, int size) { int position = start; int smallest = array[start];// first assumption for (int k=start+1; k<size; k++) if (array[k] < smallest) { smallest = array[k];// change your mind position = k;// and remember pos. } return position; } FindSmallest()

31 Sorting parallel arrays… Consider parallel arrays for student eagle numbers and grades: int eagles[100]; float grades[100]; 756496.5 536286.3 375691.0 …… 472575.9 eagles grades

32 Anytime we move an eagle number, we need to move the corresponding grade along with it – so that matching eagle numbers and grades are always in the same row…. 756496.5 536286.3 375691.0 …… 472575.9 eagles grades swap.. matching swap…

33 SelectionSort with parallel arrays… void SelectionSort (int array[ ], float array2[ ], int size) { int smallest_position; for (int n=0; n<size-1; n++) { smallest_position = FindSmallest (array1, n, size); Swap (array1, n, smallest_position); Swap (array2, n, smallest_position); } Naturally, Swap() would need to be overloaded to also handle float arrays…


Download ppt "CS 1400 March 30, 2007 Chapter 8 Searching and Sorting."

Similar presentations


Ads by Google