#include<iostream>
using namespace std;
void merge(int *arr, int s, int e) {
int mid = (s+e)/2;
int len1 = mid - s + 1;
int len2 = e - mid;
int *first = new int[len1];
int *second = new int[len2];
//copy values
int mainArrayIndex = s;
for(int i=0; i<len1; i++) {
first[i] = arr[mainArrayIndex++];
}
mainArrayIndex = mid+1;
for(int i=0; i<len2; i++) {
second[i] = arr[mainArrayIndex++];
}
//merge 2 sorted arrays
int index1 = 0;
int index2 = 0;
mainArrayIndex = s;
while(index1 < len1 && index2 < len2) {
if(first[index1] < second[index2]) {
arr[mainArrayIndex++] = first[index1++];
}
else{
arr[mainArrayIndex++] = second[index2++];
}
}
while(index1 < len1) {
arr[mainArrayIndex++] = first[index1++];
}
while(index2 < len2 ) {
arr[mainArrayIndex++] = second[index2++];
}
delete []first;
delete []second;
}
void mergeSort(int *arr, int s, int e) {
//base case
if(s >= e) {
return;
}
int mid = (s+e)/2;
//left part sort karna h
mergeSort(arr, s, mid);
//right part sort karna h
mergeSort(arr, mid+1, e);
//merge
merge(arr, s, e);
}
int main() {
int arr[15] = {3,7,0,1,5,8,3,2,34,66,87,23,12,12,12};
int n = 15;
mergeSort(arr, 0, n-1);
for(int i=0;i<n;i++){
cout << arr[i] << " ";
} cout << endl;
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIG1lcmdlKGludCAqYXJyLCBpbnQgcywgaW50IGUpIHsKCiAgICBpbnQgbWlkID0gKHMrZSkvMjsKCiAgICBpbnQgbGVuMSA9IG1pZCAtIHMgKyAxOwogICAgaW50IGxlbjIgPSBlIC0gbWlkOwoKICAgIGludCAqZmlyc3QgPSBuZXcgaW50W2xlbjFdOwogICAgaW50ICpzZWNvbmQgPSBuZXcgaW50W2xlbjJdOwoKICAgIC8vY29weSB2YWx1ZXMKICAgIGludCBtYWluQXJyYXlJbmRleCA9IHM7CiAgICBmb3IoaW50IGk9MDsgaTxsZW4xOyBpKyspIHsKICAgICAgICBmaXJzdFtpXSA9IGFyclttYWluQXJyYXlJbmRleCsrXTsKICAgIH0KCiAgICBtYWluQXJyYXlJbmRleCA9IG1pZCsxOwogICAgZm9yKGludCBpPTA7IGk8bGVuMjsgaSsrKSB7CiAgICAgICAgc2Vjb25kW2ldID0gYXJyW21haW5BcnJheUluZGV4KytdOwogICAgfQoKICAgIC8vbWVyZ2UgMiBzb3J0ZWQgYXJyYXlzICAgICAKICAgIGludCBpbmRleDEgPSAwOwogICAgaW50IGluZGV4MiA9IDA7CiAgICBtYWluQXJyYXlJbmRleCA9IHM7CgogICAgd2hpbGUoaW5kZXgxIDwgbGVuMSAmJiBpbmRleDIgPCBsZW4yKSB7CiAgICAgICAgaWYoZmlyc3RbaW5kZXgxXSA8IHNlY29uZFtpbmRleDJdKSB7CiAgICAgICAgICAgIGFyclttYWluQXJyYXlJbmRleCsrXSA9IGZpcnN0W2luZGV4MSsrXTsKICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgYXJyW21haW5BcnJheUluZGV4KytdID0gc2Vjb25kW2luZGV4MisrXTsKICAgICAgICB9CiAgICB9ICAgCgogICAgd2hpbGUoaW5kZXgxIDwgbGVuMSkgewogICAgICAgIGFyclttYWluQXJyYXlJbmRleCsrXSA9IGZpcnN0W2luZGV4MSsrXTsKICAgIH0KCiAgICB3aGlsZShpbmRleDIgPCBsZW4yICkgewogICAgICAgIGFyclttYWluQXJyYXlJbmRleCsrXSA9IHNlY29uZFtpbmRleDIrK107CiAgICB9CgogICAgZGVsZXRlIFtdZmlyc3Q7CiAgICBkZWxldGUgW11zZWNvbmQ7Cgp9Cgp2b2lkIG1lcmdlU29ydChpbnQgKmFyciwgaW50IHMsIGludCBlKSB7CgogICAgLy9iYXNlIGNhc2UKICAgIGlmKHMgPj0gZSkgewogICAgICAgIHJldHVybjsKICAgIH0KICAgIAogICAgaW50IG1pZCA9IChzK2UpLzI7CgogICAgLy9sZWZ0IHBhcnQgc29ydCBrYXJuYSBoIAogICAgbWVyZ2VTb3J0KGFyciwgcywgbWlkKTsKICAgIAogICAgLy9yaWdodCBwYXJ0IHNvcnQga2FybmEgaCAKICAgIG1lcmdlU29ydChhcnIsIG1pZCsxLCBlKTsKCiAgICAvL21lcmdlCiAgICBtZXJnZShhcnIsIHMsIGUpOwoKfQoKaW50IG1haW4oKSB7CgogICAgaW50IGFyclsxNV0gPSB7Myw3LDAsMSw1LDgsMywyLDM0LDY2LDg3LDIzLDEyLDEyLDEyfTsKICAgIGludCBuID0gMTU7CgogICAgbWVyZ2VTb3J0KGFyciwgMCwgbi0xKTsKCiAgICBmb3IoaW50IGk9MDtpPG47aSsrKXsKICAgICAgICBjb3V0IDw8IGFycltpXSA8PCAiICI7CiAgICB9IGNvdXQgPDwgZW5kbDsKCiAgICByZXR1cm4gMDsKfQ==