/**
********************************
* Merge sort. Results are stored in the member variable data.
********************************
*/
public void mergeSort(){
//Step 1. Allocate space.
int tempRow;
int tempGroups;
int tempActualRow;
int tempNextRow = 0;
int tempGroupNumber;
int tempFirstStart, tempSecondStart, tempSecondEnd;
int tempFirstIndex, tempSecondIndex;
int tempNumCopied;
for (int i = 0; i < length; i++) {
System.out.println(data[i]);
}//of for i
System.out.println();
DataNode[][] tempMatrix = new DataNode[2][length];
// Step 2.Copy data.
for (int i = 0; i < length; i++) {
tempMatrix[0][i] = data[i];
}//of for i
//Step 3.Merge. log n rounds
tempRow = -1;
for (int tempSize = 1; tempSize <= length ; tempSize *= 2) {
//Reuse the space of the two rows.
tempRow++;
System.out.println("Current row = " + tempRow);
tempActualRow = tempRow % 2;
tempNextRow = (tempRow + 1) % 2;
tempGroups = length / (tempSize * 2);
if (length % (tempSize * 2) != 0){
tempGroups++;
}//of if
System.out.println("tempSize = " + tempSize + ", numGroups = " + tempGroups);
for (tempGroupNumber = 0; tempGroupNumber < tempGroups ; tempGroupNumber++) {
tempFirstStart = tempGroupNumber * tempSize * 2;
tempSecondStart = tempGroupNumber * tempSize * 2 + tempSize;
if (tempSecondStart > length - 1) {
//Copy the first part.
for (int i = tempFirstStart; i < length; i++) {
tempMatrix[tempNextRow][i] = tempMatrix[tempActualRow][i];
}//of for i
continue;
}//of if
tempSecondEnd = tempGroupNumber * tempSize * 2 + tempSize * 2 - 1;
if (tempSecondEnd > length - 1) {
tempSecondEnd = length - 1;
}//of if
System.out.println("Trying to merge [" + tempFirstStart + ", " + (tempSecondStart - 1)
+ "] with [" + ", " + tempSecondStart + ", " + tempSecondEnd + "]");
tempFirstIndex = tempFirstStart;
tempSecondIndex = tempSecondStart;
tempNumCopied = 0;
while ((tempFirstIndex <= tempSecondStart - 1) && (tempSecondIndex <= tempSecondEnd)){
if (tempMatrix[tempActualRow][tempFirstIndex].key <= tempMatrix[tempActualRow][tempSecondIndex].key) {
tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];
tempFirstIndex++;
System.out.println("Copying " + tempMatrix[tempActualRow][tempFirstIndex]);
}else{
tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];
System.out.println("Copying " + tempMatrix[tempActualRow][tempSecondIndex]);
tempSecondIndex++;
}//of if
tempNumCopied++;
}//of while
while(tempFirstIndex <= tempSecondStart - 1){
tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];
tempFirstIndex++;
tempNumCopied++;
}//of while
while(tempSecondIndex <= tempSecondEnd){
tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];
tempSecondIndex++;
tempNumCopied++;
}//of while
}//of for tempGroupNumber
System.out.println("Round " + tempRow);
for (int i = 0; i < length; i++) {
System.out.println(tempMatrix[tempNextRow][i] + " ");
}//of for j
System.out.println();
}//of for tempSize
data = tempMatrix[tempNextRow];
}//of mergeSort
public static void mergeSortTest(){
int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9};
String[] tempContents = {"if", "then", "else", "switch", "case", "for", "while"};
DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);
System.out.println(tempDataArray);
tempDataArray.mergeSort();
System.out.println("Result\r\n" + tempDataArray);
}//of mergeSortTest
day49
最新推荐文章于 2023-12-20 20:40:09 发布