This has been asked a few times here, but I was wondering what I could do to improve the efficiency and readability of my code. My programming skill has gotten rusty, so I would like to elicit all constructive criticism that can make me write code better. init:#include <iostream>using namespace std;merge()//The merge functionvoid merge(int a[], int startIndex, int endIndex){int size = (endIndex - startIndex) + 1;int *b = new int [size]();int i = startIndex;int mid = (startIndex + endIndex)/2;int k = 0;int j = mid + 1;while (k < size){       if((i<=mid) && (a[i] < a[j]))    {        b[k++] = a[i++];    }    else    {        b[k++] = a[j++];    }}for(k=0; k < size; k++){    a[startIndex+k] = b[k];}delete []b;}merge_sort()//The recursive merge sort functionvoid merge_sort(int iArray[], int startIndex, int endIndex){int midIndex;//Check for base caseif (startIndex >= endIndex){    return;}   //First, divide in halfmidIndex = (startIndex + endIndex)/2;//First recursive call merge_sort(iArray, startIndex, midIndex);//Second recursive call merge_sort(iArray, midIndex+1, endIndex);//The merge functionmerge(iArray, startIndex, endIndex);}main()//The main functionint main(int argc, char *argv[]){int iArray[10] = {2,5,6,4,7,2,8,3,9,10};merge_sort(iArray, 0, 9);//Print the sorted arrayfor(int i=0; i < 10; i++){    cout << iArray[i] << endl;}return 0;    }
Implementing the merge sort with only arrays, out of place
c++;sorting;mergesort
Everything Jamal said (so I will not repeat)Plus:The normal idium in C++ for passing ranges is [beg, end).For those not mathematically inclined end is specified one past the end.If you follow the C++ convention you make it easier for other C++ devs to keep track without having to think to hard about what you are up to. Personally I think it also makes writting merge sort easier. merge_sort(iArray, 0, 10);Then in merge() midIndex = (startIndex + endIndex)/2;     // The start of the second half                                           // Is one past the end of the first half. merge_sort(iArray, startIndex, midIndex); merge_sort(iArray, midIndex,   endIndex);Slight optimization here://Check for base caseif (startIndex >= endIndex){    return;}You can return if the size is 1. A vector of 1 is already sorted. Since end is one past the end the size is end-start.if ((endIndex - startIndex) <= 1){    return;}Don't do manually memory management in your code.int *b = new int [size](); // Also initialization costs.                           // So why do that if you don't need to!// PS in C++ (unlike C)// It is more common to put '*' by the type.// That's becuase the type is `pointer to int`Better alternative:std::vector<int> b(size);  // Memory management handled for you.                           // Its exception safe (so you will never leak).                           // Overhead is insignificant.A conditional in the middle of a loop is expensive.while (k < size){    if((i<=mid) && (a[i] < a[j]))    {...} else {...}}Once one side is used up break out of the loop and just copy the other one.while (i <= mid && j <= endIndex){    b[k++] = (a[i] < a[j]) ? a[i++] : a[j++];}// Note: Only one of these two loop will execute.while(i <= mid){    b[k++] = a[i++];}while(j <= endIndex){    b[k++] = a[j++];}Rather than calculating the mid point both in merge() and in merge_sort, it may be worth just passing the value as a parameter.Learn to do the inplace merge so you don't have to copy the values back after the merge. midIndex = (startIndex + endIndex)/2; merge_sort(iArray, startIndex, midIndex); merge_sort(iArray, midIndex,   endIndex); merge(iArray, startIndex, midIndex, endIndex); //                        ^^^^^^^^   pass midIndex so you don't need to re-calc