通过生成随机数来测试mergesort算法

时间:2014-07-17 21:15:00

标签: java sorting testing mergesort

对于我们的实验室,我们的教授希望我们使用以下代码测试合并/气泡/插入/快速排序算法:

//fill an existing array with random doubles
//the value n will be with number of values to create
for (int i = 0; i < n; ++i) {
   ArrayToBeSorted[i] = Math.random();
}

//measuring the sorting time
long time1 = System.nanoTime();
// here is the place where you place the code
// or the call to code to be timed.
long elapsed = System.nanoTime() - time1;

这是我在互联网上找到的合并排序的算法。我很困惑如何使用教授为我们提供的代码。

package lab06;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

public class MergeSort {


private static final double[] ArrayToBeSorted = null;





public static int[] mergeSort(int [] list) {
    if (list.length <= 1) {
        return list;
    }

    // Split the array in half
    int[] first = new int[list.length / 2];
    int[] second = new int[list.length - first.length];
    System.arraycopy(list, 0, first, 0, first.length);
    System.arraycopy(list, first.length, second, 0, second.length);

    // Sort each half
    mergeSort(first);
    mergeSort(second);

    // Merge the halves together, overwriting the original array
    merge(first, second, list);
    return list;
}

private static void merge(int[] first, int[] second, int [] result) {
    // Merge both halves into the result array
    // Next element to consider in the first array
    int iFirst = 0;
    // Next element to consider in the second array
    int iSecond = 0;

    // Next open position in the result
    int j = 0;
    // As long as neither iFirst nor iSecond is past the end, move the
    // smaller element into the result.
    while (iFirst < first.length && iSecond < second.length) {
        if (first[iFirst] < second[iSecond]) {
            result[j] = first[iFirst];
            iFirst++;
        } else {
            result[j] = second[iSecond];
            iSecond++;
        }
        j++;
    }
    // copy what's left
    System.arraycopy(first, iFirst, result, j, first.length - iFirst);
    System.arraycopy(second, iSecond, result, j, second.length - iSecond);
}





public static void main(String args[]) throws Exception
{
    String list="";
    int i=0,n=0;

    MergeSort s= new MergeSort();
    ArrayList<Integer> arrlist=new ArrayList<Integer>();
    System.out.println(" ");
    System.out.println(" ");
    System.out.println("Please enter the list of elements,one element per line");
    System.out.println(" write 'STOP' when list is completed ");
    BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
    while(!(list=bf.readLine()).equalsIgnoreCase("stop")){
        int intelement=Integer.parseInt(list);
        arrlist.add(intelement);

    }

    int elementlist[]  = new int[arrlist.size()];
    Iterator<Integer> iter = arrlist.iterator();
    for (int j=0;iter.hasNext();j++) {
        elementlist[j] = iter.next();
    }

    elementlist=mergeSort(elementlist);
    System.out.println(" ");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println("Values after Merge Sort : ");
    for (int j=0;j<elementlist.length;j++) {
        System.out.println(elementlist[j]+" ");
    }
}

}

任何人都可以帮助我解决这个难题吗?我需要做的就是使用我教授的代码生成随机数,并查看排序需要多长时间。

1 个答案:

答案 0 :(得分:1)

请自行编写代码以完成作业。另外,这是一个剪切和粘贴问题,请不要在这里提出这样的问题。

  1. 在项目中剪切并粘贴mergeSort和merge方法。
  2. 在mergeSort和merge方法中将所有类型的int []更改为double []。
  3. mergeSort(ArrayToBeSorted);放在time1和elapsed之间。
  4. 对于其他排序,请将您自己的排序代码作为方法,并使用您自己的方法而不是mergeSort重试步骤1-3。