实验控制器,使用通用数组和方法。

huangapple go评论74阅读模式
英文:

Experiment controller using a generic array and method

问题

我正试图找出使用冒泡排序方法和选择排序方法对整数数组进行排序所需的时间。

public class ExperimentController
{
    private static int[] intArray = {};
    

    public static void main(String[] args)
    {
        System.out.println("");
        ExperimentController EX = new ExperimentController();

        for(int i = 0; i <= 300; i = i + 50){
            long time = EX.timeBubbleSortPerformance(i, 0);
            System.out.println(time);

        }
    }

    public long timeBubbleSortPerformance(int numberOfItems, int seed){

        long startTime = System.nanoTime();
        RandomStuffContainer RSC = new RandomStuffContainer();
        Random random = new Random(seed);
        for(int i = 0; i < numberOfItems; i++){
            int randomInt = random.nextInt();
            RSC.addToFront(intArray[randomInt]);   
            RSC.selectionSort(intArray[randomInt]);   

        }
        
        long stopTime = System.nanoTime();
        long timeTotal = stopTime - startTime;
        return timeTotal;
    }
}

调用方法 RandomStuffContainer 类中的随机容器方法。

public class RandomStuffContainer<T extends Comparable<T>>
{
    ArrayList<T> array = new ArrayList<T>();

    public void addToFront(T value)
    {
        array.add(0, value);
    }

    public void addToBack(T value)
    {
        array.add(value);
    }

    public void selectionSort() 
    {
        for(int i = 0; i < array.size() - 1; i++)
        {
            int smallestIndex = i;

            for(int j = i + 1; j < array.size(); j++)
            {
                if(array.get(smallestIndex).compareTo((array.get(j))) > 0)
                {
                    smallestIndex = j;
                }
            }
            T temp = array.get(i);
            array.add(i, array.get(smallestIndex));
            array.add(smallestIndex, temp);
        } 
    }

    public void bubbleSort(){
        T temp;

        if (array.size() > 1)
        {
            for (int x = 0; x < array.size(); x++)
            {
                for (int i = 0; i < array.size() - i; i++) {
                    if (array.get(i).compareTo(array.get(i + 1)) > 0)
                    {
                        temp = array.get(i);
                        array.set(i, array.get(i + 1));
                        array.set(i + 1, temp);
                    }
                }
            }
        }
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (T s : array)
        {
            sb.append(s.toString());
            sb.append("\t");
        }
        return sb.toString();
    }
}

我试图使用随机数填充 RandomStuffContainer 中的数组,然后使用排序方法对其进行排序,并从中获取运行时。我知道我的代码很混乱,对此我感到抱歉,但任何帮助将不胜感激。

英文:

I am trying to find the time it takes for a Bubble sort method of sorting an integer array vs a selection sort.

public class ExperimentController
{
private static int[] intArray = {};
public static void main(String[] args)
{
System.out.println(&quot;&quot;);
ExperimentController EX = new ExperimentController();
for(int i =0; i&lt;=300;i=i+50){
long time = EX.timeBubbleSortPerformance(i, 0);
System.out.println(time);
}
}
public long timeBubbleSortPerformance(int numberOfItems, int seed){
long startTime = System.nanoTime();
RandomStuffContainer RSC = new RandomStuffContainer();
Random random = new Random(seed);
for(int i=0; i&lt;numberOfItems; i++){
int randomInt = random.nextInt();
RSC.addToFront(intArray[randomInt]);   
RSC.selectionSort(intArray[randomInt]);   
}
long stopTime = System.nanoTime();
long timeTotal = stopTime-startTime;
return timeTotal;
}
}

The method calls the Random stuff container class for the methods.

public class RandomStuffContainer&lt;T extends Comparable&lt;T&gt;&gt;
{
ArrayList&lt;T&gt; array = new ArrayList&lt;T&gt;();
public  void main(String[] args){
ArrayList&lt;T&gt; array = new ArrayList&lt;T&gt;();
}
public void addToFront(T value)
{
array.add(0, value);
}
/**
* Adds the inputted String i to the back of the Arraylist
*
* @param  A generic parameter T
*/
public void addToBack(T value)
{
array.add(value);
}
/**
* Sorts the Arraylist using selection sort algorithim
*
*/
public void selectionSort() 
{
for(int i=0; i&lt;array.size() -1; i++)
{
int smallestIndex = i;
for(int j=i+1; j&lt;array.size(); j++)
{
if(array.get(smallestIndex).compareTo((array.get(j))) &gt; 0  )
{
smallestIndex = j;
}
}
T temp = array.get(i);
array.add(i,array.get(smallestIndex));
array.add(smallestIndex, temp);
} 
}
public void bubbleSort(){
T temp;
if (array.size()&gt;1) // check if the number of orders is larger than 1
{
for (int x=0; x&lt;array.size(); x++) // bubble sort outer loop
{
for (int i=0; i &lt; array.size()-i; i++) {
if (array.get(i).compareTo(array.get(i+1)) &gt; 0)
{
temp = array.get(i);
array.set(i,array.get(i+1) );
array.set(i+1, temp);
}
}
}
}
}
/**
* Copies array list data to String and returns String.
* Used for testing
*/
public String toString() {
{
StringBuilder sb = new StringBuilder();
for (T s : array)
{
sb.append(s.toString());
sb.append(&quot;\t&quot;);
}      
return  sb.toString();
}
}
}

I am trying to use random to randomly fill the array in RandomStuffContainer with ints and then use the sort method to sort that and get the runtime from it. I know my code is messy and sorry for that but any help would be very much appreciated.

答案1

得分: 1

启用所有编译器警告,并且注意处理这些警告。编译器会提示你在 RandomStuffContainer RSC = new RandomStuffContainer(); 中忘记了指定类型。你可能想要这样做:

RandomStuffContainer<Integer> RSC = new RandomStuffContainer<>();

另外,random.nextInt() 返回一个随机的 32 位整数值。这个返回值并不保证是 intArray 中的有效索引。

你会想要限制返回值的范围:

int randomInt = random.nextInt(intArray.length);

查看 RandomStuffContainer 中的 selectionSort 方法。它不接受任何参数。因此你不应该传递任何参数给它。同样,编译器已经提示了你这一点。

你不应该每次向 RandomStuffContainer 中添加内容时都调用 selectionSort。将所有元素添加到容器中,在循环完成之后调用 selectionSort()

英文:

Enable all compiler warnings, and pay attention to them. The compiler will tell you that you forgot to specify a type in RandomStuffContainer RSC = new RandomStuffContainer();. You probably wanted to do this:

RandomStuffContainer&lt;Integer&gt; RSC = new RandomStuffContainer&lt;&gt;();

Also, random.nextInt() returns a random 32-bit int value. The returned value is by no means guaranteed to be a valid index in intArray.

You’ll want to limit the value returned:

int randomInt = random.nextInt(intArray.length);

Look at the selectionSort method in RandomStuffContainer. It does not take any arguments. So you should not be passing any arguments to it. Again, the compiler told you this.

You should not call selectionSort every time you add something to the RandomStuffContainer. Give the container all the elements, and after the loop is done, call selectionSort().

huangapple
  • 本文由 发表于 2020年9月14日 05:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63875802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定