Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time.

Similar presentations


Presentation on theme: "Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time."— Presentation transcript:

1 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

2 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. 50407535 pass = 0 20

3 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. 20407535 pass = 1 50

4 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. 20407550 pass = 2 35

5 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]. 20407550 pass = 3 35

6 Introduction to Data Structure, Spring 2007 Slide- 6 California State University, Fresno Selection Sort Finally: The list is sorted. 20405075 End 35

7 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

8 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

9 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

10 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; }

11 Introduction to Data Structure, Spring 2007 Slide- 11 California State University, Fresno Sequential Search 6429510 index =seqSearch(arr, 0, 8, 3); 7 Index01234567 3 target = 3 8 match at index = 5 return index 5

12 Introduction to Data Structure, Spring 2007 Slide- 12 California State University, Fresno Sequential Search 642953107 Index01234567 target = 9 8 no match return index 8 index =seqSearch(arr, 0, 8, 9);

13 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.

14 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;

15 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; }

16 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

17 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

18 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

19 Introduction to Data Structure, Spring 2007 Slide- 19 California State University, Fresno Binary Search – target=23 Mid = (0+9)/2 = 4

20 Introduction to Data Structure, Spring 2007 Slide- 20 California State University, Fresno Binary Search – target=23 Mid = (5+9)/2 = 7

21 Introduction to Data Structure, Spring 2007 Slide- 21 California State University, Fresno Binary Search – target=23 Mid = (5+7)/2 = 6, got it!

22 Introduction to Data Structure, Spring 2007 Slide- 22 California State University, Fresno Binary Search – target=4 Mid = (0+9)/2 = 4

23 Introduction to Data Structure, Spring 2007 Slide- 23 California State University, Fresno Binary Search – target=4 mid = (0+4)/2 = 2

24 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.

25 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); }

26 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; }

27 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)

28 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)

29 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.

30 Introduction to Data Structure, Spring 2007 Slide- 30 California State University, Fresno Exponential Algorithm

31 Introduction to Data Structure, Spring 2007 Slide- 31 California State University, Fresno Comparison – Big O


Download ppt "Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time."

Similar presentations


Ads by Google