(17) Searching and sorting-Compatibility Mode

(17) Searching and sorting-Compatibility Mode - Wheres...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Searching and Sorting Nathan Friedman Fall, 2008 Where’s Waldo? 2008 Searching and Sorting 2 Where’s Waldo? ± A common use for computers is to search for the whereabouts of a specific item in a list 2008 Searching and Sorting 3 ± The most straightforward approach is just to start looking at the beginning and go on from there Linear Search int linear_search(int val, int arr[], int size) { int i; for(i = 0; i < size; ++i) { if(arr[i] == val) 2008 Searching and Sorting 4 return i; } return -1; } Searching Sorted Lists ± Is that the way we would look up a name in the Montreal telephone directory? 2008 Searching and Sorting 5 Searching Sorted Lists ± Is that the way we would look up a name in the Montreal telephone directory? 2008 Searching and Sorting 6 ± I hope not!
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Binary Search To search a sorted array, we could check the middle element The value we are looking for might be 2008 Searching and Sorting 7 The value we are looking for might be there If not we can determine whether it is in the first or second half of the array and search that smaller array Iterative Binary Search int binary_search(int val, int arr[], int size) { int left = 0, right = size-1, middle; do { middle = (left + right) / 2; if(arr[middle] < val) 2008 Searching and Sorting 8 left = middle + 1; else if(arr[middle] > val) right = middle - 1; else return middle; } while(left <= right); return -1; } Sorting Data Sorting is one of the most common tasks given to computers Much work has been done on developing 2008 Searching and Sorting 9 Much work has been done on developing efficient sorting techniques We have seen one method and now we consider some others Remember Bubble Sort? void bubble_sort(int arr[], int size){ int i, j; for (i=0; i<size-1; i++){ for (j=size-1; j>i; --j) 2008 Searching and Sorting 10 if (arr[j] < arr[j-1]) swap (&arr[j], &arr[j-1]); } } An Optimization If no swaps are made, the array is already sorted We can keep track of whether any swaps 2008 Searching and Sorting 11 We can keep track of whether any swaps were made in a pass If no swaps were made, the array must be sorted and we can stop Optimized Bubble Sort void bubble_sort(int arr[], int size) { int i, j, swapped; for (i=0; i<size-1; i++){
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2010 for the course COMP COMP 206 taught by Professor Vybihal during the Spring '04 term at McGill.

Page1 / 7

(17) Searching and sorting-Compatibility Mode - Wheres...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online