Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. int randomizedPartition(int a[], int left, int right)
  2. {
  3. srand(time(NULL)); //makes use of the computer's internal clock to control the choice of the seed.
  4. int random = left + rand() % (right - left);
  5. printf("%d ", a[random]);
  6.  
  7. int aux = a[random];
  8. a[random] = a[right];
  9. a[right] = aux;
  10.  
  11. return partition(a, left, right);
  12. }
  13. int randomizedQuickSelect(int a[], int left, int right, int y)
  14. {
  15. if (left >= right)
  16. return a[left];
  17. int x = randomizedPartition(a, left, right);
  18. if (x == y)
  19. return a[x];
  20. else
  21. if (x < y)
  22. return randomizedQuickSelect(a, x + 1, right, y);
  23. else
  24. return randomizedQuickSelect(a, left, x - 1, y);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement