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[7] = {1,2,6,8,9,10,12};
  11. printf("liczba jest w A[%d] = %d", szuk_rek(A, 12, 0, 6), A[szuk_rek(A, 12, 0, 6)]);
  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 && A[L] != X){
  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 = 6
L =0 R = 6
liczba jest w A[6] = 12