I need to create an array of size 500, and data is randomly distributed and I'm really confused about how to sort this array. Can somebody help me, please?
我需要创建一个大小为500的数组,并且数据是随机分布的,我真的很困惑如何对这个数组进行排序。请问有人可以帮助我吗?
The code for random:
随机代码:
import java.util.Random;
public class SortTest {
public static void main(String[] args) {
// create an instance of Random class for random number generation
Random random = new Random (1L);
// begin new scope
{
// create an int array of 500 elements
int[] dataArray = new int[500];
// populate the array with randomly generated values
for (int index = 0; index < 500; index++)
dataArray[index] = random.nextInt();
// end scope
}
}
}
Do I need use Arrays.sort(dataArray)? Or Arrays.sort(dataArray, Collections.reverseOrder())? But I heard somebody said those are not for primary class.
我需要使用Arrays.sort(dataArray)吗?或Arrays.sort(dataArray,Collections.reverseOrder())?但我听说有人说那些不适合小班教学。
Please help!!! Thank you!!
请帮忙!!!谢谢!!
Here should be my steps:
这应该是我的步骤:
- Create a 500-element integer array using the Random class’s nextInt() method
- Make a copy of this array and use it as a parameter to sort methods (array is passed by reference).
- Measure execution time to sort the 500-element array using bubble, selection, insertion, quicksort, and mergesort
- Create a 5000000-element integer array using the Random class’s nextInt() method.
- Make a copy of this array and use it as a parameter to sort methods (array is passed by reference).
- Measure execution time to sort the 5000000-element array using bubble, selection, insertion, quicksort, and mergesort.
- Measure execution time to sort an already sorted (in increasing order) 500-element array using bubble, selection, insertion, quicksort, and mergesort.
- Measure execution time to sort an already reverse sorted (in decreasing order) 500- element array using bubble, selection, insertion, quicksort, and mergesort.
- Measure execution time to sort an already sorted (in increasing order) 5000000-element array using bubble, selection, insertion, quicksort, and mergesort.
- Measure execution time to sort an already reverse sorted (in decreasing order) 5000000- element array using bubble, selection, insertion, quicksort, and mergesort.
- Tabulate your results and comment on sensitivity of the six sorting algorithms on input data distribution
使用Random类的nextInt()方法创建一个500元素的整数数组
制作此数组的副本并将其用作参数来排序方法(数组通过引用传递)。
测量执行时间,以使用气泡,选择,插入,快速排序和合并排序对500个元素的阵列进行排序
使用Random类的nextInt()方法创建5000000元素的整数数组。
制作此数组的副本并将其用作参数来排序方法(数组通过引用传递)。
测量执行时间,以使用气泡,选择,插入,快速排序和合并排序对5000000元素数组进行排序。
测量执行时间,以使用气泡,选择,插入,快速排序和合并排序对已排序(按递增顺序)的500个元素数组进行排序。
使用bubble,selection,insertion,quicksort和mergesort测量执行时间以对已经反向排序(按递减顺序)的500个元素数组进行排序。
测量执行时间,以使用气泡,选择,插入,快速排序和合并排序对已排序(按递增顺序)5000000元素数组进行排序。
测量执行时间,使用气泡,选择,插入,快速排序和合并排序对已经反向排序(按降序排列)5000000-元素数组进行排序。
列出结果并评论六种排序算法对输入数据分布的敏感性
I'm confused about strp 7 and 8, how can I sort that array distributed above. And where I should add that in? I used Arrays.sort() but the result was printed out is not in increasing order.
我对strp 7和8感到困惑,我怎样才能对上面分布的数组进行排序。我应该在哪里添加?我使用了Arrays.sort()但是打印出的结果并没有按递增顺序排列。
2 个解决方案
#1
1
If you are asking about a good way to get a sorted array of random numbers, here's a simple solution using Java 8 streams:
如果你问一个获得随机数排序数组的好方法,这里有一个使用Java 8流的简单解决方案:
int[] randoms = Random.ints(500).sorted().toArray();
#2
0
either use this method public static void sort(int[]);
from java.util.Arrays
Class
使用此方法public static void sort(int []);来自java.util.Arrays类
Arrays.sort(dataArray);
OR
Change your array declaration from
从中更改数组声明
int[] dataArray = new int[500];
to
ArrayList<Integer> dataArray = new ArrayList<Integer>();
and then you may apply sort()
method from Collection
Class to Sort your Array. beacause Integer
is a Comparable
type of Class.
然后你可以从Collection Class中应用sort()方法来对你的Array进行排序。 beacause Integer是一种可比较的类。
public final class java.lang.Integer extends java.lang.Number implements Comparable<java.lang.Integer>
#1
1
If you are asking about a good way to get a sorted array of random numbers, here's a simple solution using Java 8 streams:
如果你问一个获得随机数排序数组的好方法,这里有一个使用Java 8流的简单解决方案:
int[] randoms = Random.ints(500).sorted().toArray();
#2
0
either use this method public static void sort(int[]);
from java.util.Arrays
Class
使用此方法public static void sort(int []);来自java.util.Arrays类
Arrays.sort(dataArray);
OR
Change your array declaration from
从中更改数组声明
int[] dataArray = new int[500];
to
ArrayList<Integer> dataArray = new ArrayList<Integer>();
and then you may apply sort()
method from Collection
Class to Sort your Array. beacause Integer
is a Comparable
type of Class.
然后你可以从Collection Class中应用sort()方法来对你的Array进行排序。 beacause Integer是一种可比较的类。
public final class java.lang.Integer extends java.lang.Number implements Comparable<java.lang.Integer>