参考代码如下,可以按需求自己修改
import javautilDate;public class SortThread {
public static void main(String[] args) {
//产生一个随机数组
int[] ary = getArray();
//启动冒泡排序线程
new Thread(new MaoPao(ary))start();
//启动快速排序线程
new Thread(new KuaiSu(ary))start();
}
private static int[] getArray() {
//建议数字n不要超过1百万,十万左右就好了
int n = (int) (Mathrandom()1000000)+11;
int[] ary= new int[n];
Systemoutprintln("n的值是" + n);
for (int i = 0; i < arylength; i++) {
ary[i] = (int) (Mathrandom()100000);
}
return ary;
}
}
//冒泡排序
class MaoPao implements Runnable {
int[] ary;
public MaoPao(int[] ary) {
thisary = ary;
}
@Override
public void run() {
long st = SystemcurrentTimeMillis();
Systemoutprintln(new Date() + "冒泡排序线程:开始执行排序");
for (int i = 0; i < arylength - 1; i++) {
for (int j = 0; j < arylength - i - 1; j++) {
if (ary[j] < ary[j + 1]) {
int temp = ary[j];
ary[j] = ary[j + 1];
ary[j + 1] = temp;
}
}
}
long et = SystemcurrentTimeMillis();
Systemoutprintln(new Date() + "冒泡排序线程完成排序,耗费时间" + (et - st) + "毫秒");
for (int i = 0; i < arylength; i++) {
Systemoutprintln(ary[i]+" ");
}
}
}
//快速排序
class KuaiSu implements Runnable {
int[] ary;
public KuaiSu(int[] ary) {
thisary = ary;
}
@Override
public void run() {
long st = SystemcurrentTimeMillis();
Systemoutprintln(new Date() + "快速排序线程:开始执行排序");
quickSort(ary, 1, arylength);
long et = SystemcurrentTimeMillis();
Systemoutprintln(new Date() + "快速排序线程排序完成,耗费时间" + (et - st) + "毫秒");
for (int i = 0; i < arylength; i++) {
Systemoutprintln(ary[i]+" ");
}
}
public static int Partition(int a[], int p, int r) {
int x = a[r - 1];
int i = p - 1;
int temp;
for (int j = p; j <= r - 1; j++) {
if (a[j - 1] <= x) {
i++;
temp = a[j - 1];
a[j - 1] = a[i - 1];
a[i - 1] = temp;
}
}
temp = a[r - 1];
a[r - 1] = a[i + 1 - 1];
a[i + 1 - 1] = temp;
return i + 1;
}
public static void quickSort(int a[], int p, int r) {
if (p < r) {
int q = Partition(a, p, r);
quickSort(a, p, q - 1);
quickSort(a, q + 1, r);
}
}
}
以上就是关于请用java语言编写排序程序。全部的内容,包括:请用java语言编写排序程序。、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)