Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching and Sorting 1-D Arrays

Similar presentations


Presentation on theme: "Searching and Sorting 1-D Arrays"— 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 On the Internet
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 do lecture demonstration: bring a phone book to class, use linear and binary searches on it

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    areaCodes (int) locations (string)
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 Let N = 8. Search for key = 607 (linear)
index == 0  index to 1st item in the array being searched areaCodes [0] 202 203 401 413 516 607 717 802 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:
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 1 2 3 4 5 6 7 202 203 401 mid = (first+last) / 2 = (0+7) / 2 = 3 413 516 607 To see an example of when first == last and a comparison still has to be made, then first becomes greater than last and must exit, use target value of 734 on this array data 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 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 (int num_area_codes, // # of codes const int areaCodes[], 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 1, 10, 23

24 linear 60 logarithmic 15 N 15 ~100 TIME in nanoseconds Now, ask class
how would you really search the phone book? (bin search, try to find section with correct letter, like 'S' for Smith) How does google work? AI students became millionaires! Think about how an intelligent human being would index or categorize the web, then search it. Think about things like a search for "women and inline skating" What do you see on the front screen at a web search site, like Excite, Lycos, Yahoo? Categories: Sports, Health, Entertainment; people categorize for searching 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 N log2N N*log2N N2 ,384 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] A[1] A[2] A[3] A[4]

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 -16 A[1] 115 115 A[2] 56 56 A[3] -16 32 A[4] 43 43
sorted unsorted

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

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

33 Fourth Pass A[0] -16 -16 A[1] 32 32 A[2] 43 43 A[3] 115 56 A[4] 56 115
sorted

34 void selectionSort (int N, int A[]) {
// sort an array of integers into ascending order int i, // index to first value in unsorted portion smallest, // index to smallest value in unsorted portion 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 array if (area_codes[current] < area_codes [small]) exchange: perform in both arrays area_codes[i] with area_codes[small] locations[i] with locations[small]

39 Insertion Sort 3 one item is automatically in order 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 12 16 1 1 2 2 compare: OK
compare this element against the one above it It is < it, so swap 16 1 1 2 2

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

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

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

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.


Download ppt "Searching and Sorting 1-D Arrays"

Similar presentations


Ads by Google