|
1 | 1 | package com.thealgorithms.maths;
|
2 | 2 |
|
3 |
| -import java.util.Arrays; |
4 | 3 | import java.util.Random;
|
5 | 4 |
|
6 | 5 | /**
|
7 |
| - * use quick sort algorithm to get kth largest or kth smallest element in given array |
| 6 | + * Use a quicksort-based approach to identify the k-th largest or k-th max element within the provided array. |
8 | 7 | */
|
9 | 8 | public final class FindKthNumber {
|
10 | 9 | private FindKthNumber() {
|
11 | 10 | }
|
12 | 11 |
|
13 | 12 | private static final Random RANDOM = new Random();
|
14 | 13 |
|
15 |
| - public static void main(String[] args) { |
16 |
| - /* generate an array with random size and random elements */ |
17 |
| - int[] nums = generateArray(100); |
18 |
| - |
19 |
| - /* get 3th largest element */ |
20 |
| - int kth = 3; |
21 |
| - int kthMaxIndex = nums.length - kth; |
22 |
| - int targetMax = findKthMax(nums, kthMaxIndex); |
23 |
| - |
24 |
| - /* get 3th smallest element */ |
25 |
| - int kthMinIndex = kth - 1; |
26 |
| - int targetMin = findKthMax(nums, kthMinIndex); |
| 14 | + public static int findKthMax(int[] array, int k) { |
| 15 | + if (k <= 0 || k > array.length) { |
| 16 | + throw new IllegalArgumentException("k must be between 1 and the size of the array"); |
| 17 | + } |
27 | 18 |
|
28 |
| - Arrays.sort(nums); |
29 |
| - assert nums[kthMaxIndex] == targetMax; |
30 |
| - assert nums[kthMinIndex] == targetMin; |
| 19 | + // Convert k-th largest to index for QuickSelect |
| 20 | + return quickSelect(array, 0, array.length - 1, array.length - k); |
31 | 21 | }
|
32 | 22 |
|
33 |
| - private static int[] generateArray(int capacity) { |
34 |
| - int size = RANDOM.nextInt(capacity) + 1; |
35 |
| - int[] array = new int[size]; |
36 |
| - |
37 |
| - for (int i = 0; i < size; i++) { |
38 |
| - array[i] = RANDOM.nextInt() % 100; |
| 23 | + private static int quickSelect(int[] array, int left, int right, int kSmallest) { |
| 24 | + if (left == right) { |
| 25 | + return array[left]; |
39 | 26 | }
|
40 |
| - return array; |
41 |
| - } |
42 | 27 |
|
43 |
| - private static int findKthMax(int[] nums, int k) { |
44 |
| - int start = 0; |
45 |
| - int end = nums.length; |
46 |
| - while (start < end) { |
47 |
| - int pivot = partition(nums, start, end); |
48 |
| - if (k == pivot) { |
49 |
| - return nums[pivot]; |
50 |
| - } else if (k > pivot) { |
51 |
| - start = pivot + 1; |
52 |
| - } else { |
53 |
| - end = pivot; |
54 |
| - } |
| 28 | + // Randomly select a pivot index |
| 29 | + int pivotIndex = left + RANDOM.nextInt(right - left + 1); |
| 30 | + pivotIndex = partition(array, left, right, pivotIndex); |
| 31 | + |
| 32 | + if (kSmallest == pivotIndex) { |
| 33 | + return array[kSmallest]; |
| 34 | + } else if (kSmallest < pivotIndex) { |
| 35 | + return quickSelect(array, left, pivotIndex - 1, kSmallest); |
| 36 | + } else { |
| 37 | + return quickSelect(array, pivotIndex + 1, right, kSmallest); |
55 | 38 | }
|
56 |
| - return -1; |
57 | 39 | }
|
58 | 40 |
|
59 |
| - private static int partition(int[] nums, int start, int end) { |
60 |
| - int pivot = nums[start]; |
61 |
| - int j = start; |
62 |
| - for (int i = start + 1; i < end; i++) { |
63 |
| - if (nums[i] < pivot) { |
64 |
| - j++; |
65 |
| - swap(nums, i, j); |
| 41 | + private static int partition(int[] array, int left, int right, int pivotIndex) { |
| 42 | + int pivotValue = array[pivotIndex]; |
| 43 | + // Move pivot to end |
| 44 | + swap(array, pivotIndex, right); |
| 45 | + int storeIndex = left; |
| 46 | + |
| 47 | + // Move all smaller elements to the left |
| 48 | + for (int i = left; i < right; i++) { |
| 49 | + if (array[i] < pivotValue) { |
| 50 | + swap(array, storeIndex, i); |
| 51 | + storeIndex++; |
66 | 52 | }
|
67 | 53 | }
|
68 |
| - swap(nums, start, j); |
69 |
| - return j; |
| 54 | + |
| 55 | + // Move pivot to its final place |
| 56 | + swap(array, storeIndex, right); |
| 57 | + return storeIndex; |
70 | 58 | }
|
71 | 59 |
|
72 |
| - private static void swap(int[] nums, int a, int b) { |
73 |
| - int tmp = nums[a]; |
74 |
| - nums[a] = nums[b]; |
75 |
| - nums[b] = tmp; |
| 60 | + private static void swap(int[] array, int i, int j) { |
| 61 | + int temp = array[i]; |
| 62 | + array[i] = array[j]; |
| 63 | + array[j] = temp; |
76 | 64 | }
|
77 | 65 | }
|
0 commit comments