100 likes | 224 Views
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!.
E N D
Cs212: DataStructures Lecture 3: Searching
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!
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.
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}
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; • }
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; } }
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}
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; • }
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); } }