Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int t[35], d;
  6.  
  7. int qsearch ( int L, int P , int k )
  8. {
  9. int m = L;
  10. for ( int i = m+1; i <= P; i++ )
  11. if ( t[i] < t[L] ) swap ( t[++m], t[i] );
  12. swap ( t[L], t[m] );
  13. if ( m - L + 1 == k ) return t[m];
  14. if ( m - L + 1 > k ) return qsearch ( L, m - 1, k );
  15. if ( m - L + 1 < k ) return qsearch ( m + 1, P, k - ( m - L + 1 ) );
  16. }
  17. int main()
  18. {
  19. srand(time(NULL));
  20. for ( int i = 0; i < 30; i++ )
  21. t[i] = rand() %90 +10;
  22.  
  23. for ( int i = 0; i < 30; i++ )
  24. cout << t[i] << " ";
  25.  
  26. cout << endl;
  27.  
  28. for ( int i = 0; i < 30; i++ )
  29. {
  30. cout << qsearch( 0, 29, i + 1 ) << " ";
  31. random_shuffle( t, t + 29 );
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement