Advertisement
Manavard

Untitled

Dec 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. public class QuickSort {
  2. public static void quickSort(int[] arr, int low, int high) {
  3. if (arr.length == 0)
  4. return;
  5. if (low >= high)
  6. return;
  7. int middle = (low + high) / 2;
  8. int cntr = arr[middle];
  9. int i = low, j = high;
  10. while (i <= j) {
  11. while (arr[i] < cntr) {
  12. i++;
  13. }
  14. while (arr[j] > cntr) {
  15. j--;
  16. }
  17. if (i <= j) {
  18. int tmp = arr[i];
  19. arr[i] = arr[j];
  20. arr[j] = tmp;
  21. i++;
  22. j--;
  23. }
  24. }
  25. if (low < j)
  26. quickSort(arr, low, j);
  27. if (high > i)
  28. quickSort(arr, i, high);
  29. }
  30.  
  31. public static void main(String args[]) {
  32. Random rnd = new Random();
  33. int arr[] = new int[10];
  34. for(int i=0; i<arr.length; i++) {
  35. arr[i] = rnd.nextInt(1000);
  36. }
  37. System.out.println("Было");
  38. System.out.println(Arrays.toString(arr));
  39. int low = 0;
  40. int high = arr.length - 1;
  41. quickSort(arr, low, high);
  42. System.out.println("Стало");
  43. System.out.println(Arrays.toString(arr));
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement