fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. int szuk_rek(int *A, int X, int L, int R);
  6.  
  7. int main(){
  8.  
  9. int A[9] = {-1,2,6,7,12,15,60,428,338};
  10. printf("liczba jest w A[%d] = %d", szuk_rek(A, 12, 0, 6), A[szuk_rek(A, 11, 0, 9)]);
  11.  
  12.  
  13.  
  14. return 0;
  15. }
  16.  
  17. int szuk_rek(int *A, int X, int L, int R){
  18. printf("L =%d R = %d\n", L, R);
  19. if(A[L] > X || A[R] < X){
  20. return 0;
  21. }
  22. if(A[R]==X){
  23. return R;
  24. }
  25. if(L > R){
  26. return -1;
  27. }
  28. if( L >= (R-1) && A[L] != X){
  29. printf("jestem tu, powinno wywalic blad\n");
  30. return -1;
  31. }
  32.  
  33. if (A[(L+R)/2]== X){
  34. return (L+R)/2;
  35. }
  36.  
  37. if( A[(L+R)/2] < X){
  38. return szuk_rek(A, X, (L + R)/2, R);
  39. }
  40. if( A[(L+R)/2] > X){
  41. return szuk_rek(A, X, L, (L + R)/2);
  42. }
  43.  
  44. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
L =0 R = 9
L =0 R = 6
L =3 R = 6
liczba jest w A[4] = -1