1 / 15

Cs212: DataStructures

Cs212: DataStructures. Lecture 3: Searching. Lecture Contents. searching S equential search algorithm . B inary search algorithm. Search Algorithms. Searching , the process used to find the location of a target among a list of objects.

grover
Télécharger la présentation

Cs212: DataStructures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cs212: DataStructures Lecture 3: Searching

  2. Lecture Contents • searching • Sequential search algorithm. • Binary search algorithm.

  3. Search Algorithms • Searching , the process used to find the location of a target among a list of objects. • In this chapter, we will study searches that work with arrays

  4. Search Algorithms • Sequential search. • It works on the ordered list or the unordered. • Binary search. • It requires an ordered list.

  5. 1/ Sequential (Linear) Search • Search an array or list by checking items one at a time. Sequential search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list. • Look at every element : This is a very straightforward loop comparing every element in the array with the target(key). • Eighter we find it, • or we reach the end of the list! • Sequential search works the same for both array-based and linked lists

  6. Sequential Search public intlinearSearch (int target) { for (int n = 0; n < a.length ; n++) { if (a[n] == target) return n; { return –1; }

  7. Locating data in unordered list.

  8. 2/ Binary search algorithm • Search a sorted array by repeatedly dividing the search interval in half. • A fast way to search a sorted array is to use a binary search. • Can’t use binary search algorithm with linked list • No physical relationship between the nodes

  9. Binary search algorithm Calculate the middle element Test the data in the element at the middle of the array. Target > middle element Target < middle element it is in the second half after middle it is in the first halfbefore middle Calculate the middle element Calculate the middle element Test the data in the element at the middle of the array. Test the data in the elementat the middle of the array. Target < middle Target > middle Target < middle Target > middle it is in the first half! it is in the second half! it is in the second half! it is in the first half! . . . . . . . . If the middle element equals to the Target , the algorithm stops

  10. mid=(first+last)/2 target > A[mid] first = mid +1 target < A[mid] last = mid -1 target ==A[mid]

  11. target <A[mid] last = mid -1 target > A[mid] first = mid +1 target > A[mid] first = mid +1 target <A[mid] last = first  not found stop

  12. Recursive Binary search algorithm algorithm RecBinarySearch (First, last,target) INPUT First is index to first element in the list. last is index to last element in the list. target contains the data to be located. OUTPUT if found – return the index if not found – return (-1)

  13. Recursive search algorithm mid = if target=a[mid]then Location= mid else if (first=last) then Location= -1 else if (target < a[mid]) then Location =binarySearch(first, mid-1,target) else if (target> a[mid]) then Location=binarySearch(mid+1, last, target) Return Location base cases recursive calls

  14. Example BinarySearch(0,4,20) M=0+4/2=2 20 >7 then binarySearch(3, 4,20) Return 4 Return 4 BinarySearch(3,4,20) M=3+4/2=3 20 >11 then binarySearch(4, 4,20) Recursive call Return 4 BinarySearch(4,4,20) M=4+4/2=2 20 == 20 Recursive call

  15. End Of Chapter

More Related