Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class QuickSort {
  4. public static void main(String[] args) {
  5. int[] x = {9, 2, 4, 7, 3, 7, 10};
  6. System.out.println(Arrays.toString(x));
  7. int low = 0, high = x.length - 1;
  8. quickSort(x, low, high);
  9. System.out.println(Arrays.toString(x));
  10. }
  11. public static void quickSort(int[] arr, int low, int high) {
  12. if (arr == null || arr.length == 0) return;
  13. if (low >= high) return;
  14. // pick the pivot
  15. int middle = low + (high - low) / 2;
  16. int pivot = arr[middle];
  17. // make left < pivot and right > pivot
  18. int i = low, j = high;
  19. while (i <= j) {
  20. while (arr[i] < pivot) i++;
  21. while (arr[j] > pivot) j--;
  22. if (i <= j) {
  23. int temp = arr[i];
  24. arr[i] = arr[j];
  25. arr[j] = temp;
  26. i++;
  27. j--;
  28. }
  29. }
  30. // recursively sort 2 sub arrays
  31. if (low < j) quickSort(arr, low, j);
  32. if (high > i) quickSort(arr, i, high);
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement