1 / 10

Cs212: DataStructures

Cs212: DataStructures. Lecture 3: Searching. Search Algorithms. 1/ Sequential (Linear ) Search. Look at every element : 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!.

omar-scott
Download Presentation

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. Search Algorithms

  3. 1/ Sequential (Linear) Search • Look at every element : • very straightforward loop comparing every element in the array with the target(key). • Eighter we find itor we reach the end of the list!

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

  5. Example 1 • Write a program that Prints the index of a number in the following list. The number is entered from the user (using the binary search). {4,6,8,10,14,16,22,34,47,55,58,90}

  6. Example 1 – C++ • #include<iostream> • intBinSearchI(intlist[], intkey, intsize){ • int mid; • int first=0; • int last=size-1; • while(first<=last) • { • mid = (first+last)/2; • if (list[mid]==key) • return mid; • else if (list[mid]<key) • first=mid+1; • else • last = mid-1; • } • return -1;} • void main() { • int A[]= {4,6,8,10,14,16,22,34,47,55,58,90}; int x ; int size=12; • cout<<"The list is: {"; • for (inti = 0; i< 12;i++){ • cout<<A[i]<<",";} • cout<<"}"; • cout<<"Please enter a number: "; • cin>>x; • cout<<"The index of "<<x<<" is: "<<BinSearchI (A,x,size); • system ("pause"); • return 0; • }

  7. Example 1 – Java package binarys1; importjava.util.Scanner; public class BinaryS1{ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); int [] A = {4,6,8,10,14,16,22,34,47,55,58,90}; System.out.print("The list is: {"); for (inti = 0; i<A.length;i++){ System.out.print(A[i]+","); } System.out.println("}"); System.out.print("Please enter a number: "); int x = input.nextInt(); System.out.println("The index of "+x+" is: "+BinSearchI (A,x,A.length)); } public static intBinSearchI(int [] list, int key, int size){ intmid,first=0,last=size-1; while(first<=last){ mid = (first+last)/2; if (list[mid]==key) return mid; else if (list[mid]<key) first=mid+1; else last = mid-1; } return -1; } }

  8. Example 2 • Write a program that search for a number in the following list and prints FOUND if the number is in the list and NOT FOUND if the number is not in the list. The number must be entered from the user (using binary search) {0,3,5,7,8,9,12,21,26,33,37,38,40,43,46,50}

  9. Example 2 – C++ • #include<iostream> • voidBinarySearchR(intlist[], intfirst, intlast, intkey){ • int loc; • int m = (first+last)/2; • if (key==list[m]) • cout<<key<<" is FOUND.\n"; • else if (first==last) • cout<<key<<" is NOT FOUND.\n"); • else if(key<list[m]) • BinarySearchR(list,first,m-1,key); • else if(key>list[m]) • BinarySearchR(list,m+1,last,key); • } • void main() { • int B[]= {0,3,5,7,8,9,12,26,33,37,38,40,43,46,50}; • int y; • int size=12 • cout<<"The list is: {"; • for(inti=0; i<12; i++){ • cout<<B[i]; • cout<<","; • } • cout<<"}\n“; • cout<<"Enter a number: "; • cin>>y; • BinarySearchR(B,0,size-1,y); • system("pause"); • return 0; • }

  10. Example 2 – Java package binarys2; importjava.util.Scanner; public class BinaryS2 { public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); int [] B = {0,3,5,7,8,9,12,26,33,37,38,40,43,46,50}; System.out.print("The list is: {"); for(inti=0; i<B.length; i++){ System.out.print(B[i]+","); } System.out.println("}"); System.out.print("Enter a number: "); int y = input.nextInt(); BinarySearchR(B,0,B.length-1,y); } public static voidBinarySearchR(int [] list, int first, int last, int key){ intloc,m = (first+last)/2; if (key==list[m]) System.out.println(key+" is FOUND."); else if (first==last) System.out.println(key+" is NOT FOUND."); else if(key<list[m]) BinarySearchR(list,first,m-1,key); else if(key>list[m]) BinarySearchR(list,m+1,last,key); } }

More Related