10
CS212: DATASTRUCTURES Lecture 3: Searching 1

Cs212: DataStructures

Embed Size (px)

DESCRIPTION

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!. - PowerPoint PPT Presentation

Citation preview

Page 1: Cs212:  DataStructures

CS212: DATASTRUCTURES

Lecture 3: Searching

1

Page 2: Cs212:  DataStructures

2

Search Algorithms

Search Algorithms

Sequential Search

It does not require an ordered list.

Binary Search

It requires an ordered

list.

Page 3: Cs212:  DataStructures

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 it or we reach the end of

the list!

Page 4: Cs212:  DataStructures

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.

Page 5: Cs212:  DataStructures

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}

Page 6: Cs212:  DataStructures

6

Example 1 – C++ #include<iostream>int BinSearchI(int list[], int key, int size){ 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 (int i = 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; }

Page 7: Cs212:  DataStructures

7

Example 1 – Java package binarys1;import java.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 (int i = 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 int BinSearchI(int [] list, int key, int size){ int mid,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; }}

Page 8: Cs212:  DataStructures

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}

Page 9: Cs212:  DataStructures

9

Example 2 – C++#include<iostream>

void BinarySearchR(int list[], int first, int last, int key){ 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(int i=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; }

Page 10: Cs212:  DataStructures

10

Example 2 – Java

package binarys2;import java.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(int i=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 void BinarySearchR(int [] list, int first, int last, int key){ int loc,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); }}