#include<stdio.h>
void swap(int *i, int *j)
{
int temp = *i;
*i = *j;
*j = temp;
}
int partition(int arr[], int start, int end)
{
int pivot = arr[end];
int i = start;
int j = end-1;
while(i<=j)
{
while(arr[i]<pivot)
i++;
while(arr[j]>pivot)
j--;
if(i<=j)
{
swap(&arr[i],&arr[j]);
i++;
j--;
}
}
swap(&arr[i],&arr[end]);
return i;
}
void performQuickSort(int arr[], int start, int end)
{
if(start<end)
{
int p = partition(arr, start, end);
performQuickSort(arr, start, p-1);
performQuickSort(arr, p+1, end);
}
}
void quickSort(int arr[], int size)
{
performQuickSort(arr, 0, size-1);
}
// driver program
int main(void)
{
int i;
int arr[10] = {2, 6, 4, 10, 8, 1, 9, 5, 3, 7};
quickSort(arr,10);
printf("The 5th largest number in array is:- %d", arr
[10 - 5]); return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KIAp2b2lkIHN3YXAoaW50ICppLCBpbnQgKmopCnsKICAgIGludCB0ZW1wID0gKmk7CiAgICAqaSA9ICpqOwogICAgKmogPSB0ZW1wOwp9CiAKaW50IHBhcnRpdGlvbihpbnQgYXJyW10sIGludCBzdGFydCwgaW50IGVuZCkKewogICAgaW50IHBpdm90ID0gYXJyW2VuZF07CiAgICBpbnQgaSA9IHN0YXJ0OwogICAgaW50IGogPSBlbmQtMTsKICAgIHdoaWxlKGk8PWopCiAgICB7CiAgICAgICAgd2hpbGUoYXJyW2ldPHBpdm90KQogICAgICAgICAgICBpKys7CiAJCXdoaWxlKGFycltqXT5waXZvdCkKICAgICAgICAgICAgai0tOwogCQlpZihpPD1qKQogICAgICAgIHsKICAgICAgICAgICAgc3dhcCgmYXJyW2ldLCZhcnJbal0pOwogICAgICAgICAgICBpKys7CiAgICAgICAgICAgIGotLTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIHN3YXAoJmFycltpXSwmYXJyW2VuZF0pOwogICAgcmV0dXJuIGk7Cn0KIAp2b2lkIHBlcmZvcm1RdWlja1NvcnQoaW50IGFycltdLCBpbnQgc3RhcnQsIGludCBlbmQpCnsKICAgIGlmKHN0YXJ0PGVuZCkKICAgIHsKICAgICAgICBpbnQgcCA9IHBhcnRpdGlvbihhcnIsIHN0YXJ0LCBlbmQpOwogICAgICAgIHBlcmZvcm1RdWlja1NvcnQoYXJyLCBzdGFydCwgcC0xKTsKICAgICAgICBwZXJmb3JtUXVpY2tTb3J0KGFyciwgcCsxLCBlbmQpOwogICAgfQp9Cgp2b2lkIHF1aWNrU29ydChpbnQgYXJyW10sIGludCBzaXplKQp7CiAgICBwZXJmb3JtUXVpY2tTb3J0KGFyciwgMCwgc2l6ZS0xKTsKfQogCi8vIGRyaXZlciBwcm9ncmFtCmludCBtYWluKHZvaWQpCnsKICAgIGludCBpOwogICAgaW50IGFyclsxMF0gPSB7MiwgNiwgNCwgMTAsIDgsIDEsIDksIDUsIDMsIDd9OwogCiAgICBxdWlja1NvcnQoYXJyLDEwKTsKICAgIAogICAgcHJpbnRmKCJUaGUgNXRoIGxhcmdlc3QgbnVtYmVyIGluIGFycmF5IGlzOi0gICVkIiwgYXJyWzEwIC0gNV0pOwogICAgcmV0dXJuIDA7Cn0K