#include <bits/stdc++.h>
using namespace std;
int arr[] = {14,4,5,21,1,33,23},
n = sizeof( arr )/sizeof( arr[ 0 ] );
int partition(int lo, int hi) {
int j = lo - 1;
int pivot = arr[hi];
for(int i = lo; i <= hi; ++i) {
if(arr[i]<=pivot) {
j++;
int temp = arr[j];
arr[j]=arr[i];
arr[i] = temp;
}
}
return j;
}
void quicksort(int lo, int hi) {
int piv = partition(lo, hi);
if(lo<piv-1) quicksort(lo, piv-1);
if(piv+1<hi) quicksort(piv+1,hi);
}
int select(int kmin, int lo, int hi) {
int pivot = partition(lo, hi);
if(pivot == kmin-1) {
return arr[pivot];
} else if(kmin-1 > pivot)
return select(kmin,pivot+1,hi);
return select(kmin, lo, pivot-1);
}
int main(int argc, char const *argv[]) {
for(int i = 0; i < n; ++i) cout<<arr[i]<<" ";
cout<<endl;
quicksort(0, n-1);
for(int i = 0; i < n; ++i) cout<<arr[i]<<" ";
cout<<"\n"<<select(5, 0, n - 1);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGFycltdID0gezE0LDQsNSwyMSwxLDMzLDIzfSwKCiAgICBuID0gc2l6ZW9mKCBhcnIgKS9zaXplb2YoIGFyclsgMCBdICk7CgppbnQgcGFydGl0aW9uKGludCBsbywgaW50IGhpKSB7CgogICAgaW50IGogPSBsbyAtIDE7CiAgICBpbnQgcGl2b3QgPSBhcnJbaGldOwogICAgZm9yKGludCBpID0gbG87IGkgPD0gaGk7ICsraSkgewogICAgICAgIGlmKGFycltpXTw9cGl2b3QpIHsKICAgICAgICAgIGorKzsKICAgICAgICAgIGludCB0ZW1wID0gYXJyW2pdOwogICAgICAgICAgYXJyW2pdPWFycltpXTsKICAgICAgICAgIGFycltpXSA9IHRlbXA7CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiBqOwp9Cgp2b2lkIHF1aWNrc29ydChpbnQgbG8sIGludCBoaSkgewogICAgIGludCBwaXYgPSBwYXJ0aXRpb24obG8sIGhpKTsKICAgICBpZihsbzxwaXYtMSkgcXVpY2tzb3J0KGxvLCBwaXYtMSk7CiAgICAgaWYocGl2KzE8aGkpIHF1aWNrc29ydChwaXYrMSxoaSk7Cn0KCmludCBzZWxlY3QoaW50IGttaW4sIGludCBsbywgaW50IGhpKSB7CgogICAgaW50IHBpdm90ID0gcGFydGl0aW9uKGxvLCBoaSk7CgogICAgaWYocGl2b3QgPT0ga21pbi0xKSB7CgogICAgICAgcmV0dXJuIGFycltwaXZvdF07CgogICAgfSBlbHNlIGlmKGttaW4tMSA+IHBpdm90KQoKICAgICAgcmV0dXJuIHNlbGVjdChrbWluLHBpdm90KzEsaGkpOwoKICAgICAgcmV0dXJuIHNlbGVjdChrbWluLCBsbywgcGl2b3QtMSk7CgoKfQoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbXSkgewoKICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgKytpKSBjb3V0PDxhcnJbaV08PCIgIjsKCiAgY291dDw8ZW5kbDsKCiAgcXVpY2tzb3J0KDAsIG4tMSk7CgogIGZvcihpbnQgaSA9IDA7IGkgPCBuOyArK2kpIGNvdXQ8PGFycltpXTw8IiAiOwoKICBjb3V0PDwiXG4iPDxzZWxlY3QoNSwgMCwgbiAtIDEpOwoKICByZXR1cm4gMDsKfQ==