Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. public class QuickSort {
  2. private int[] arr;
  3.  
  4. public QuickSort(int[] arr) {
  5. this.arr = arr;
  6. }
  7.  
  8. public int[] sort() {
  9. quickSort(0, arr.length - 1);
  10.  
  11. return arr;
  12. }
  13.  
  14. private void quickSort(int start, int end) {
  15. if (start < end) {
  16. int pIndex = partition(start, end);
  17.  
  18. quickSort(start, pIndex - 1);
  19. quickSort(pIndex + 1, end);
  20. }
  21. }
  22.  
  23. private int partition(int start, int end) {
  24. int pivot = arr[end];
  25. int pIndex = start;
  26.  
  27. for (int i = start; i < end; i++) {
  28. if (arr[i] <= pivot) {
  29. swap(i, pIndex);
  30. pIndex++;
  31. }
  32. }
  33.  
  34. swap(pIndex, end);
  35.  
  36. return pIndex;
  37. }
  38.  
  39. private void swap(int a, int b) {
  40. int temp = arr[a];
  41. arr[a] = arr[b];
  42. arr[b] = temp;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement