Algorithm & data structures lec4&5

4,110 views

Published on

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,110
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
176
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Algorithm & data structures lec4&5

  1. 1. Algorithms & Data Structures CS112 Spring 2012 Lecture 4 Syed Muhammad Raza
  2. 2. Searching Algorithms Searching is the process of determining whether or not a given value exists in a data structure or a storage media. We will study two searching algorithms  Linear Search  Binary Search
  3. 3. Linear Search: O(n) The linear (or sequential) search algorithm on an array is:  Start from beginning of an array/list and continues until the item is found or the entire array/list has been searched.  Sequentially scan the array, comparing each array item with the searched value.  If a match is found; return the index of the matched element; otherwise return –1. Note: linear search can be applied to both sorted and unsorted arrays.
  4. 4. Linear Search bool LinSearch(double x[ ], int n, double item) { for(int i=0;i<n;i++) { if(x[i]==item) { return true; } else { return false; } } return false; }
  5. 5. Linear Search Tradeoffs Benefits  Easy to understand  Array can be in any order Disadvantages  Inefficient for array of N elements Examines N/2 elements on average for value in array, N elements for value not in array
  6. 6. Binary Search: O(log2 n) Binary search looks for an item in an array/list using divide and conquer strategy
  7. 7. Binary Search  Binary search algorithm assumes that the items in the array being searched is sorted  The algorithm begins at the middle of the array in a binary search  If the item for which we are searching is less than the item in the middle, we know that the item won’t be in the second half of the array  Once again we examine the “middle” element  The process continues with each comparison cutting in half the portion of the array where the item might be
  8. 8. Binary Search Binary search uses a recursive method to search an array to find a specified value The array must be a sorted array: a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex] If the value is found, its index is returned If the value is not found, -1 is returned Note: Each execution of the recursive method reduces the search space by about a half
  9. 9. Pseudocode for Binary Search
  10. 10. Execution of Binary Search
  11. 11. Execution of Binary Search
  12. 12. Key Points in Binary Search1. There is no infinite recursion • On each recursive call, the value of first is increased, or the value of last is decreased • If the chain of recursive calls does not end in some other way, then eventually the method will be called with first larger than last2. Each stopping case performs the correct action for that case • If first > last, there are no array elements between a[first] and a[last], so key is not in this segment of the array, and result is correctly set to -1 • If key == a[mid], result is correctly set to mid
  13. 13. Key Points in Binary Search3. For each of the cases that involve recursion, if all recursive calls perform their actions correctly, then the entire case performs correctly • If key < a[mid], then key must be one of the elements a[first] through a[mid-1], or it is not in the array • The method should then search only those elements, which it does • The recursive call is correct, therefore the entire action is correct
  14. 14. Key Points in Binary Search • If key > a[mid], then key must be one of the elements a[mid+1] through a[last], or it is not in the array • The method should then search only those elements, which it does • The recursive call is correct, therefore the entire action is correctThe method search passes all three tests:Therefore, it is a good recursive method definition
  15. 15. Efficiency of Binary Search The binary search algorithm is extremely fast compared to an algorithm that tries all array elements in order  About half the array is eliminated from consideration right at the start  Then a quarter of the array, then an eighth of the array, and so forth
  16. 16. Efficiency of Binary Search Given an array with 1,000 elements, the binary search will only need to compare about 10 array elements to the key value, as compared to an average of 500 for a serial search algorithm The binary search algorithm has a worst-case running time that is logarithmic: O(log n)  A serial search algorithm is linear: O(n) If desired, the recursive version of the method search can be converted to an iterative version that will run more efficiently
  17. 17. Binary Search
  18. 18. Algorithms & Data Structures CSC-112 Fall 2011 Lecture 5 Syed Muhammad Raza
  19. 19. Sorting Algorithms Sorting is the process of rearranging your data elements/Item in ascending or descending order  Unsorted Data 4 3 2 7 1 6 5 8 9  Sorted Data (Ascending) 1 2 3 4 5 6 7 8 9  Sorted Data (Descending) 9 8 7 6 5 4 3 2 1
  20. 20. Sorting Algorithms They are many  Bubble Sort  Selection Sort  Insertion Sort  Shell sort  Comb Sort  Merge Sort  Heap Sort  Quick Sort  Counting Sort  Bucket Sort  Radix Sort  Distribution Sort  Time Sort Source: Wikipedia
  21. 21. Bubble Sort Compares Adjacent Items and Exchanges Them if They are Out of Order When You Order Successive Pairs of Elements, the Largest Element Bubbles to the Top(end) of the Array Bubble Sort (Usually) Requires Several Passes Through the Array
  22. 22. Bubble Sort Pass 1 Pass 229 10 14 37 13 10 14 29 13 3710 29 14 37 13 10 14 29 13 3710 14 29 37 13 10 14 29 13 3710 14 29 37 13 10 14 13 29 3710 14 29 13 37
  23. 23. Selection Sort To Sort an Array into Ascending Order, First Search for the Largest Element Because You Want the Largest Element to be in the Last Position of the Array, You Swap the Last Item With the Largest Item to be in the Last Position of the Array, You Swap the Last Item with the Largest Item, Even if These Items Appear to be Identical Now, Ignoring the Last (Largest) Item of the Array, search Rest of the Array For Its Largest Item and Swap it With Its Last Item, Which is the Next-to-Last Item in the original Array You Continue Until You Have Selected and Swapped N-1 of the N Items in the Array The Remaining Item, Which is Now in the First Position of the Array, is in its Proper Order
  24. 24. Selection Sort Initial Array 29 10 14 37 13 After1st Swap 29 10 14 13 37 After2nd Swap 13 10 14 29 37 After3rd Swap 13 10 14 29 37 After4th Swap 10 13 14 29 37
  25. 25. Insertion Sort Divide Array Into Sorted Section At Front (Initially Empty), Unsorted Section At End Step Iteratively Through Array, Moving Each Element To Proper Place In Sorted Section Sorted Unsorted ….. ….. 0 i N-1 After i Iterations
  26. 26. Insertion SortInitial Array 29 10 14 37 13 Copy 10 29 29 14 37 13 Shift 29 10 29 14 37 13 Insert 10, Copy 14 10 29 29 37 13 Shift 29 Insert 14; Copy 37 10 14 29 37 13 Insert 37 on Itself 10 14 29 37 13 Copy 13 10 14 14 29 37 Shift 14, 29, 37Sorted Array 10 13 14 29 37 Insert 13

×