Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main(String[] args) {
  4. System.out.println("Hello World!");
  5. int[][] arraysToSort = {{9, 0, -2, 89, 1, 2, 3, -3, -99, 6},
  6. {5, 3, 1, 2, 2, 1, 0},
  7. {1, 1},
  8. {3, 1, 1},
  9. {2, 1, 1, 2, 1, 2},
  10. {2, 5, 3, 1},
  11. {-1, 3, 2, 4, 0, 1},
  12. {-1, 0}
  13. };
  14. for (int i = 0; i < arraysToSort.length; i++) {
  15. int[] arrayToSort = arraysToSort[i];
  16. quickSort(arrayToSort,0,arrayToSort.length-1);
  17. printArray(arrayToSort);
  18. }
  19. }
  20.  
  21. private static void printArray(int[] arrayToSort) {
  22. System.out.println("Start");
  23. for (int index = 0; index < arrayToSort.length; index++) {
  24. System.out.print(arrayToSort[index] + " ");
  25. }
  26. System.out.println("\nEnd");
  27. }
  28.  
  29. private static void quickSort(int[] array,int start, int end) {
  30. if (start >= end)
  31. return;
  32. int i = start, j = end;
  33. int cur = i - (i - j) / 2;
  34. while (i < j) {
  35. while (i < cur && (array[i] <= array[cur])) {
  36. i++;
  37. }
  38. while (j > cur && (array[cur] <= array[j])) {
  39. j--;
  40. }
  41. if (i < j) {
  42. swapItems(array,i,j);
  43. if (i == cur)
  44. cur = j;
  45. else if (j == cur)
  46. cur = i;
  47. }
  48. }
  49. quickSort(array,start, cur);
  50. quickSort(array,cur+1, end);
  51. }
  52.  
  53. private static void swapItems(int[] array, int start, int end) {
  54. int temp = array[start];
  55. array[start] = array[end];
  56. array[end] = temp;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement