-2

QuickSort Random Real Numbers 使用您自己的自定义随机发生器生成一个包含 150 个数字的随机数组。1)显示未排序的数字。2)启动秒表并显示时间。3) 使用 QuickSort 对数组进行排序。4)停止秒表并显示它运行了多长时间。5)按升序显示数字。

这是我的随机生成器类

/**
*/
  class RandomGenerator
{
  public RandomGenerator()
{
}

public double randomLong(double shift)
{
  long x = System.nanoTime();
  x ^= (x <<(int)(shift/100)); 
  x ^= (x >>>(int)(shift%10)); 
  x ^= (x << (int)Math.sqrt(shift)); 
  if ((x>0)||(x<0))
{
  return x;
 }
  else
  return(randomLong(System.nanoTime()*Math.sqrt(System.nanoTime())));
 }
}

这是我的快速排序课程

 public class QuickSort
 {
 public QuickSort()
{
 }
 public double[] sort(double[] a)
{
 sort(a, 0, a.length - 1);
 return(a);
}
 private void sort(double[] a, int from, int to)
{
 if (from >= to){return; }
 int p = partition(a, from, to);
 sort(a, from, p);
 sort(a, p + 1, to);
 }
  private int partition(double[] a, int from, int to)
 {
  double pivot = a[from];
  int i = from - 1;
  int j = to + 1;
  while(i < j)
  {
     i++; while (a[i] < pivot) {i++;}
 j--; while (a[j] > pivot) {j--;}
 if(i < j)
 {
  double temp;
  temp = a[i];
  a[i] = a[j];
  a[j] = temp;
  }
    }
   return j;
   }
 }

这是我的秒表课

/**
   A stopwatch accumulates time when it is running. You can 
   repeatedly start and stop the stopwatch. You can use a
   stopwatch to measure the running time of a program.
 */
     public class StopWatch
      {  
       private long elapsedTime;
       private long startTime;
       private boolean isRunning;

    /**
      Constructs a stopwatch that is in the stopped state
      and has no time accumulated.
   */
   public StopWatch()
{  
   reset();
}

 /**
  Starts the stopwatch. Time starts accumulating now.
 */
 public void start()
{  
   if (isRunning) { return; }
   isRunning = true;
   startTime = System.currentTimeMillis();
}

/**
   Stops the stopwatch. Time stops accumulating and is
   is added to the elapsed time.
*/
public void stop()
{  
   if (!isRunning) { return; }
   isRunning = false;
   long endTime = System.currentTimeMillis();
   elapsedTime = elapsedTime + endTime - startTime;
}

 /**
   Returns the total elapsed time.
   @return the total elapsed time
 */
public long getElapsedTime()
{  
   if (isRunning) 
   {  
      long endTime = System.currentTimeMillis();
      return elapsedTime + endTime - startTime;
   }
   else
   {
      return elapsedTime;
   }
}

/**
   Stops the watch and resets the elapsed time to 0.
*/
public void reset()
{  
   elapsedTime = 0;
   isRunning = false;
 }

}

这是我的查看器类或主要方法(我真的需要帮助)

 public class QsortViewer
 {
 private static final int MAX_ELEMENTS=150; 
 public static void main(String[] args)
 {
  RandomGenerator RG = new RandomGenerator();
  QuickSort QS = new QuickSort();
  StopWatch SW = new StopWatch();

 double[] numbers = new double[MAX_ELEMENTS];

 for (int i = 0; i <= numbers.length-1; i++)
{
 numbers[i] = RG.randomLong(i*i*Math.sqrt(i)*Math.pow(i,i));
  }
  }
}

我所有的课程都是正确的,我需要帮助的是 1)显示未排序的数字。2)启动秒表并显示时间。3) 使用 QuickSort 对数组进行排序。4)停止秒表并显示它运行了多长时间。5)按升序显示数字。现在我的主要方法已部分完成,所以我可以在完成我的主要方法方面获得一些帮助。这意味着调用正确的类并对数组进行排序并显示它们。谢谢你

4

1 回答 1

0

我觉得我可能在这里遗漏了一些东西,因为您似乎已经完成了所有繁重的工作,但这是我的草稿

public class QsortViewer{
    private static final int MAX_ELEMENTS=150; 

     public static void main(String[] args){
        RandomGenerator RG = new RandomGenerator();
        QuickSort QS = new QuickSort();
        StopWatch SW = new StopWatch();

        double[] numbers = new double[MAX_ELEMENTS];

        System.out.println("Unordered set: ");
        for (int i = 0; i <= numbers.length-1; i++){
            numbers[i] = RG.randomLong(i*i*Math.sqrt(i)*Math.pow(i,i));
            System.out.print(numbers[i]+" ");
        }

        SW.start();
        System.out.println("Start Time: "+SW.getStartTime());

        numbers = QS,sort(numbers);

        SW.stop();
        System.out.println("Elapsed Time: "+SW.getElapsedTime());

        System.out.println("Sorted Array: ");
        for (int i = 0; i <= numbers.length-1; i++){
            System.out.print(numbers[i]+" ");
        }
    }
}

我调用了一个您尚未编写的名为 SW.getStartTime() 的方法,它只会返回 SW 对象中记录的开始时间的值。如果这是硬件分配或您无法编辑 SW 的东西,那么将 SW.getStartTime() 替换为 System.currentTimeMillis() 也可以。

于 2013-03-02T00:04:12.590 回答