Download Slide 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
CS212: DATASTRUCTURES
Lecture 3: Searching
Search Algorithms
2
Search
Algorithms
Sequential
Search
It does not
require an
ordered list.
Binary Search
It requires an
ordered list.
1/ Sequential (Linear) Search
3

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!
2/ Binary search algorithm
4


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
5
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++
6




























#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;
}
Example 1 – Java
7
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;
}
}
Example 2
8
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++
9

#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;
}
Example 2 – Java
10
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);
}
}
Related documents