Advertisement
AgungAlfiansyah

Untitled

Oct 17th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5. const int jdata=1024*1024;
  6. int data[jdata];
  7.  
  8. int binarysearch(int cari){
  9.    //bool found=false;
  10.    int mid;
  11.    int bawah=0;
  12.    int atas=jdata;
  13.    int result=-1;
  14.    
  15.    while(bawah <= atas){
  16.        cout << atas << " " << bawah << endl;
  17.        mid=int((atas+bawah)/2);
  18.        if (data[mid] == cari){
  19.            result=mid; break;
  20.        }
  21.        else if (data[mid]>cari)
  22.             atas = mid;
  23.        else
  24.             bawah = mid;
  25.         }
  26.        
  27.   return result;
  28. }
  29.  
  30. int main() {
  31.    data[0]=0;
  32.    for (int i=1; i<jdata; i++){
  33.       data[i]=data[i-1]+random()%10;
  34. //      cout << data[i]<<endl;
  35.   }
  36.      
  37.    cout << "data ditemukan pada: " << binarysearch(data[1048575]);
  38.    //cout << search(-100);
  39.    
  40.    return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement