#include <iostream>
using namespace std;
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int partition(int arr[], int low, int high, int step) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
cout << "Buoc " << step << ": ";
printArray(arr, high + 1);
return i + 1;
}
void quickSort(int arr[], int low, int high, int& step) {
if (low < high) {
int pi = partition(arr, low, high, step);
step++;
quickSort(arr, low, pi - 1, step);
quickSort(arr, pi + 1, high, step);
}
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int step = 0;
quickSort(arr, 0, n - 1, step);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBzd2FwKGludCYgYSwgaW50JiBiKSB7CiAgICBpbnQgdGVtcCA9IGE7CiAgICBhID0gYjsKICAgIGIgPSB0ZW1wOwp9Cgp2b2lkIHByaW50QXJyYXkoaW50IGFycltdLCBpbnQgc2l6ZSkgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICBjb3V0IDw8IGFycltpXSA8PCAiICI7CiAgICB9CiAgICBjb3V0IDw8IGVuZGw7Cn0KCmludCBwYXJ0aXRpb24oaW50IGFycltdLCBpbnQgbG93LCBpbnQgaGlnaCwgaW50IHN0ZXApIHsKICAgIGludCBwaXZvdCA9IGFycltoaWdoXTsKICAgIGludCBpID0gbG93IC0gMTsKICAgIAogICAgZm9yIChpbnQgaiA9IGxvdzsgaiA8IGhpZ2g7IGorKykgewogICAgICAgIGlmIChhcnJbal0gPCBwaXZvdCkgewogICAgICAgICAgICBpKys7CiAgICAgICAgICAgIHN3YXAoYXJyW2ldLCBhcnJbal0pOwogICAgICAgIH0KICAgIH0KICAgIHN3YXAoYXJyW2kgKyAxXSwgYXJyW2hpZ2hdKTsKICAgIAogICAgY291dCA8PCAiQnVvYyAiIDw8IHN0ZXAgPDwgIjogIjsKICAgIHByaW50QXJyYXkoYXJyLCBoaWdoICsgMSk7CiAgICAKICAgIHJldHVybiBpICsgMTsKfQoKdm9pZCBxdWlja1NvcnQoaW50IGFycltdLCBpbnQgbG93LCBpbnQgaGlnaCwgaW50JiBzdGVwKSB7CiAgICBpZiAobG93IDwgaGlnaCkgewogICAgICAgIGludCBwaSA9IHBhcnRpdGlvbihhcnIsIGxvdywgaGlnaCwgc3RlcCk7CiAgICAgICAgc3RlcCsrOwogICAgICAgIHF1aWNrU29ydChhcnIsIGxvdywgcGkgLSAxLCBzdGVwKTsKICAgICAgICBxdWlja1NvcnQoYXJyLCBwaSArIDEsIGhpZ2gsIHN0ZXApOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICBpbnQgYXJyW25dOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBjaW4gPj4gYXJyW2ldOwogICAgfQogICAgCiAgICBpbnQgc3RlcCA9IDA7CiAgICBxdWlja1NvcnQoYXJyLCAwLCBuIC0gMSwgc3RlcCk7CiAgICAKICAgIGNvdXQgPDwgIlNvcnRlZCBhcnJheTogXG4iOwogICAgcHJpbnRBcnJheShhcnIsIG4pOwogICAgCiAgICByZXR1cm4gMDsKfQo=