html5-img
1 / 46

Searching and Sorting 1-D Arrays

Searching and Sorting 1-D Arrays. Linear Search Binary Search Selection Sort. Searching. Scan a collection of data looking for a particular value retrieve the information modify/update it print it delete it locate related information etc. Searching: examples. Using the Library System

elissaj
Télécharger la présentation

Searching and Sorting 1-D Arrays

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. Searching and Sorting 1-D Arrays • Linear Search • Binary Search • Selection Sort

  2. Searching • Scan a collection of data looking for a particular value • retrieve the information • modify/update it • print it • delete it • locate related information • etc.

  3. Searching: examples • Using the Library System • look for a book with title "Pride and Prejudice" • which library has it? more than one? • are any copies available? where? • what is the catalog number? etc. • On the Internet • search for all sites which refer to "job hunting" • Local phone book: look up a name

  4. Area Codes and Corresponding Locations • Two parallel arrays of size N • one--area codes • other--corresponding locations • find area code in 1st array • use the index to find location in 2nd array • We will consider two search algorithms • linear search • binary search

  5.         202 Wash. D.C. areaCodes [0] locations [0] 203 Connecticut areaCodes [1] 802 Vermont areaCodes [N-1] locations [N-1] areaCodes (int) locations (string)

  6. Linear Search: Brute Force Linear search is like paging through a book: • look at 1st value and test for match • if match, stop, otherwise… • look at 2nd value and test for match • if match, stop, otherwise • look at 3rd value • etc... • also stop if hit end of array

  7. 202 203 401 413 516 607 717 802 Let N = 8. Search for key = 607 (linear) index == 0  index to 1st item in the array being searched areaCodes [0] index == 1 index == 2 index == 3 index == 4 index == 5 found with 6 comparisons

  8. Search for the “code” // loop to search for the code in the codes array found = false; while (index < num_area_codes && !found) { if (areaCodes [index] != codeWanted ) index++; // they do NOT match else found = true; // they do match foundAt = index; // index where found } comparison

  9. Linear Search (making sense of the results) if (found) cout << "Location is " << locations [foundAt]; else cout << "Area code not found.";

  10. Linear Search • Advantage: array elements do not have to be in any order • Disadvantage: very slow if the array is large

  11. Order of Linear Search: O(N) • Let N be the number of things being searched through • In this case, the number of area codes • Worst case: • N comparisons • value sought is last one in array • value is not in the array • Best case: • 1 comparison • value sought is 1st in array

  12. On Average • Average number of comparisons: • (N + 1) / 2 • Actual processing time proportional to N • we say the algorithm is O (N) • "big O" notation -- the order of • Want faster? • Must sort array first

  13. Binary Search • Requirement: array must be sorted • Principle: check an element • either get a match • or eliminate half the elements • or run out of elements to search

  14. Remember when you were a kid… • I’m thinking of a number 1 – 100 • Number picked? • Response?

  15. Binary Search Algorithm if (key == middle item) found a match else if key < middle item) next search 1st half of list else next search 2nd half of list repeat entire process using new "half"

  16. Let N = 8. Search for 607 (binary) first = 0 = index to 1st item in part of array being searched 0 1 2 3 4 5 6 7 202 203 401 mid = (first+last) / 2 = (0+7) / 2 = 3 413 516 607 717 last = 7 = index to last item in part of array being searched 802

  17. Reset first to mid + 1, then reset mid found with 2 comparisons 202 0 1 2 3 4 5 6 7 203 401 413 first = 4 516 607 mid = (4 + 7) / 2 = 5 717 802 last = 7

  18. Binary Search Call (from main) binarySearch(num_area_codes, areaCodes, key, found, foundAt); if (found) cout << "Location is " << locations [foundAt];else cout << "Area code not found.";

  19. Binary Search Function void binarySearch (intnum_area_codes, // # of codes constintareaCodes[], int key, //code to look for bool& found, // true if found int&foundAt ) // index found at{ int first, last, mid; // indexes found = false; //initializations first = 0; last = num_area_codes - 1;

  20. Search for Area Code in Array found = false; while ( first <= last &&!found ) { mid = (first + last) / 2; // set mid if (key < area_codes[ mid ] ) last = mid - 1; else if (key > area_codes[ mid ]) first = mid + 1; else { found = true; // match ! foundAt = mid; // index found at }

  21. About the Binary Search • Why compare first <= last ? • when first == last, • have reached final comparison; • subarray being searched reaches a size of 1 • when first > last • they have "crossed" • no more array elements left to consider • efficiency: why order tests this way? • equals case is last, is least likely in a long list

  22. Efficiency of Binary Search • Processing time: • # of comparisons is proportional to log2(N) • Say algorithm is O (log2N) • e.g., if N == 1024 • linear search: average #comparisons is • 512 • binary search: average # comparisons is • 10

  23. Comparison of Sequential and Binary Searches Average Number of Iterations to Find item Length Sequential Search Binary Search 10 5.5 2.9 100 50.5 5.8 1,000 500.5 9.0 10,000 5000.5 12.4 23

  24. TIME in nanoseconds linear 60 logarithmic 15 N 15 ~100

  25. Names of Orders of Magnitude O(1) constant time O(log2N) logarithmic time O(N) linear time O(N2) quadratic time O(N3 ) cubic time 26

  26. 1 0 0 1 2 1 2 4 4 2 8 16 8 3 24 64 16 4 64 256 32 5 160 1024 64 6 384 4096 128 7 896 16,384 N log2N N*log2N N2 27

  27. Big-O Comparison of Array Operations OPERATION UnsortedList SortedList IsPresent O(N) O(N) sequential search O(log2N) binary search Insert O(1) O(N) Delete O(N) O(N) SelSort O(N2) 28

  28. Sorting • How do we put a list of items in order (ascending or descending)? • Example: sort an array A with N integer elements into ascending order A[0] 32 -16A[1] 115 32A[2] 56 43A[3] -16 56A[4] 43 115

  29. Selection/Bubble Sort • Basic Algorithm • perform N-1 passes • on each pass, exchange the first element in the unsorted part of the array with the smallest element in the unsorted portion of the array; at the end of each pass, one element is thus "sorted" into the correct position • repeat, reducing the unsorted portion to the remaining unsorted elements

  30. First Pass A[0] 32 -16A[1] 115 115A[2] 56 56A[3] -16 32A[4] 43 43 sorted unsorted

  31. Second Pass A[0]-16-16A[1] 115 32A[2] 56 56A[3] 32 115A[4] 43 43 sorted unsorted

  32. Third Pass A[0]-16-16A[1]3232A[2] 56 43A[3] 115 115A[4] 43 56 sorted unsorted

  33. Fourth Pass A[0]-16-16A[1]3232A[2]4343A[3] 115 56A[4] 56 115 sorted

  34. void selectionSort (int N, int A[]) { // sort an array of integers into ascending order inti,// index to first value in unsorted portion smallest,// index to smallest value in unsortedportion current,// used to scan array for smallest value temp; // temporary storage during a swap for (i= 0; i<(N - 1); i++ ){ smallest = i;// scan unsorted part of array to find smallest value for (current = i+1; current < N; current++ ){ if ( A [current] < A [smallest] ) smallest = current; } // inner for loop // perform one exchange of elements if necessary if (i != smallest ){ temp = A[i]; A[i] = A[smallest]; A[smallest] = temp; } } // outer for loop } // function SelectionSort

  35. Efficiency: Selection Sort • maximum number of comparisons: • N * (N - 1) / 2 • maximum number of exchanges: • N - 1 • processing time is proportional to N2 • O(N2) • this is a quadratic sort (fairly slow)about the best we can do without using different structuring

  36. Descending Sort (1) How would we change this selection sort function so that it sorted the array elements into descending order instead of ascending?

  37. Descending Sort (1a) How would we change this selection sort function so that it sorted the array elements into descending order instead of ascending? - change the < operator in the comparison to > - change the name of the variable smallest to largest if ( A [ current ] > A [ largest ] )

  38. A Note on sorting parallel arrays • example: sort the area codes and locations arrays we discussed earlier into ascending numeric order, by area code • the area code is called the sort key • comparison: use one arrayif (area_codes[current] < area_codes [small]) • exchange: perform in both arraysarea_codes[i] with area_codes[small]locations[i] with locations[small]

  39. Insertion Sort one item is automatically in order 3 16 12 1 2

  40. Insertion Sort 3 16 compare this element against the one above it it is > than it  so it is in order 12 1 2

  41. Insertion Sort 3 3 16 12 compare: OK 12 16 compare this element against the one above it It is < it, so swap 1 1 2 2

  42. 3 12 16 1 2 Insertion Sort 3 3 12 1 1 12 compare swap 16 16 compare it is < swap 2 2

  43. Insertion Sort 3 1 1 compare it is < swap 3 12 12 16 16 2 compare 2

  44. 1 1 1 1 3 3 3 2 12 12 2 3 16 2 12 12 2 16 16 16 Insertion Sort

  45. Insertion Sort void insert( int n, int A[]) { int i,j, tmp; for (i = 1; i < n-1; i++) { j = i; while (j > 0 && A[j] < A[j-1]){ tmp = A[j]; A[j] = A[j-1]; A[j-1] = tmp; j--; } }

  46. Insertion Sort • Great for reading in values and inserting them into an array in order.

More Related