Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time Analysis Ming Li Department of Computer Science California State University, Fresno Spring 2007
Introduction to Data Structure, Spring 2007 Slide- 2 California State University, Fresno Selection Sort Pass 0: Scan the entire list from arr[0] to arr[4] and identify 20 at index 1 as the smallest element. Exchange 20 with arr[0] = 50, the first element in the list pass = 0 20
Introduction to Data Structure, Spring 2007 Slide- 3 California State University, Fresno Selection Sort Pass 1: Scan the entire list from arr[1] to arr[4] and identify 35 at index 4 as the smallest element. Exchange 20 with arr[0] = 50, the first element in the list pass = 1 50
Introduction to Data Structure, Spring 2007 Slide- 4 California State University, Fresno Selection Sort Pass 2: Scan the entire list from arr[2] to arr[4] and identify 40 at index 2 as the smallest element. No exchange necessary since arr[2] is the smallest pass = 2 35
Introduction to Data Structure, Spring 2007 Slide- 5 California State University, Fresno Selection Sort Pass 3: Scan the entire list from arr[3] to arr[4] and identify 50 at index 4 as the smallest element. Exchange arr[3] and arr[4] pass = 3 35
Introduction to Data Structure, Spring 2007 Slide- 6 California State University, Fresno Selection Sort Finally: The list is sorted End 35
Introduction to Data Structure, Spring 2007 Slide- 7 California State University, Fresno Selection Sort - Algorithm for each pass i from 0 to n-2 { find the smallest element from arr[i] to arr[n-1] -> k; if (k == i) continue; else exchange arr[i] and arr[k]; } end
Introduction to Data Structure, Spring 2007 Slide- 8 California State University, Fresno Selection Sort - Algorithm for each pass i from 0 to n-2 { k=i; for each element j from i+1 to n-1 if (arr[j] < arr[k]) k = j; if (k == i) continue; else exchange arr[i] and arr[k]; } end
Introduction to Data Structure, Spring 2007 Slide- 9 California State University, Fresno Selection Sort - Algorithm for each pass i from 0 to n-2 { k = i; for each element j from i+1 to n-1 if (arr[j] < arr[k]) k = j; if (k == i) continue; else temp = arr[i]; arr[i] = arr[k]; arr[k] = temp; } end
Introduction to Data Structure, Spring 2007 Slide- 10 California State University, Fresno Selection Sort - Code void selectionSort(int arr[], int n) { int smallIndex, pass, j, temp; for (pass = 0; pass < n-1; pass++) { for (j = pass+1; j < n; j++) if (arr[j] < arr[smallIndex]) smallIndex = j; if (smallIndex != pass) { temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; }
Introduction to Data Structure, Spring 2007 Slide- 11 California State University, Fresno Sequential Search index =seqSearch(arr, 0, 8, 3); 7 Index target = 3 8 match at index = 5 return index 5
Introduction to Data Structure, Spring 2007 Slide- 12 California State University, Fresno Sequential Search Index target = 9 8 no match return index 8 index =seqSearch(arr, 0, 8, 9);
Introduction to Data Structure, Spring 2007 Slide- 13 California State University, Fresno Sequential Search - Algorithm Search algorithms start with a target value and employ some strategy to visit the elements looking for a match. –If target is found, the index of the matching element becomes the return value. –Otherwise, return NONE.
Introduction to Data Structure, Spring 2007 Slide- 14 California State University, Fresno Sequential Search - Algorithm matchIndex = NONE; matchValue = x; for each element i from 0 to n-1 if(arr[i] == x) matchIndex = i; break; return matchIndex;
Introduction to Data Structure, Spring 2007 Slide- 15 California State University, Fresno Sequential Search - Code int seqSearch(const int arr[], int first, int last, int target) { int i = first; while(i != last && arr[i] != target) i++; return i; }
Introduction to Data Structure, Spring 2007 Slide- 16 California State University, Fresno Binary Search - Example For sorted list: If target=mid, then find it. midfirst target last-1last
Introduction to Data Structure, Spring 2007 Slide- 17 California State University, Fresno Binary Search - Example For sorted list: If target<mid, then find between (first, mid). mid firstlast-1last Targe t mid-1
Introduction to Data Structure, Spring 2007 Slide- 18 California State University, Fresno Binary Search - Example For sorted list: If target>mid, then find between (first, mid). mid firstlast-1last Targe t mid+1
Introduction to Data Structure, Spring 2007 Slide- 19 California State University, Fresno Binary Search – target=23 Mid = (0+9)/2 = 4
Introduction to Data Structure, Spring 2007 Slide- 20 California State University, Fresno Binary Search – target=23 Mid = (5+9)/2 = 7
Introduction to Data Structure, Spring 2007 Slide- 21 California State University, Fresno Binary Search – target=23 Mid = (5+7)/2 = 6, got it!
Introduction to Data Structure, Spring 2007 Slide- 22 California State University, Fresno Binary Search – target=4 Mid = (0+9)/2 = 4
Introduction to Data Structure, Spring 2007 Slide- 23 California State University, Fresno Binary Search – target=4 mid = (0+4)/2 = 2
Introduction to Data Structure, Spring 2007 Slide- 24 California State University, Fresno Binary Search – target=4 mid = (0+2)/2 = 1, unsuccessful! Since target = 4 > midvalue = 3, step 4 should search the upper sublist with first = 2 and last =2. However, since first >= last, the target is not in the list and we return index last = 9.
Introduction to Data Structure, Spring 2007 Slide- 25 California State University, Fresno Binary Search - Algorithm int BinarySearch(first, last, x) { if(first >= last) return NONE; Mid = (first+last)/2; If (x == arr[mid]) return mid; else if (x > arr[mid]) return BinarySearch(mid+1, last, target); else return BinarySearch(first, mid-1, target); }
Introduction to Data Structure, Spring 2007 Slide- 26 California State University, Fresno Binary Search - Code Int binSearch(const int arr[], int first, int last, int target) { int mid, int midvalue; int origLast = last; while (first < last){ mid = (first+last)/2;midvalue = arr[mid]; if (target == midvalue)return mid; else if (target < midvalue)last = mid; else first = mid+1; } return origLast; }
Introduction to Data Structure, Spring 2007 Slide- 27 California State University, Fresno Examples Given a sorted list: –Find two numbers such that x+y = c (constant)
Introduction to Data Structure, Spring 2007 Slide- 28 California State University, Fresno Big O Notation Identifies the time efficiency –Q: Does the number of operations depend on the array size? Given array size of n –Each comparison/assignment: O(1) –Selection Sort: O(n 2 ) –Sequential Search: O(n) –Binary Search: O(logn)
Introduction to Data Structure, Spring 2007 Slide- 29 California State University, Fresno Exponential Algorithm Algorithms with running time O(n 2 ) are quadratic. –practical only for relatively small values of n. Whenever n doubles, the running time of the algorithm increases by a factor of 4. Algorithms with running time O(n 3 )are cubic. –efficiency is generally poor; doubling the size of n increases the running time eight-fold.
Introduction to Data Structure, Spring 2007 Slide- 30 California State University, Fresno Exponential Algorithm
Introduction to Data Structure, Spring 2007 Slide- 31 California State University, Fresno Comparison – Big O