1 / 17

Sorting and Searching

Sorting and Searching. Problem. Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers into ascending order. Finally, print the numbers in sorted order. The Linear or Selection Sort.

halden
Download Presentation

Sorting and Searching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting and Searching

  2. Problem • Read in a parameter value n, then read in a set of n numbers. • Print the numbers in their original order. • Sort the numbers into ascending order. • Finally, print the numbers in sorted order.

  3. The Linear or Selection Sort • Initialize an array with the numbers to be sorted • Hold the leftmost position (index 0) and compare the value in that position to each of the values in the other positions, one at a time • When the value in position 0 is greater than the compared value, swap the numbers Pass 0 (0-1) 4523 18 0 (45,23) swap (0-2) 23 45 18 0 (23,18) swap (0-3) 18 45 23 0 (18,0) swap 0 45 23 18 • After this pass, the smallest number is in the leftmost position. • Now fix the leftmost position plus 1 (index 1) and compare the value in that position to each of the other positions, one at a time • When the value in position 1 is greater than the compared value, swap the numbers • Repeat this process for index 2, 3 , etc until the next to last index • When completed, the number will be sorted in ascending order

  4. The Linear or Selection Sort for each position in the array (except the last) for each candidate for that position, compare the candidate to the element currently in that position if the candidate is smaller swap them /* Function linearsort() * Input: * numb - array to be sorted * n - number of elements to sort * Process: linear sort * Output: numb sorted into ascending order */ void linearsort(int numb[], int n) { int temp; for (int pass = 0; pass < (n-1); pass++) for (int cand = (pass+1); cand < n; cand++) if (numb[pass] > numb[cand]) { temp = numb[pass]; numb[pass] = numb[cand]; numb[cand] = temp; } return; }

  5. Tracing the Selection Sort void linearsort(int numb[], int n) { int temp; for (int pass = 0; pass < (n-1); pass++) for (int cand = (pass+1); cand < n; cand++) if (numb[pass] > numb[cand]) { temp = numb[pass]; numb[pass] = numb[cand]; numb[cand] = temp; cout<<"pass "<<pass<<" cand "<<cand<<": "; printArray(numb,n); } return; } #include <iostream> #include <fstream> using namespace std; void linearsort(int[], int); void printArray(int [], int); int main(){ int numb[]={98, 23, 13, 2, 0, 210, 45, 87, 65, 23, 100, -1}; printArray(numb,12); linearsort(numb,12); printArray(numb,12); system("pause"); return 0; } void printArray(int numb[], int n) { for (int i=0; i<n; i++) cout<<numb[i]<<" "; cout<<endl; }

  6. The Linear or Selection Sort pass 6 cand 7: -1 0 2 13 23 23 98 210 87 65 100 45 pass 6 cand 8: -1 0 2 13 23 23 87 210 98 65 100 45 pass 6 cand 9: -1 0 2 13 23 23 65 210 98 87 100 45 pass 6 cand 11: -1 0 2 13 23 23 45 210 98 87 100 65 pass 7 cand 8: -1 0 2 13 23 23 45 98 210 87 100 65 pass 7 cand 9: -1 0 2 13 23 23 45 87 210 98 100 65 pass 7 cand 11: -1 0 2 13 23 23 45 65 210 98 100 87 pass 8 cand 9: -1 0 2 13 23 23 45 65 98 210 100 87 pass 8 cand 11: -1 0 2 13 23 23 45 65 87 210 100 98 pass 9 cand 10: -1 0 2 13 23 23 45 65 87 100 210 98 pass 9 cand 11: -1 0 2 13 23 23 45 65 87 98 210 100 pass 10 cand 11: -1 0 2 13 23 23 45 65 87 98 100 210 -1 0 2 13 23 23 45 65 87 98 100 210 98 23 13 2 0 210 45 87 65 23 100 -1 pass 0 cand 1: 23 98 13 2 0 210 45 87 65 23 100 -1 pass 0 cand 2: 13 98 23 2 0 210 45 87 65 23 100 -1 pass 0 cand 3: 2 98 23 13 0 210 45 87 65 23 100 -1 pass 0 cand 4: 0 98 23 13 2 210 45 87 65 23 100 -1 pass 0 cand 11: -1 98 23 13 2 210 45 87 65 23 100 0 pass 1 cand 2: -1 23 98 13 2 210 45 87 65 23 100 0 pass 1 cand 3: -1 13 98 23 2 210 45 87 65 23 100 0 pass 1 cand 4: -1 2 98 23 13 210 45 87 65 23 100 0 pass 1 cand 11: -1 0 98 23 13 210 45 87 65 23 100 2 pass 2 cand 3: -1 0 23 98 13 210 45 87 65 23 100 2 pass 2 cand 4: -1 0 13 98 23 210 45 87 65 23 100 2 pass 2 cand 11: -1 0 2 98 23 210 45 87 65 23 100 13 pass 3 cand 4: -1 0 2 23 98 210 45 87 65 23 100 13 pass 3 cand 11: -1 0 2 13 98 210 45 87 65 23 100 23 pass 4 cand 6: -1 0 2 13 45 210 98 87 65 23 100 23 pass 4 cand 9: -1 0 2 13 23 210 98 87 65 45 100 23 pass 5 cand 6: -1 0 2 13 23 98 210 87 65 45 100 23 pass 5 cand 7: -1 0 2 13 23 87 210 98 65 45 100 23 pass 5 cand 8: -1 0 2 13 23 65 210 98 87 45 100 23 pass 5 cand 9: -1 0 2 13 23 45 210 98 87 65 100 23 pass 5 cand 11: -1 0 2 13 23 23 210 98 87 65 100 45

  7. The Bubble Sort • Compares adjacent pairs of numbers instead of fixing a position and comparing the candidates to it as in the linear sort • After the first pass, the largest number is in the rightmost position • After each subsequent pass, the largest number remaining is in the rightmost position that has not been already fixed

  8. The Bubble Sort const int TRUE = 1; const int FALSE = 0; void bubblesort(int numb[], int n) { int swapped; int temp; do { swapped = FALSE; // initialize swapped to FALSE for (int pos = 0; pos < (n-1); pos++) if (numb[pos] > numb[pos+1]) { temp = numb[pos]; numb[pos] = numb[pos+1]; numb[pos+1] = temp; // indicate swap has occurred swapped = TRUE; } } while (swapped); return; } do the following as long as there has been a swap on the last pass for each element of the array compare the element to its neighbor if they are out of order swap them /* Function bubblesort() * Input: * numb - array to be sorted * n - number of elements to sort * Process: * bubble sort * Output: * numb sorted into ascending order */ See http://www.ece.unb.ca/brp/lib/java/bubblesort/

  9. The Bubble Sort const int TRUE = 1; const int FALSE = 0; void bubblesort(int numb[], int n) { int swapped; int temp; do { swapped = FALSE; // initialize swapped to FALSE for (int pos = 0; pos < (n-1); pos++) if (numb[pos] > numb[pos+1]) { temp = numb[pos]; numb[pos] = numb[pos+1]; numb[pos+1] = temp; // indicate swap has occurred swapped = TRUE; } } while (swapped); return; } 98 23 13 2 0 210 45 87 65 23 100 -1 pos 0: 23 98 13 2 0 210 45 87 65 23 100 -1 pos 1: 23 13 98 2 0 210 45 87 65 23 100 -1 pos 2: 23 13 2 98 0 210 45 87 65 23 100 -1 pos 3: 23 13 2 0 98 210 45 87 65 23 100 -1 pos 5: 23 13 2 0 98 45 210 87 65 23 100 -1 pos 6: 23 13 2 0 98 45 87 210 65 23 100 -1 pos 7: 23 13 2 0 98 45 87 65 210 23 100 -1 pos 8: 23 13 2 0 98 45 87 65 23 210 100 -1 pos 9: 23 13 2 0 98 45 87 65 23 100 210 -1 pos 10: 23 13 2 0 98 45 87 65 23 100 -1 210 pos 0: 13 23 2 0 98 45 87 65 23 100 -1 210 pos 1: 13 2 23 0 98 45 87 65 23 100 -1 210 pos 2: 13 2 0 23 98 45 87 65 23 100 -1 210 pos 4: 13 2 0 23 45 98 87 65 23 100 -1 210 pos 5: 13 2 0 23 45 87 98 65 23 100 -1 210 pos 6: 13 2 0 23 45 87 65 98 23 100 -1 210 pos 7: 13 2 0 23 45 87 65 23 98 100 -1 210 pos 9: 13 2 0 23 45 87 65 23 98 -1 100 210 pos 0: 2 13 0 23 45 87 65 23 98 -1 100 210 pos 1: 2 0 13 23 45 87 65 23 98 -1 100 210 pos 5: 2 0 13 23 45 65 87 23 98 -1 100 210 pos 6: 2 0 13 23 45 65 23 87 98 -1 100 210 pos 8: 2 0 13 23 45 65 23 87 -1 98 100 210 pos 0: 0 2 13 23 45 65 23 87 -1 98 100 210 pos 5: 0 2 13 23 45 23 65 87 -1 98 100 210 pos 7: 0 2 13 23 45 23 65 -1 87 98 100 210 pos 4: 0 2 13 23 23 45 65 -1 87 98 100 210 pos 6: 0 2 13 23 23 45 -1 65 87 98 100 210 pos 5: 0 2 13 23 23 -1 45 65 87 98 100 210 pos 4: 0 2 13 23 -1 23 45 65 87 98 100 210 pos 3: 0 2 13 -1 23 23 45 65 87 98 100 210 pos 2: 0 2 -1 13 23 23 45 65 87 98 100 210 pos 1: 0 -1 2 13 23 23 45 65 87 98 100 210 pos 0: -1 0 2 13 23 23 45 65 87 98 100 210 -1 0 2 13 23 23 45 65 87 98 100 210

  10. Sorting an Array of Strings Original Array Jones Harrow Tenenbaum Arnow Raphan Sorted Array Arnow Harrow Jones Raphan Tenenbaum #include <iostream> #include <string> using namespace std; const int SIZE = 5; int main() { string str[SIZE]; string tempstr; str[0] = "Jones"; str[1] = "Harrow"; str[2] = "Tenenbaum"; str[3] = "Arnow"; str[4] = "Raphan"; //print the original array cout << "Original Array" << endl; for (int i = 0; i < SIZE; i++) cout << str[i] << endl; // sort the array for (int pos = 0; pos < SIZE-1; pos++) for (int cand = pos+1; cand < SIZE; cand++) if (str[cand] < str[pos]) { tempstr = str[cand]; str[cand] = str[pos]; str[pos] = tempstr; } //print the sorted array cout << endl << "Sorted Array" << endl; for (int i = 0; i < SIZE; i++) cout << str[i] << endl; return 0; }

  11. Sorting an Array of Strings in a Function void sortthearray(string str[]) { string tempstr; // sort the array for (int pos = 0; pos < SIZE-1; pos++) for (int cand = pos+1; cand < SIZE; cand++) if (str[cand] < str[pos]) { tempstr = str[cand]; str[cand] = str[pos]; str[pos] = tempstr; } return; } cout << "Original Array" << endl; for (int i = 0; i < SIZE; i++) cout << str[i] << endl; // sort the array sortthearray(str); //print the sorted array cout << endl << "Sorted Array" << endl; for (int i = 0; i < SIZE; i++) cout << str[i] << endl;

  12. Searching • Problem: • We have an array of values: 23 -65 89 99 76 -56 95 • Search for 99 and 77 • If found, return the position of the value within the array • If not found, return -1

  13. Linear Search • for each position in the array • compare the element in that position to the search value • if they are equal • return the position in the array • if no element is equal to the search value • return a failure signal (-1) /* Function linearsearch() * Input: * numb - array to be searched * n - number of elements in the array * searchvalue - the value to search for * Process: * linear search * Output: * returns the position of searchvalue in the array * returns -1 if searchvalue not found */ int linearsearch(int numb[], int n, int searchnumber) { for (int position = 0; position < n; position++) if (numb[position] == searchnumber) return (position); // search value found return (-1); // search value not found }

  14. Binary Search of Sorted Array • Pseudocode for Binary Search: low = 0; high = n-1; while (low <= high) look at the element halfway between low and high if the element equals the search value return its position else if the element is larger than the search value high = the tested position - 1 else low = tested position + 1 • NOTE: To do a binary search of an array, the numbers must be in sorted order

  15. Binary Search of Sorted Array • For example, consider the following sequence of integers sorted in ascending order and say we are looking for the number 55: 0 5 13 19 22 41 55 68 72 81 98Initially, the search space contains indices 1 through 11. • Store two numbers, the low and high indices. • Choose the median value, which is the value at index 6 (the midpoint between 1 and 11): this value is 41 and it is smaller than the target value. • From this we conclude not only that the element at index 6 is not the target value, but also that no element at indices between 1 and 5 can be the target value, because all elements at these indices are smaller than 41, which is smaller than the target value. • This brings the search space down to indices 7 through 11: 55 68 72 81 98Proceeding in a similar fashion, we chop off the second half of the search space and are left with: 55 68Depending on how we choose the median of an even number of elements we will either find 55 in the next step or chop off 68 to get a search space of only one element. Either way, we conclude that the index where the target value is located is 7.

  16. Comparison of Search Algorithms

  17. Binary Search of Sorted Array /* Function binarysearch() * Input: * numb - array to be searched * n - number of elements in the array * searchvalue - the value to search for * Process: binary search * Output: * returns the position of searchvalue in the array * returns -1 if searchvalue not found */ int binarysearch(int numb[], int n, int searchnumber) { int low,high,test; low = 0; high = n - 1; while (low <= high) { test = (low + high) / 2; if (numb[test] == searchnumber) return (test); // search value found else if (numb[test] > searchnumber) high = test - 1; else low = test + 1; } return (-1); // search value not found }

More Related