Примери
Linear Search in C++
#include <iostream> using namespace std; void linearno(int a[], int m, int i, int n) { int t = 0; for (i = 0; i < n ; i++) if (m == a[i] ) { t= 1; break; } if (t == 1) cout<<"Elementot e na pozicija "<<i; else cout<<"Elementot ne e prisuten vo nizata."; } int main() { int n, a[100]; int i, m, t = 0; cout<<"Vnesi go n "; cin>>n; cout<<"Vnesi gi elementite"<<endl; for (i = 0; i < n; i++) cin>> a[i]; cout<<"Vnesi go elementot sto go baras "; cin>>m; linearno(a, m,i,n); return 0; }
Output:
Vnesi go n 5 Vnesi gi elementite 1 3 -2 45 7 Vnesi go elementot sto go baras 2 Elementot ne e prisuten vo nizata.
Vnesi go n 4 Vnesi gi elementite 9 0 -2 1 Vnesi go elementot sto go baras 0 Elementot e na pozicija 1
Linear Search with Duplicate Element
#include<iostream> using namespace std; int main() { int arr[100], tot, i, num, arrTemp[50], j=0, chk=0; cout<<"Enter the Size for Array Size: "; cin>>tot; cout<<"Enter "<<tot<<" Array Elements: "; for(i=0; i<tot; i++) cin>>arr[i]; cout<<"\nEnter the Number to Search: "; cin>>num; for(i=0; i<tot; i++) { if(arr[i]==num) { arrTemp[j] = i; j++; chk++; } } if(chk>0) { cout<<"\nNumber Found at Index No. "; tot = chk; for(i=0; i<tot; i++) cout<<arrTemp[i]<<" "; } else cout<<"\nNumber doesn't Found!"; cout<<endl; return 0; }Enter the Size for Array Size: 5 Enter 5 Array Elements: 1 3 45 1 1 Enter the Number to Search: 1 Number Found at Index No. 0 3 4
We have implemented a linear search using C++
#include <iostream>
#include <string>
using
namespace
std;
int
main()
{
int
myarray[10] = {21,43,23,54,75,13,5,8,25,10};
int
key,loc;
cout<<
"The input array is"
<<endl;
for
(
int
i=0;i<10;i++){
cout<<myarray[i]<<
" "
;
}
cout<<endl;
cout<<
"Enter the key to be searched : "
; cin>>key;
for
(
int
i = 0; i< 10; i++)
{
if
(myarray[i] == key)
{
loc = i+1;
break
;
}
else
loc = 0;
}
if
(loc != 0)
{
cout<<
"Key found at position "
<<loc<<
" in the array"
;
}
else
{
cout<<
"Could not find given key in the array"
;
}
}
Output:
The input array is
21 43 23 54 75 13 5 8 25 10
Enter the key to be searched : 3
Could not find given key in the arrayThe input array is
21 43 23 54 75 13 5 8 25 10
Enter the key to be searched: 75
Key found at position 5 in the array
#include using namespace std; int binarySearch(int arr[], int p, int r, int num) { if (p <= r) { int mid = (p + r)/2; if (arr[mid] == num) return mid ; if (arr[mid] > num) return binarySearch(arr, p, mid-1, num); if (arr[mid] < num) return binarySearch(arr, mid+1, r, num); } return -1; } int main(void) { int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40}; int n = sizeof(arr)/ sizeof(arr[0]); int num; cout << "Enter the number to search: \n"; cin >> num; int index = binarySearch (arr, 0, n-1, num); if(index == -1){ cout<< num <<" is not present in the array"; }else{ cout<< num <<" is present at index "<< index <<" in the array"; } return 0; }
Output
Enter the numberto search
20
20 is present at index 5 in the array
#include <iostream> #include <string> using namespace std; int binarySearch( int myarray[], int beg, int end, int key) { int mid; if (end >= beg) { mid = (beg + end)/2; if (myarray[mid] == key) { return mid+1; } else if (myarray[mid] < key) { return binarySearch(myarray,mid+1,end,key); } else { return binarySearch(myarray,beg,mid-1,key); } } return -1; } int main () { int myarray[10] = {5,8,10,13,21,23,25,43,54,75}; int key, location=-1; cout<< "The input array is" <<endl; for ( int i=0;i<10;i++){ cout<<myarray[i]<< " " ; } cout<<endl; cout<< "Enter the key that is to be searched:" ; cin>>key; location = binarySearch(myarray, 0, 9, key); if (location != -1) { cout<< "Key found at location " <<location; } else { cout<< "Requested key not found" ; } } |
Output:
The input array is
5 8 10 13 21 23 25 43 54 75
Enter the key that is to be searched:21
Key found at location 5
Задачи
No comments:
Post a Comment