Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class QuickSort {
- public static void quickSort(int[] arr, int low, int high) {
- if (arr.length == 0)
- return;
- if (low >= high)
- return;
- int middle = (low + high) / 2;
- int cntr = arr[middle];
- int i = low, j = high;
- while (i <= j) {
- while (arr[i] < cntr) {
- i++;
- }
- while (arr[j] > cntr) {
- j--;
- }
- if (i <= j) {
- int tmp = arr[i];
- arr[i] = arr[j];
- arr[j] = tmp;
- i++;
- j--;
- }
- }
- if (low < j)
- quickSort(arr, low, j);
- if (high > i)
- quickSort(arr, i, high);
- }
- public static void main(String args[]) {
- Random rnd = new Random();
- int arr[] = new int[10];
- for(int i=0; i<arr.length; i++) {
- arr[i] = rnd.nextInt(1000);
- }
- System.out.println("Было");
- System.out.println(Arrays.toString(arr));
- int low = 0;
- int high = arr.length - 1;
- quickSort(arr, low, high);
- System.out.println("Стало");
- System.out.println(Arrays.toString(arr));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement