//
// main.cpp
// Quicksort and Mergesort
//
// Created by Himanshu on 29/08/21.
//
#include <iostream>
#include <limits.h>
using namespace std;
const int n = 5;
void printArray (int arr[]) {
for (int i=0; i<n; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
}
int partition (int A[], int p, int r) {
int x = A[r];
int i = p-1;
for (int j=p; j<r; j++) {
if (A[j] <= x) {
i++;
swap(A[i], A[j]);
}
}
swap(A[r], A[i+1]);
return (i+1);
}
void quicksort (int A[], int p, int r) {
if (p < r) {
int q = partition(A, p, r);
cout<<"Pivot: "<<A[q]<<endl;
cout<<"New array after partition"<<endl;
printArray(A);
quicksort(A, p, q-1);
quicksort(A, q+1, r);
}
}
void merge (int A[], int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int L[n1 + 1], R[n2 + 1];
//Adding element from 0 to q, to L array
for (int i=0; i<n1; i++) {
L[i] = A[p+i];
}
//Adding element from q+1 to r, to R array
for (int j=0; j<n2; j++) {
R[j] = A[q+j+1];
}
L[n1] = INT_MAX;
R[n2] = INT_MAX;
int i=0, j=0;
for (int k=p; k<=r; k++) {
if (L[i] <= R[j]) {
A[k] = L[i];
i = i+1;
} else {
A[k] = R[j];
j = j+1;
}
}
}
void mergesort (int A[], int p, int r) {
if (p < r) {
int q = (p+r)/2;
mergesort(A, p, q);
mergesort(A, q+1, r);
merge(A, p, q, r);
cout<<"New array after merge"<<endl;
printArray(A);
}
}
int main() {
int A[n] = {11, 2, 90, 57, 13};
int B[n] = {11, 2, 90, 57, 13};
cout<<"Initial Array:"<<endl<<endl;
printArray(A);
cout<<endl;
cout<<"-------Quicksort-------"<<endl<<endl;
quicksort(A, 0, n-1);
cout<<"-----------------------"<<endl;
cout<<"Initial Array:"<<endl<<endl;
printArray(B);
cout<<endl;
cout<<"-------Mergesort-------"<<endl<<endl;
mergesort(B, 0, n-1);
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBRdWlja3NvcnQgYW5kIE1lcmdlc29ydAovLwovLyAgQ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gUltuMiArIDFdOwogICAgCiAgICAvL0FkZGluZyBlbGVtZW50IGZyb20gMCB0byBxLCB0byBMIGFycmF5CiAgICBmb3IgKGludCBpPTA7IGk8bjE7IGkrKykgewogICAgICAgIExbaV0gPSBBW3AraV07CiAgICB9CiAgICAKICAgIC8vQWRkaW5nIGVsZW1lbnQgZnJvbSBxKzEgdG8gciwgdG8gUiBhcnJheQogICAgZm9yIChpbnQgaj0wOyBqPG4yOyBqKyspIHsKICAgICAgICBSW2pdID0gQVtxK2orMV07CiAgICB9CiAgICAKICAgIExbbjFdID0gSU5UX01BWDsKICAgIFJbbjJdID0gSU5UX01BWDsKICAgIAogICAgaW50IGk9MCwgaj0wOwogICAgCiAgICBmb3IgKGludCBrPXA7IGs8PXI7IGsrKykgewogICAgICAgIGlmIChMW2ldIDw9IFJbal0pIHsKICAgICAgICAgICAgQVtrXSA9IExbaV07CiAgICAgICAgICAgIGkgPSBpKzE7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgQVtrXSA9IFJbal07CiAgICAgICAgICAgIGogPSBqKzE7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIG1lcmdlc29ydCAoaW50IEFbXSwgaW50IHAsIGludCByKSB7CiAgICBpZiAocCA8IHIpIHsKICAgICAgICBpbnQgcSAgPSAocCtyKS8yOwogICAgICAgIG1lcmdlc29ydChBLCBwLCBxKTsKICAgICAgICBtZXJnZXNvcnQoQSwgcSsxLCByKTsKICAgICAgICBtZXJnZShBLCBwLCBxLCByKTsKICAgICAgICBjb3V0PDwiTmV3IGFycmF5IGFmdGVyIG1lcmdlIjw8ZW5kbDsKICAgICAgICBwcmludEFycmF5KEEpOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBBW25dID0gezExLCAyLCA5MCwgNTcsIDEzfTsKICAgIGludCBCW25dID0gezExLCAyLCA5MCwgNTcsIDEzfTsKICAgIAogICAgY291dDw8IkluaXRpYWwgQXJyYXk6Ijw8ZW5kbDw8ZW5kbDsKICAgIHByaW50QXJyYXkoQSk7CiAgICBjb3V0PDxlbmRsOwogICAgY291dDw8Ii0tLS0tLS1RdWlja3NvcnQtLS0tLS0tIjw8ZW5kbDw8ZW5kbDsKICAgIHF1aWNrc29ydChBLCAwLCBuLTEpOwogICAgY291dDw8Ii0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIjw8ZW5kbDsKICAgIGNvdXQ8PCJJbml0aWFsIEFycmF5OiI8PGVuZGw8PGVuZGw7CiAgICBwcmludEFycmF5KEIpOwogICAgY291dDw8ZW5kbDsKICAgIGNvdXQ8PCItLS0tLS0tTWVyZ2Vzb3J0LS0tLS0tLSI8PGVuZGw8PGVuZGw7CiAgICBtZXJnZXNvcnQoQiwgMCwgbi0xKTsKICAgIHJldHVybiAwOwp9Cgo=