SEARCHING in c++ language in hindi , Searching operation , Linear , Binary search example program code

Linear , Binary search example program code , SEARCHING in c++ language in hindi , Searching operation :-
इससे पहले के article मे , c++ language के basic उदाहरनो को discuss किया था अब इस article मे searching algorithm को discuss किया जाता है |
Searching operation
searching operation का मतलब है किसि data type जैसे array या stack या queue मे से एक data values को find किया जाता है | इसके लिए programming language मे दो algorithm को use किया गया है |
Linear searching 
linear searching मे किसी data type मे से element को find करने के लिए compression operation को use किया जाता है |इसलिए लिए निन्म step को फोलूव किया जाता है :
1.सबसे पहले searching को array के left most element से start किया जाता है | जिसे array के सभी elements से compare किया जाता है |
2.अगर search element array के element से match होता है तब इसकी position return हो जाती है |
3.अगर search element array के किसी भी element से match नहीं होता है तब इसकी ‘-1’ return हो जाती है |
/*##Simple Searching In Array* C++ Example*/
/*##Searching Programs in C++, Array Example Programs in C++*/
// Header Files
#include <iostream>
#include<conio.h>
using namespace std;
#define size 5
int main()
{
int array [size], i ,search_item ;
cout<<“Simple C++ Example Program for Linear Searching In Array\n”;
// Read Input
for (i = 0; i < size; i++)
{
cout<<“Enter the Number : “<< (i+1) <<”  : “;
cin>>array[i];
}
cout<<“Enter the key\n”;
cin>>search_item ;
/*  Simple Search with Position */
for (i = 0; i < size; i++)
{
if(array[i] == search_item )
{
cout<<“Search Element Found . Position Is :”<< (i+1) <<” \n”;
break;
}
if(i == size – 1)
{
cout<<“Search Element is not in Array.\n”;
}
}
}
 
Binary search
binary search , linear search से काफी advance होता है | इसका complexity भी बहुत कम होती है | लेकिन ये search तभी perform किया जाता है जब array के element किसी order मे sort हो |
linear array की तरह , binary search मे searching array के first element से start नहीं होता है | बल्कि searching array के middle से start होता है  | अगर search _element array मे उपस्थित होता है तब इसका position return होता है |
अगर middle element की value searched element से बड़ा होता है तब searching left side array मे होता है अगर अगर middle element की value searched element से बड़ा नहीं  होता है तब searching right side array मे होता है | इस case मे बाकि के side के array portion को eliminate हो जाता है |
नीचे दिए गये उदाहरन मे binary searching के code को explain किया गया है :
#include <iostream>
using namespace std;
int binarySearch(int [], int, int);
const int size = 20;
int main() {
// Create an array of numbers sorted in ascending order
int array[size] = {101, 142, 147, 189, 199, 207, 222,
234, 289, 296, 310, 319, 388, 394,
417, 429, 447, 521, 536, 600 };
int search_item , // Holds the search_item to search for
results; // Holds the search results
cout << “Enter the search item you wish to search for: “;
cin >> search_item ;
output = binarySearch(array, size, search_item);
// If binarySearch returned -1, the ID was not found
if (output == -1)
cout << “That number does not exist in the array.\n”;
else {
cout << “search item ” << search_item << ” was found in element “
<< output << ” of the array.\n”;
}
getch();
}
int binarySearch(int array[], int size, int value) {
int f= 0, // farray element
l= size – 1, // larray element
m, // Midpoint of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && f<= l) {
m= (f+ l) / 2; // Calculate midpoint
if (array[m] == value) // If value is found at mid {
found = true;
position = m;
}   else if (array[m] > value) // If value is in lower half
l= m- 1;
else
f= m+ 1; // If value is in upper half
}
return position;
}
इस उदाहरन मे
101, 142, 147, 189, 199, 207, 222,234, 289, 296, 310, 319, 388, 394,417, 429, 447, 521, 536, 600 array होता है
यूजर द्वारा serach item 310 है |
पहले pass मे
20 element है | तब इसका comparison 296 से होता है | यहा पर 310 के value 296 से बड़ी होती है तब next comparison array के right side portion मे होता है | 310, 319, 388, 394,417, 429, 447, 521, 536, 600 होता है |
9 element है | तब इसका comparison 417 से होता है | यहा पर 310 के value 417 से छोटी होती है तब next comparison array के left side portion मे होता है | 310, 319, 388, 394 होता है |
4 element है | तब इसका comparison 388 से होता है | यहा पर 310 के value 388 से छोटी होती है तब next comparison array के left side portion मे होता है | 310, 319 होता है |
इस article मे searching operation को discuss किया है |