|
1 | 1 | package com.examplehub.sorts; |
2 | 2 |
|
3 | | -import com.examplehub.utils.SortUtils; |
4 | | - |
5 | 3 | public class QuickSort implements Sort { |
6 | 4 |
|
7 | 5 | @Override |
8 | 6 | public void sort(int[] numbers) { |
9 | 7 | quickSort(numbers, 0, numbers.length - 1); |
10 | 8 | } |
11 | 9 |
|
| 10 | + private int partition(int[] number, int left, int right) { |
| 11 | + int pivot = number[left]; |
| 12 | + while (left < right) { |
| 13 | + while (left < right && number[right] >= pivot) { |
| 14 | + right--; |
| 15 | + } |
| 16 | + number[left] = number[right]; |
| 17 | + |
| 18 | + while (left < right && number[left] <= pivot) { |
| 19 | + left++; |
| 20 | + } |
| 21 | + number[right] = number[left]; |
| 22 | + } |
| 23 | + number[left] = pivot; |
| 24 | + return left; |
| 25 | + } |
| 26 | + |
12 | 27 | /** |
13 | 28 | * QuickSort algorithm implements. |
14 | 29 | * |
15 | 30 | * @param numbers the numbers to be sorted. |
16 | 31 | */ |
17 | 32 | public void quickSort(int[] numbers, int left, int right) { |
18 | | - if (left >= right) { |
19 | | - return; |
20 | | - } |
21 | | - int pivot = numbers[right]; /* pick last element as pivot */ |
22 | | - /* begin partition */ |
23 | | - int i = left; /* left pointer */ |
24 | | - int j = right - 1; /* right pointer */ |
25 | | - while (i <= j) { |
26 | | - while (numbers[i] < pivot) { |
27 | | - i++; /* move i forward */ |
28 | | - } |
29 | | - while (j >= 0 && numbers[j] >= pivot) { |
30 | | - j--; /* move j backward */ |
31 | | - } |
32 | | - if (i < j) { |
33 | | - SortUtils.swap(numbers, i, j); |
34 | | - i++; |
35 | | - j--; |
36 | | - } |
| 33 | + if (left < right) { |
| 34 | + int pivotIndex = partition(numbers, left, right); |
| 35 | + quickSort(numbers, left, pivotIndex - 1); |
| 36 | + quickSort(numbers, pivotIndex + 1, right); |
37 | 37 | } |
38 | | - |
39 | | - SortUtils.swap(numbers, i, right); |
40 | | - |
41 | | - quickSort(numbers, left, i - 1); |
42 | | - quickSort(numbers, i + 1, right); |
43 | 38 | } |
44 | 39 |
|
| 40 | + /** |
| 41 | + * Generic quickSort algorithm implements. |
| 42 | + * |
| 43 | + * @param array the array to be sorted. |
| 44 | + * @param <T> the class of the objects in the array. |
| 45 | + */ |
45 | 46 | @Override |
46 | 47 | public <T extends Comparable<T>> void sort(T[] array) { |
47 | 48 | quickSort(array, 0, array.length - 1); |
48 | 49 | } |
49 | 50 |
|
50 | | - public static <T extends Comparable<T>> void quickSort(T[] array, int left, int right) { |
51 | | - if (left >= right) { |
52 | | - return; |
53 | | - } |
54 | | - T pivot = array[right]; /* pick last element as pivot */ |
55 | | - /* begin partition */ |
56 | | - int i = left; /* left pointer */ |
57 | | - int j = right - 1; /* right pointer */ |
58 | | - while (i <= j) { |
59 | | - while (array[i].compareTo(pivot) < 0) { |
60 | | - i++; /* move i forward */ |
| 51 | + private static <T extends Comparable<T>> int partition(T[] array, int left, int right) { |
| 52 | + T pivot = array[left]; |
| 53 | + while (left < right) { |
| 54 | + while (left < right && array[right].compareTo(pivot) >= 0) { |
| 55 | + right--; |
61 | 56 | } |
62 | | - while (j >= 0 && array[j].compareTo(pivot) >= 0) { |
63 | | - j--; /* move j backward */ |
64 | | - } |
65 | | - if (i < j) { |
66 | | - SortUtils.swap(array, i, j); |
67 | | - i++; |
68 | | - j--; |
| 57 | + array[left] = array[right]; |
| 58 | + |
| 59 | + while (left < right && array[left].compareTo(pivot) <= 0) { |
| 60 | + left++; |
69 | 61 | } |
| 62 | + array[right] = array[left]; |
70 | 63 | } |
| 64 | + array[left] = pivot; |
| 65 | + return left; |
| 66 | + } |
71 | 67 |
|
72 | | - SortUtils.swap(array, i, right); |
73 | | - |
74 | | - quickSort(array, left, i - 1); |
75 | | - quickSort(array, i + 1, right); |
| 68 | + public static <T extends Comparable<T>> void quickSort(T[] array, int left, int right) { |
| 69 | + if (left < right) { |
| 70 | + int pivotIndex = partition(array, left, right); |
| 71 | + quickSort(array, left, pivotIndex - 1); |
| 72 | + quickSort(array, pivotIndex + 1, right); |
| 73 | + } |
76 | 74 | } |
77 | 75 | } |
0 commit comments