#include<stdio.h>
void swap(int *a, int *b){
int temp = *a;
*a=*b;
*b=temp;
}
int partition(int arr[], int l, int r){
int pivot = arr[l];
int left,right;
for(left=l+1,right=r;left<=right;){
if(arr[left]>pivot && arr[right]<=pivot){
swap(&arr[left],&arr[right]);
}
if(arr[left]<=pivot) {
left++;
}
if(arr[right]>pivot) {
right--;
}
}
swap(&arr[l],&arr[right]);
// printf("%d\n",right);
return right;
}
void quicksort(int arr[], int l, int r){
int p;
if (l < r){
p = partition(arr,l,r);
quicksort(arr,l,p-1);
quicksort(arr,p+1,r);
}
}
int main(){
int i,size;
// int arr[] = { 10, 20, 7 , 5, 24 , 17, 13, 56, 38, 12 , 29, 46};
int arr[] = {0,2,2};
// int arr[] = {2,2,1,0};
size = sizeof(arr)/sizeof(arr[0]);
printf("The array before sorting is --->"); for(i=0;i<size;i++){
}
quicksort(arr,0,size-1);
printf("The array after sorting is --->"); for(i=0;i<size;i++){
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCnZvaWQgc3dhcChpbnQgKmEsIGludCAqYil7CiAgICAgICAgaW50IHRlbXAgPSAqYTsKICAgICAgICAqYT0qYjsKICAgICAgICAqYj10ZW1wOwp9CgppbnQgcGFydGl0aW9uKGludCBhcnJbXSwgaW50IGwsIGludCByKXsKICAgICAgICBpbnQgcGl2b3QgPSBhcnJbbF07CiAgICAgICAgaW50IGxlZnQscmlnaHQ7CiAgICAgICAgZm9yKGxlZnQ9bCsxLHJpZ2h0PXI7bGVmdDw9cmlnaHQ7KXsKICAgICAgICAgICAgaWYoYXJyW2xlZnRdPnBpdm90ICYmIGFycltyaWdodF08PXBpdm90KXsKICAgICAgICAgICAgICAgICAgICBzd2FwKCZhcnJbbGVmdF0sJmFycltyaWdodF0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKGFycltsZWZ0XTw9cGl2b3QpIHsKICAgICAgICAgICAgICAgICAgICBsZWZ0Kys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoYXJyW3JpZ2h0XT5waXZvdCkgewogICAgICAgICAgICAgICAgICAgIHJpZ2h0LS07CiAgICAgICAgICAgIH0KICAgIH0KICAgICAgICAgICAgc3dhcCgmYXJyW2xdLCZhcnJbcmlnaHRdKTsKLy8gICAgICAgICAgICBwcmludGYoIiVkXG4iLHJpZ2h0KTsKICAgICAgICAgICAgcmV0dXJuIHJpZ2h0Owp9CgogICAgdm9pZCBxdWlja3NvcnQoaW50IGFycltdLCBpbnQgbCwgaW50IHIpewogICAgICAgICAgICBpbnQgcDsKICAgICAgICBpZiAobCA8IHIpewogICAgICAgICAgICAgICAgcCA9IHBhcnRpdGlvbihhcnIsbCxyKTsKICAgICAgICAgICAgICAgIHF1aWNrc29ydChhcnIsbCxwLTEpOwogICAgICAgICAgICAgICAgcXVpY2tzb3J0KGFycixwKzEscik7CiAgICAgICAgfQp9CgogICBpbnQgbWFpbigpewogICAgICAgICAgICBpbnQgaSxzaXplOwogICAgICAgICAvLyBpbnQgYXJyW10gPSB7IDEwLCAyMCwgNyAsIDUsIDI0ICwgMTcsIDEzLCA1NiwgMzgsIDEyICwgMjksIDQ2fTsKICAgICAgICAgICAgaW50IGFycltdID0gezAsMiwyfTsKICAgICAgICAvLyAgaW50IGFycltdID0gezIsMiwxLDB9OwogICAgICAgICAgICBzaXplID0gc2l6ZW9mKGFycikvc2l6ZW9mKGFyclswXSk7CiAgICAgICAgICAgIHByaW50ZigiVGhlIGFycmF5IGJlZm9yZSBzb3J0aW5nIGlzIC0tLT4iKTsKICAgICAgICAgICAgZm9yKGk9MDtpPHNpemU7aSsrKXsKICAgICAgICAgICAgICAgICAgICBwcmludGYoIi0tLT4lZCIsYXJyW2ldKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBwcmludGYoIi0tLT5FTkRcblxuIik7CiAgICAgICAgICAgIHF1aWNrc29ydChhcnIsMCxzaXplLTEpOwogICAgICAgICAgICBwcmludGYoIlRoZSBhcnJheSBhZnRlciBzb3J0aW5nIGlzIC0tLT4iKTsKICAgICAgICAgICAgZm9yKGk9MDtpPHNpemU7aSsrKXsKICAgICAgICAgICAgICAgICAgICBwcmludGYoIi0tLT4lZCIsYXJyW2ldKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBwcmludGYoIi0tLT5FTkQiKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIHJldHVybiAwOwogICAgfQ==