fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int find(string a[], int low, int high, string target){
  7. int mid = low + (high-low)/2;
  8. if (low > high)
  9. return -1;
  10. if (a[mid] == target)
  11. return mid;
  12.  
  13. if (a[mid] == ""){
  14. int tmp1 = mid -1 ;
  15. int tmp2 = mid +1 ;
  16. while (a[tmp1] == "" && tmp1>0)
  17. tmp1--;
  18. while (a[tmp2] == "" && tmp2 < 12)
  19. tmp2++;
  20. if (target <= a[tmp1]){
  21. find(a, low, tmp1, target);
  22. }else{
  23. find(a, tmp2, high, target);
  24. }
  25. }
  26.  
  27. std::cout << "find did not return a value.\n" ;
  28. }
  29.  
  30. int main(){
  31. string s[13] = {"at", "", "", "", "ball", "", "", "car", "", "", "dad", "", ""};
  32. cout<< find(s, 0, 12, "car");
  33. }
  34.  
  35.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
find did not return a value.
find did not return a value.
134521504