#include <stdio.h>
int partition(int array[], int start, int end) {
int pivot = array[end];
int i = start - 1;
for (int j = start; j < end; j++) {
if (array[j] >= pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[end];
array[end] = temp;
return i + 1;
}
int quickSelect(int array[], int start, int end, int k) {
if (start <= end) {
int pivotPos = partition(array, start, end);
if (pivotPos == k) {
return array[pivotPos];
} else if (pivotPos < k) {
return quickSelect(array, pivotPos + 1, end, k);
} else {
return quickSelect(array, start, pivotPos - 1, k);
}
}
return -1;
}
int findKthLargest(int array[], int size, int k) {
if (k > 0 && k <= size) {
return quickSelect(array, 0, size - 1, k - 1);
}
return -1;
}
int main() {
int array[] = {5, 4, 9, 10, 4, 3, 5};
int size = sizeof(array) / sizeof(int);
int k = 3;
int kthLargest = findKthLargest(array, size, k);
if (kthLargest != -1) {
printf("The %dth largest element is: %d\n", k, kthLargest);
} else {
printf("Invalid value of k.\n");
}
return 0;
}