Posts

Showing posts with the label Binary Search

Binary search in C++

 #include<iostream> using namespace std ; int main () { int i , found = 0 , num , n , arr [ 100 ], mid , low , high ;  // declaration and initialization of variables cout << "enter the size of array\n" ;    //prints the msg in double colon cin >> n ;    //stores the value in n for ( i = 0 ; i < n ; i ++)   //for loop { cout << "\n arr[" <<i<< "] = " ;      //for block cin >> arr [ i ];        //for block } cout << "Enter number to be search\n" ;   //prints the msg in double colon cin >> num ;    //stores the value in n low = 0 ;    //initialization of low  to 0 high = n - 1 ;     //initialization of high to n-1 while ( low <= high )    //while loop { mid =(l ow + high )/ 2 ; if ( arr [ mid ]== num ) { cout << "Enter element is found" ; break ...

Binary search in C

Binary search in C  #include<stdio.h> int main () { int n,i,arr [ 10 ], num,found = 0 ; printf ( "Enter size of array=\n" ); scanf ( "%d" ,&n); printf ( "Enter array element\n" ); for ( i =0;i<n;i++) { printf ( "\narr[%d]=" ,i); scanf ( "%d" ,& arr [ i ]); } printf ( "which no want to find\n" ); scanf ( "%d" , &num ); for ( i = 0 ; i < n ; i ++) { if ( arr [ i ]== num ) { found = 1 ; printf ( "Enter element is found in the array" ); break; } } if ( found ==0) printf ( "Enter element is not found" ); } output: Enter size of array= 5 Enter array element arr[0]=55 arr[1]=33 arr[2]=53 arr[3]=34 arr[4]=44 which no want to find 53 Enter element is found in the array