JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

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 किया है |
Sbistudy

Recent Posts

सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है

सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…

13 hours ago

मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the

marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…

13 hours ago

राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi

sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…

2 days ago

गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi

gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…

2 days ago

Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन

वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…

3 months ago

polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten

get all types and chapters polity notes pdf in hindi for upsc , SSC ,…

3 months ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now