#include<iostream>
using namespace std;
int partition( int arr[], int s, int e) {
int pivot = arr[s];
int cnt = 0;
for(int i = s+1; i<=e; i++) {
if(arr[i] <=pivot) {
cnt++;
}
}
//place pivot at right position
int pivotIndex = s + cnt;
swap(arr[pivotIndex], arr[s]);
//left and right wala part smbhal lete h
int i = s, j = e;
while(i < pivotIndex && j > pivotIndex) {
while(arr[i] <= pivot)
{
i++;
}
while(arr[j] > pivot) {
j--;
}
if(i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quickSort(int arr[], int s, int e) {
//base case
if(s >= e)
return ;
//partitioon karenfe
int p = partition(arr, s, e);
//left part sort karo
quickSort(arr, s, p-1);
//right wala part sort karo
quickSort(arr, p+1, e);
}
int main() {
int arr[10] = {2,4,1,6,9 ,9,9,9,9,9};
int n = 10;
quickSort(arr, 0, n-1);
for(int i=0; i<n; i++)
{
cout << arr[i] << " ";
} cout << endl;
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKaW50IHBhcnRpdGlvbiggaW50IGFycltdLCBpbnQgcywgaW50IGUpIHsKCiAgICBpbnQgcGl2b3QgPSBhcnJbc107CgogICAgaW50IGNudCA9IDA7CiAgICBmb3IoaW50IGkgPSBzKzE7IGk8PWU7IGkrKykgewogICAgICAgIGlmKGFycltpXSA8PXBpdm90KSB7CiAgICAgICAgICAgIGNudCsrOwogICAgICAgIH0KICAgIH0KCiAgICAvL3BsYWNlIHBpdm90IGF0IHJpZ2h0IHBvc2l0aW9uCiAgICBpbnQgcGl2b3RJbmRleCA9IHMgKyBjbnQ7CiAgICBzd2FwKGFycltwaXZvdEluZGV4XSwgYXJyW3NdKTsKCiAgICAvL2xlZnQgYW5kIHJpZ2h0IHdhbGEgcGFydCBzbWJoYWwgbGV0ZSBoIAogICAgaW50IGkgPSBzLCBqID0gZTsKCiAgICB3aGlsZShpIDwgcGl2b3RJbmRleCAmJiBqID4gcGl2b3RJbmRleCkgewoKICAgICAgICB3aGlsZShhcnJbaV0gPD0gcGl2b3QpIAogICAgICAgIHsKICAgICAgICAgICAgaSsrOwogICAgICAgIH0KCiAgICAgICAgd2hpbGUoYXJyW2pdID4gcGl2b3QpIHsKICAgICAgICAgICAgai0tOwogICAgICAgIH0KCiAgICAgICAgaWYoaSA8IHBpdm90SW5kZXggJiYgaiA+IHBpdm90SW5kZXgpIHsKICAgICAgICAgICAgc3dhcChhcnJbaSsrXSwgYXJyW2otLV0pOwogICAgICAgIH0KCiAgICB9CgogICAgcmV0dXJuIHBpdm90SW5kZXg7Cgp9Cgp2b2lkIHF1aWNrU29ydChpbnQgYXJyW10sIGludCBzLCBpbnQgZSkgewoKICAgIC8vYmFzZSBjYXNlCiAgICBpZihzID49IGUpIAogICAgICAgIHJldHVybiA7CgogICAgLy9wYXJ0aXRpb29uIGthcmVuZmUKICAgIGludCBwID0gcGFydGl0aW9uKGFyciwgcywgZSk7CgogICAgLy9sZWZ0IHBhcnQgc29ydCBrYXJvCiAgICBxdWlja1NvcnQoYXJyLCBzLCBwLTEpOwoKICAgIC8vcmlnaHQgd2FsYSBwYXJ0IHNvcnQga2FybwogICAgcXVpY2tTb3J0KGFyciwgcCsxLCBlKTsKCn0KCmludCBtYWluKCkgewoKICAgIGludCBhcnJbMTBdID0gezIsNCwxLDYsOSAsOSw5LDksOSw5fTsKICAgIGludCBuID0gMTA7CgogICAgcXVpY2tTb3J0KGFyciwgMCwgbi0xKTsKCiAgICBmb3IoaW50IGk9MDsgaTxuOyBpKyspIAogICAgewogICAgICAgIGNvdXQgPDwgYXJyW2ldIDw8ICIgIjsKICAgIH0gY291dCA8PCBlbmRsOwoKCiAgICByZXR1cm4gMDsKfQ==