Untitled
unknown
plain_text
4 years ago
1.1 kB
7
Indexable
#include<iostream> #define MAX_SIZE 1000 using namespace std; void SORT(int array[], int size){ //SORT for(int i=0; i<size; i++){ for(int j=i+1; j<size; j++){ if(array[i] > array[j]){ int temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } int BINARY_SEARCH(int item, int array[], int size){ SORT(array, size); int low=0, high=size-1, mid=0; while(low <= high){ mid = (low+high)/2; if(array[mid] == item){ return 0; } else if(array[mid] > item) high = mid-1; else if(array[mid] < item) low = mid+1; } if(low > high) return 1; } int main(){ int arr[MAX_SIZE], n, x; cout<<"Enter the size of the array: "; cin>>n; cout<<"Enter the elements of the array: "; for(int i=0; i<n; i++){ cin>>arr[i]; } cout<<"Which element you want to search: "; cin>>x; if(BINARY_SEARCH(x,arr,n) == 0) cout<<"\nFOUND\n"; else cout<<"\nNOT FOUND\n"; return 0; }
Editor is loading...