繁体   English   中英

排序代码和经过时间

[英]Sorting Code And elapsed time

所以我有这两个代码。 一种叫做MyTimer和SortCode。 MyTimer计算一段代码的经过时间,然后SortCode读取文件并打印出排序的代码。 如何使用MyTimer查找每种排序代码的经过时间。

import java.io.*;
import java.util.*;


public class SortCode
{
    public static void main(String[] args)
    {
        ArrayList<Integer> hold = new ArrayList<Integer>();
        try(
              //Open files
              FileReader reader = new FileReader("TestData.txt");
              Scanner in = new Scanner(reader);
              FileWriter writer = new FileWriter("SortedData.txt");
              PrintWriter out = new PrintWriter(writer);
           )
        {
            while(in.hasNextLine())
            {
                String next = in.nextLine();
                try
                {
                    int n = Integer.parseInt(next);
                    hold.add(n);
                }
                catch(NumberFormatException e)
                {
                    System.out.println("Invalid input " + next);
                }
            }

            insertionSort(hold);
            for(Integer i : hold)
               out.printf("%7d\n", i);
        }
        catch(IOException e)
        {
            System.out.println("Error opening the files." + e);
            System.exit(1);
        }

    }

  public static <T extends Comparable<T>>
  void selectionSort(List<T> table)
  {
     int size = table.size();
     for(int i = 0; i < size - 1; i++)
     {
        int minPos = i;
        T minValue = table.get(i);
        for(int k = i + 1; k < size; k++)
        {
           T nextValue = table.get(k);
           if( nextValue.compareTo(minValue) < 0)
           {
               minPos = k;
               minValue = nextValue;
           }
        }
        if(minPos != i)
        {
           T temp = table.get(i);
           table.set(minPos,temp);
           table.set(i, minValue);
        }
     }
  }


    public static <T extends Comparable<T>>
    int binSearch(ArrayList<T> table, int low, int high, T value)
    {
        while(low <= high)
        {
            int mid = (low + high)/2;
            int result = value.compareTo(table.get(mid));
            if(result  == 0)
              return mid;
            if(result < 0)
              high = mid - 1;
            else low = mid + 1;
        }
        return -low -1;
    }

    public static <T extends Comparable<T>>
    void insertionSort(ArrayList<T> table)
    {
      int size = table.size();
      for(int i = 1; i < size; i++)
      {
         T temp = table.remove(i);
         int pos = binSearch(table, 0, i - 1, temp);
         if(pos < 0)
           pos = -pos - 1;
         table.add(pos, temp);

      }
    }

    public static <T extends Comparable<T>>
    void mergeSort(List<T> table)
    {
      int size = table.size();
      if(size <= 1)
         return;

      int size1 = size/2;
      int size2 = size - size1;
      List<T> v1 = new ArrayList<T>();
      List<T> v2 = new ArrayList<T>();

      for(int i = 0; i < size1; i++)
         v1.add(table.get(i));
      for(int i = 0; i < size2; i++)
         v2.add(table.get(size1 + i));

      mergeSort(v1);
      mergeSort(v2);

      int i1 = 0, i2 = 0;
      int i = 0;
      T value1 = v1.get(i1);
      T value2 = v2.get(i2);

      while(i1 < size1 && i2 < size2)
      {
        if(value1.compareTo(value2) <= 0)
        {
           table.set(i++, value1);
           i1++;
           if(i1 < size1)
             value1 = v1.get(i1);
        }
        else
        {
           table.set(i++, value2);
           i2++;
           if(i2 < size2)
               value2 = v2.get(i2);
        }
      }
      int k;
      if(i1 < size1)
      {
         for(k = i1; k < size1; k++)
            table.set(i++, v1.get(k));
      }
      else
      {
         for(k = i2; k < size2; k++)
           table.set(i++, v2.get(k));
      }
    }

}

这是MyTimer

public class MyTimer
{
  private long    elapsedTime;
  private long    startTime;
  private boolean on;


  /**
  Create a time in the "not running" state and initialize its elapsed time to zero.
  */
  public MyTimer()
  {
    this.on = false;
    this.elapsedTime = 0L;
  }

  /**
  Change the state of the timer to running, initialize its elapsed time and
  set its start time
  */
  public void start()
  {
    this.on = true;
    this.elapsedTime = 0L;
    this.startTime = System.nanoTime();
  }

  /**
  If the timer is in the running state change its state to "not running" and
  accumulate elapsed time.  If the timer is in the "not running" state stop
  has no effect.
  */
  public void stop()
  {
    if(this.on)
    {
      this.on = false;
      long currentTime = System.nanoTime();
      this.elapsedTime += (currentTime - this.startTime);
    }
  }

  /**
  If the timer is in the "not running" state, change its state to running, but do
  not reset its elapsed time.  New running time will be accumulated with previous
  running time.
  */
  public void resume()
  {
    if(!this.on)
    {
      this.startTime = System.nanoTime();
      this.on = true;
    }
  }

  /**
  Return the elapsed time that this timer has accumulated.  If the
  timer is in the running state, the elapsed time must be calculated.
  If the timer is in the "not running" state the elapsed time was already
  calculated by the stop function.
  */
  public long getElapsedTime()
  {
    if (this.on)
    {
      long currentTime = System.nanoTime();
      this.elapsedTime += (currentTime - this.startTime);
      this.startTime = currentTime;
    }
    return this.elapsedTime;
  }

  /**
  Return elapsed time of the current timer in the form of a string.
  */
  public String toString()
  {
    String result = "" + this.getElapsedTime();
    return result;
  }
}

你可以这样使用

MyTimer() timer = new MyTimer(); // create new MyTimer instance 
timer.start(); // start the timer
insertionSort(hold); // run the sorting code here
timer.stop(); // stop the timer
long elapsedTime = timer.getElapsedTime(); // get the elapsed time

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM