The mergeSort.java Program

// mergeSort.java
// demonstrates recursive mergesort
// to run this program: C>java MergeSortApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class DArray

...{
private double[] theArray; // ref to array theArray
private int nElems; // number of data items
//----------------------------------------------------------
public DArray(int max) // constructor

...{
theArray = new double[max]; // create array
nElems = 0;
}
//----------------------------------------------------------
public void insert(double value) // put element into array

...{
theArray[nElems] = value; // insert it
nElems++; // increment size
}
//----------------------------------------------------------
public void display() // displays array contents

...{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(theArray[j] + " "); // display it
System.out.println("");
}
//----------------------------------------------------------
public void mergeSort() // called by main()

...{ // provides workspace
double[] workSpace = new double[nElems];
recMergeSort(workSpace, 0, nElems-1);
}
//----------------------------------------------------------
private void recMergeSort(double[] workSpace, int
lowerBound,
int upperBound)

...{
if(lowerBound == upperBound) // if range is 1,
return; // no use sorting
else

...{ // find midpoint
int mid = (lowerBound+upperBound) / 2;
// sort low half
recMergeSort(workSpace, lowerBound, mid);
// sort high half
recMergeSort(workSpace, mid+1, upperBound);
// merge them
merge(workSpace, lowerBound, mid+1, upperBound);
} // end else
} // end recMergeSort
//----------------------------------------------------------
private void merge(double[] workSpace, int lowPtr,
int highPtr, int upperBound)

...{
int j = 0; // workspace index
int lowerBound = lowPtr;
int mid = highPtr-1;
int n = upperBound-lowerBound+1; // # of items
while(lowPtr <= mid && highPtr <= upperBound)
if( theArray[lowPtr] < theArray[highPtr] )
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
while(lowPtr <= mid)
workSpace[j++] = theArray[lowPtr++];
while(highPtr <= upperBound)
workSpace[j++] = theArray[highPtr++];
for(j=0; j<n; j++)
theArray[lowerBound+j] = workSpace[j];
} // end merge()
//----------------------------------------------------------
} // end class DArray
////////////////////////////////////////////////////////////////
class MergeSortApp

...{
public static void main(String[] args)

...{
int maxSize = 100; // array size
DArray arr; // reference to array
arr = new DArray(maxSize); // create the array
arr.insert(64); // insert items
arr.insert(21);
arr.insert(33);
arr.insert(70);
arr.insert(12);
arr.insert(85);
arr.insert(44);
arr.insert(3);
arr.insert(99);
arr.insert(0);
arr.insert(108);
arr.insert(36);
arr.display(); // display items
arr.mergeSort(); // mergesort the array
arr.display(); // display items again
} // end main()
} // end class MergeSortApp

本文介绍了一种使用递归实现的归并排序算法,并通过Java程序进行了详细演示。该程序首先创建了一个双精度浮点数数组,用于存放待排序的数据元素,然后实现了递归归并排序的过程,包括寻找中点、对左右子数组进行排序,最后将它们合并为有序数组。
1763

被折叠的 条评论
为什么被折叠?



