Code:
/********************************************************************************
FUNCTION NAME: mergeSort
INPUTS: roomsize (double array)
roomprofit (double array)
firstIndex (int) - first value for unsorted array index
lastIndex (int) - last value for unsorted array index
array (int) - specificies which array needs to be sorted
column (int) - specifices which column needs to be sorted
sortType (int) specifices which sort is needed
OUTPUTS: Recursive call to mergeSort allows for the array to be divided in half
with each call.
Calls mergeLtoS if items need to be sorted from greatest to smallest
Calls mergeStoL if items need to be sorted from smallest to largest
RETURN: None
PURPOSE: Breaks up an unsorted array in preperation for sorting merge
**********************************************************************************/
int mergeSort (double roomprofit[25][5], int firstIndex, int lastIndex, int sortType, int array, int column)
{
if ( firstIndex < lastIndex)
{
mergeSort (roomprofit, firstIndex, (firstIndex+lastIndex)/2, sortType, array, column);
mergeSort (roomprofit, (firstIndex+lastIndex)/2 +1, lastIndex, sortType, array, column);
switch (sortType)
{
case 1: mergeLtoS(roomprofit, firstIndex, (firstIndex+lastIndex)/2, (firstIndex+lastIndex)/2 + 1, lastIndex, array, column);
break;
case 2: mergeStoL(roomprofit, firstIndex, (firstIndex+lastIndex)/2, (firstIndex+lastIndex)/2 + 1, lastIndex, array, column);
break;
}
}
}
/********************************************************************************
FUNCTION NAME: mergeLtoS
INPUTS: roomsize (double array)
roomprofit (double array)
firstIndex1 (int)
lastIndex1 (int)
firstIndex2 (int)
lastIndex2 (int)
array (int) - specifies which array needs to be sorted
column (int) - specifices which column needs to be sorted
OUTPUTS: calls mergeMove to merge compared array sections together
RETURN: None
PURPOSE: Compares divided array sections in prep for merge to greatest to smallest
**********************************************************************************/
void mergeLtoS(double roomprofit[25][5], int firstIndex1, int lastIndex1, int firstIndex2, int lastIndex2, int array, int column)
{
double tempArray[25][5] = {{0}};
int index, index1, index2;
int num;
index = 0;
index1 = firstIndex1;
index2 = firstIndex2;
num = lastIndex1 - firstIndex1 + lastIndex2 - firstIndex2 + 2;
if (array == 3)
{
while ( (index1 <= lastIndex1) && (index2 <= lastIndex2) )
{
if (roomprofit[index1][column] > roomprofit[index2][column])
{
tempArray[index++][0] = roomprofit[index1++][0];
tempArray[index++][1] = roomprofit[index1++][1];
tempArray[index++][2] = roomprofit[index1++][2];
tempArray[index++][3] = roomprofit[index1++][3];
tempArray[index++][4] = roomprofit[index1++][4];
}
else
{
roomprofit[index1++][0] = tempArray[index++][0];
roomprofit[index1++][1] = tempArray[index++][1];
roomprofit[index1++][2] = tempArray[index++][2];
roomprofit[index1++][3] = tempArray[index++][3];
roomprofit[index1++][4] = tempArray[index++][4];
}
}
if (index1 > lastIndex1)
{
mergeMove(roomprofit, index2, lastIndex2, tempArray, index);
}
else
{
mergeMove(roomprofit, index1, lastIndex1, tempArray, index);
}
mergeMove (tempArray, 0, num-1, roomprofit, firstIndex1);
}
}
/********************************************************************************
FUNCTION NAME: mergeMove
INPUTS: arrayList1 (double array)
arrayList2 (double array)
firstIndex1 (int)
firstIndex2 (int)
lastIndex1 (int)
OUTPUTS: RETURN: None
PURPOSE: Combines sections two lists of a sorted array.
**********************************************************************************/
void mergeMove(double arrayList1[25][5], int firstIndex1, int lastIndex1, double arrayList2[25][5], int firstIndex2)
{
while (firstIndex1 <= lastIndex1)
{
arrayList2[firstIndex2++][0] = arrayList1[lastIndex1++][0];
arrayList2[firstIndex2++][1] = arrayList1[lastIndex1++][1];
arrayList2[firstIndex2++][2] = arrayList1[lastIndex1++][2];
arrayList2[firstIndex2++][3] = arrayList1[lastIndex1++][3];
arrayList2[firstIndex2++][4] = arrayList1[lastIndex1++][4];
}
}
/**** the function call and assignments for the merge sort from main ****/
sortType = 1;
array = 3;
column = GROSS;
roomprofitPrint(roomprofit, salesroom, roomname);
mergeSort (roomprofit, 0, 25, sortType, array, column);
roomprofitPrint(roomprofit, salesroom, roomname);