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