[英]Java - Mergesort counting
I have made a mergesort that is being juxtaposed with two already created selection and insertion sorts which both count the comparisons so that when executed the program illustrates which methods are faster. 我做了一个合并排序,将它与两个已经创建的选择排序和插入排序并置在一起,它们都对比较进行计数,以便在执行程序时可以说明哪种方法更快。
I'll post full code so far so you can see selection and insertion, how they were used. 到目前为止,我将发布完整的代码,以便您可以看到选择和插入以及它们的用法。
Arraysort.java Arraysort.java
public class ArraySort {
private long[] a; // ref to array a
private int nElems; // number of data items
public ArraySort(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
public void Clone(ArraySort c) // c is another array
{
c.nElems = this.nElems; // Copy nElems
System.arraycopy(this.a, 0, c.a, 0, this.nElems); // Copy elements
}
public void insert(long value) // put element into array
{
a[nElems++] = value; // insert value
}
public String toString() // displays array contents
{
String res="";
for(int j=0; j<nElems; j++) // for each element,
res = res + a[j] + " "; // append it to res
return res;
}
private int insertOrder(int n, long temp) { // insert temp into a[0..(n-1)]
// and keep a[0..n] sorted.
int count = 0;
while (n>0) {
count++; // count next comparison
if (a[n-1] > temp) { // until one is smaller,
a[n] = a[n-1]; // shift item to right
--n; // go left one position
} else break;
}
a[n] = temp; // insert marked item
return count;
}
public int insertionSort() {
int count = 0;
for (int n=1; n<nElems; n++)
count += insertOrder(n, a[n]); // insert a[n] into a[0..(n-1)]
return count;
} // end insertionSort()
private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
public int selectionSort() {
int out, in, max, count=0;
for(out=nElems-1; out > 0; out--) { // outer loop
max = out; // max is maximum item's index
for(in=0; in<out; in++) { // inner loop
if(a[in] > a[max] ) // if max is smaller,
max = in; // we have a new max
count++; // count one comparison
}
swap(out, max); // swap them
} // end for(out)
return count;
} // end selectionSort()
public void mergeSort() {
long[] ws = new long[nElems];
recMergeSort(ws, 0, nElems-1);
}
public void recMergeSort(long[] ws, int lower, int upper) {
if (lower == upper)
return;
else {
int mid = (lower + upper) / 2; //find midpoint
recMergeSort(ws, lower, mid); //sort lower
recMergeSort(ws, mid+1, upper); //sort upper
merge(ws, lower, mid+1, upper); //merge
}
}
public void merge(long[] ws, int lowPtr, int highPtr, int upper) {
int j = 0;
int lower = lowPtr;
int mid = highPtr-1;
int n = upper-lower+1; //# of items
while(lowPtr <= mid && highPtr <= upper)
if( a[lowPtr] < a[highPtr] )
ws[j++] = (int) a[lowPtr++];
else
ws[j++] = (int) a[highPtr++];
while(lowPtr <= mid)
ws[j++] = (int) a[lowPtr++];
while(highPtr <= upper)
ws[j++] = (int) a[highPtr++];
for(j=0; j<n; j++)
a[lower+j] = ws[j];
}
public void display() {
for(int j=0; j<nElems; j++) {
System.out.print(a[j] + " "); }
System.out.println("");
}
//end
}
SortComparison.java SortComparison.java
import java.util.*;
public class SortComparison {
public static void main(String[] args) {
int count, maxSize = 100; // array size
ArraySort arr, carr; // reference to array
arr = new ArraySort(maxSize); // create the arrays
carr = new ArraySort(maxSize);
// insert some random numbers
Random generator = new Random();
for (int i = 0; i < 20; i++) {
arr.insert(Math.abs(generator.nextInt())%maxSize);
}
System.out.println("Before sort: " + arr); // display items
arr.Clone(carr);
count = carr.insertionSort(); // insertion-sort a clone of arr
System.out.println("\nInsert sort: \n" + carr + " ### Comparisons: " + count);
arr.Clone(carr);
count = carr.selectionSort(); // selection-sort a clone of arr
System.out.println("\nSelect sort: \n" + carr + " ### Comparisons: " + count);
carr.mergeSort();
System.out.println("\nMerge sort: ");
carr.display();
}
}
You can see how things should be called, with the count returning, by selection and insertion.. 您可以通过选择和插入来查看应如何调用事物,并返回计数。
There are several ways you could return the count in addition to the sorted array. 除了排序数组之外,还有几种方法可以返回计数。 You could store either the count or the array as class variables.
您可以将count或数组存储为类变量。 You could create an object which encapsulates both the count and the array.
您可以创建一个封装计数和数组的对象。 You could even tack the count onto the front of the array, if you promise to remember that the first element is the count, not a part of the sort (this is probably a bad idea).
如果您保证要记住第一个元素是计数,而不是排序的一部分,那么您甚至可以将计数添加到数组的前面(这可能是个坏主意)。
You might want to check that your sort works correctly by copying the array again before you worry about counting the number of comparisons. 您可能需要在担心计算比较次数之前通过再次复制数组来检查排序是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.