#include<iostream.h>
class searching
{
private:
double *array;
int n;
public:
void input();
void linearsearch();
};
void searching::input()
{
cout<<”*********************************…
<<”This program is to implement linear search algorithm\n”
<<”*************************************…
cout<<”Enter how many numbers you are going to enter::”;
cin>>n;
array=new double[n+1];
cout<<”Now enter your elements ::\n”;
for(int i=1;i<=n;i++)
cin>>array[i];
}
void searching::linearsearch()
{
cout<<”Enter the number to be searched ::”;
double x;
cin>>x;
int i;
for(i=1;i<=n;i++)
{
if(array[i]==x)
{
cout<<”found at position ::”<<i<<endl;
break;
}
}
if(i>n)
cout<<”Not found\n”;
}
int main()
{
searching obj;
obj.input();
obj.linearsearch();
return 0;
}
/************* Please not copy this************************…
SAMPLE OUTPUT ::
**************************************…
This program is to implement linear search algorithm
**************************************…
Enter how many numbers you are going to enter::5
Now enter your elements ::
1.1
1.2
1.3
1.4
1.5
Enter the number to be searched ::1.5
found at position ::5
Press any key to continue
//Binary Searching
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int l_v=1;
int h_v;
int a[51];
int middle;
int num,i=0;
cout<<"Input your sorted num :
";
while(scanf("%d",&a[i])!=EOF)
i++;
h_v=i;
cout<<"
The input numbers are :
";
i=0;
while(i<h_v)
{
cout<<a[i]<<endl;
i++;
}
getch();
cout<<"
Input your searching number :
";
cin>>num;
for(int n=0;a[middle]!=num;n++)
{
middle = (l_v + h_v)/2;
if(a[middle]>num)
h_v = middle - 1;
else if(a[middle]<num)
l_v = middle + 1;
else if(a[middle]==num)
cout<<"
Found your number in "<<middle+1<<" position.";
}
cout<<"
This program's loop is executed "<<n<<" times to find out
the
number.";
getch();
}
Binary search program in C
#include <stdio.h>
int main()
{
int a[20] = {0};
int n, i, j, temp;
int *beg, *end, *mid, target;
printf(" enter the total integers you want to enter (make it less then 20):\n");
scanf("%d", &n);
if (n >= 20) return 0; // ouch!
printf(" enter the integer array elements:\n" );
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
// sort the loaded array, a must for binary search!
// you can apply qsort or other algorithms here
for(i = 0; i < n-1; i++)
{
for(j = 0; j < n-i-1; j++)
{
if (a[j+1] < a[j])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf(" the sorted numbers are:");
for(i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
// point to beginning and end of the array
beg = &a[0];
end = &a[n]; // use n = one element past the loaded array!
printf("\n beg points to address %d and end to %d",beg, end); // test
// mid should point somewhere in the middle of these addresses
mid = beg += n/2;
printf("\n mid points to address %d", mid); // test
printf("\n enter the number to be searched:");
scanf("%d",&target);
// binary search, there is an AND in the middle of while()!!!
while((beg <= end) && (*mid != target))
{
// is the target in lower or upper half?
if (target < *mid)
{
end = mid - 1; // new end
n = n/2;
mid = beg += n/2; // new middle
}
else
{
beg = mid + 1; // new beginning
n = n/2;
mid = beg += n/2; // new middle
}
}
// did you find the target?
if (*mid == target)
{
printf("\n %d found!", target);
}
else
{
printf("\n %d not found!", target);
}
getchar(); // trap enter
getchar(); // wait
return 0;
}
//----------- Java application-------------
Binary Search in Java
import java.util.*; public class BinarySearch { public static void main(String[] args) { int[] intArray = new int[10]; int searchValue = 0, index; System.out.println("Enter 10 numbers"); Scanner input = new Scanner(System.in); for (int i = 0; i < intArray.length; i++) { intArray[i] = input.nextInt(); } System.out.print("Enter a number to search for: "); searchValue = input.nextInt(); index = binarySearch(intArray, searchValue); if (index != -1) { System.out.println("Found at index: " + index); } else { System.out.println("Not Found"); } } static int binarySearch(int[] search, int find) { int start, end, midPt; start = 0; end = search.length - 1; while (start <= end) { midPt = (start + end) / 2; if (search[midPt] == find) { return midPt; } else if (search[midPt] < find) { start = midPt + 1; } else { end = midPt - 1; } } return -1; } }Output
Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10 Enter a number to search for:5 Found at index: 4
No comments:
Post a Comment