// Merge Sort #include #include using namespace std; // Prototypes: void merge(int sorted[], int leftHalf[], int leftCount, int rightHalf[], int rightCount); void mergeSort(int myArray[], int elementCount); int main() { int num,i; int * a; cout<<"**********************************************************"<>num; cout< 1) { int half = elementCount/2; int * leftHalf = new int[half]; int * rightHalf = new int[elementCount - half]; for (int i = 0; i < half; i++) { leftHalf[i] = myArray[i]; } // end for int right = 0; for (int i = half; i < elementCount; i++) { rightHalf[right] = myArray[i]; right++; } // end for mergeSort(leftHalf, half); mergeSort(rightHalf, elementCount - half); merge(myArray, leftHalf, half, rightHalf, elementCount - half); // free up the memory allocated for the array created above delete [] leftHalf; delete [] rightHalf; } // end if } // end mergeSort void merge(int sorted[], int leftHalf[], int leftCount, int rightHalf[], int rightCount) { int left=0, right=0, nextSorted=0; while (left < leftCount && right < rightCount) { if (leftHalf[left] <= rightHalf[right]) { sorted[nextSorted] = leftHalf[left]; left++; } else { sorted[nextSorted] = rightHalf[right]; right++; } // end if nextSorted++; } // end while while (left < leftCount) { sorted[nextSorted] = leftHalf[left]; left++; nextSorted++; } // end while while (right < rightCount) { sorted[nextSorted] = rightHalf[right]; right++; nextSorted++; } // end while } // end merge