// Quick Sort example
// For input array
// 2 8 7 1 3 5 6 4
//
#include <stdio.h>
#define IPSIZE 8
// To enable debug messages uncomment #define
// #define DEBUG 1
#ifdef DEBUG
# define D(x) x
#else
# define D(x)
#endif
void quicksort(int arr[], int p, int r);
int partition(int arr[], int p, int r);
void swap(int i, int j);
int arr[IPSIZE] = {2, 8, 7, 1, 3, 5, 6, 4};
int main()
{
int i = 0;
for (i = 0; i <= IPSIZE - 1; i++) {
}
quicksort(arr, 0, IPSIZE - 1);
for (i = 0; i <= IPSIZE - 1; i++) {
}
return 0;
}
void quicksort(int arr[], int p, int r)
{
if (p < r) {
int pivotIndx = partition(arr, p, r);
quicksort(arr, p, pivotIndx - 1);
quicksort(arr, pivotIndx + 1, r);
}
}
int partition(int arr[], int p, int r)
{
int pivot = arr[r];
int i = p - 1;
int j = 0;
// Debug messages
D
(printf("\n############\n")); D
(printf("p=%d, r=%d, pivot=%d\n", p
, r
, pivot
)); for (j = p; j <= r; j++) {
}
for (j = p; j <= r - 1; j++) {
if (arr[j] <= pivot) {
i++;
swap(i, j);
}
}
swap(i + 1, r);
D
(printf("Elements after partition\n")); for (j = p; j <= r; j++) {
}
return (i + 1);
}
void swap(int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Ly8gUXVpY2sgU29ydCBleGFtcGxlCi8vIEZvciBpbnB1dCBhcnJheQovLyAyIDggNyAxIDMgNSA2IDQKLy8KI2luY2x1ZGUgPHN0ZGlvLmg+CiNkZWZpbmUgSVBTSVpFIDgKCi8vIFRvIGVuYWJsZSBkZWJ1ZyBtZXNzYWdlcyB1bmNvbW1lbnQgI2RlZmluZQovLyAjZGVmaW5lIERFQlVHIDEKCiNpZmRlZiBERUJVRwojICBkZWZpbmUgRCh4KSB4CiNlbHNlCiMgIGRlZmluZSBEKHgpCiNlbmRpZgoKdm9pZCBxdWlja3NvcnQoaW50IGFycltdLCBpbnQgcCwgaW50IHIpOyAKaW50IHBhcnRpdGlvbihpbnQgYXJyW10sIGludCBwLCBpbnQgcik7IAp2b2lkIHN3YXAoaW50IGksIGludCBqKTsgCgppbnQgYXJyW0lQU0laRV0gPSB7MiwgOCwgNywgMSwgMywgNSwgNiwgNH07IAoKaW50IG1haW4oKQp7CglpbnQgaSA9IDA7CiAgICBwcmludGYoIklucHV0IGFycmF5XG4iKTsKICAgIGZvciAoaSA9IDA7IGkgPD0gSVBTSVpFIC0gMTsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCIlZCAiLCBhcnJbaV0pOwogICAgfSAgIAogICAgcHJpbnRmKCJcblxuIik7CgogICAgcXVpY2tzb3J0KGFyciwgMCwgSVBTSVpFIC0gMSk7IAoKICAgIHByaW50ZigiU29ydGVkIG91dHB1dFxuIik7CiAgICBmb3IgKGkgPSAwOyBpIDw9IElQU0laRSAtIDE7IGkrKykgewogICAgICAgIHByaW50ZigiJWQgIiwgYXJyW2ldKTsKICAgIH0gICAKICAgIHByaW50ZigiXG4iKTsKCiAgICByZXR1cm4gMDsKfQoKdm9pZCBxdWlja3NvcnQoaW50IGFycltdLCBpbnQgcCwgaW50IHIpCnsKICAgIGlmIChwIDwgcikgewogICAgICAgIGludCBwaXZvdEluZHggPSBwYXJ0aXRpb24oYXJyLCBwLCByKTsKCiAgICAgICAgcXVpY2tzb3J0KGFyciwgcCwgcGl2b3RJbmR4IC0gMSk7CiAgICAgICAgcXVpY2tzb3J0KGFyciwgcGl2b3RJbmR4ICsgMSwgcik7CiAgICB9Cn0KCmludCBwYXJ0aXRpb24oaW50IGFycltdLCBpbnQgcCwgaW50IHIpCnsKICAgIGludCBwaXZvdCA9IGFycltyXTsKICAgIGludCBpID0gcCAtIDE7CiAgICBpbnQgaiA9IDA7CgogICAgLy8gRGVidWcgbWVzc2FnZXMKICAgIEQocHJpbnRmKCJcbiMjIyMjIyMjIyMjI1xuIikpOwogICAgRChwcmludGYoIlBhcnRpdGlvblxuIikpOwogICAgRChwcmludGYoInA9JWQsIHI9JWQsIHBpdm90PSVkXG4iLCBwLCByLCBwaXZvdCkpOwogICAgRChwcmludGYoIkVsZW1lbnRzXG4iKSk7CiAgICBmb3IgKGogPSBwOyBqIDw9IHI7IGorKykgewogICAgICAgIEQocHJpbnRmKCIlZCAiLCBhcnJbal0pKTsKICAgIH0KICAgIEQocHJpbnRmKCJcbiIpKTsKCiAgICBmb3IgKGogPSBwOyBqIDw9IHIgLSAxOyBqKyspIHsKICAgICAgICBpZiAoYXJyW2pdIDw9IHBpdm90KSB7CiAgICAgICAgICAgIGkrKzsKICAgICAgICAgICAgc3dhcChpLCBqKTsKICAgICAgICB9CiAgICB9CgogICAgc3dhcChpICsgMSwgcik7CgogICAgRChwcmludGYoIkVsZW1lbnRzIGFmdGVyIHBhcnRpdGlvblxuIikpOwogICAgZm9yIChqID0gcDsgaiA8PSByOyBqKyspIHsKICAgICAgICBEKHByaW50ZigiJWQgIiwgYXJyW2pdKSk7CiAgICB9CiAgICBEKHByaW50ZigiXG4iKSk7CgogICAgcmV0dXJuIChpICsgMSk7Cn0KCnZvaWQgc3dhcChpbnQgaSwgaW50IGopCnsKICAgIGludCB0ZW1wID0gYXJyW2ldOwogICAgYXJyW2ldID0gYXJyW2pdOwogICAgYXJyW2pdID0gdGVtcDsKfQo=