十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
java编程实现随机数组的快速排序步骤如下:
创新互联公司是一家专业提供盘州企业网站建设,专注与网站设计制作、成都网站制作、H5开发、小程序制作等业务。10年已为盘州众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
1、打开Eclipse,新建一个Java工程,在此工程里新建一个Java类;
2、在新建的类中声明一个产生随机数的Random变量,再声明一个10个长度的int型数组;
3、将产生的随机数逐个放入到数组中;
4、利用排序算法对随机数组进行排序。
具体代码如下:
import java.util.Random;
public class Demo {
public static void main(String[] args) {
int count = 0;
Random random = new Random();
int a[] = new int[10];
while(count 10){
a[count] = random.nextInt(1000);//产生0-999的随机数
count++;
}
for (int i = 0; i a.length - 1; i++) {
int min = i;
for (int j = i + 1; j a.length; j++) {
if (a[j] a[min]) {
min = j;
}
}
if (min != i) {
int b = a[min];
a[min] = a[i];
a[i] = b;
}
}
for (int c = 0; c a.length; c++) {
System.out.print(a[c] + " ");
}
}
}
本人特地给你编的代码\x0d\x0a亲测\x0d\x0a\x0d\x0apublicclassQuickSort{\x0d\x0a\x0d\x0apublicstaticintPartition(inta[],intp,intr){\x0d\x0aintx=a[r-1];\x0d\x0ainti=p-1;\x0d\x0ainttemp;\x0d\x0afor(intj=p;jif(a[j-1]//swap(a[j-1],a[i-1]);\x0d\x0ai++;\x0d\x0atemp=a[j-1];\x0d\x0aa[j-1]=a[i-1];\x0d\x0aa[i-1]=temp;\x0d\x0a\x0d\x0a}\x0d\x0a}\x0d\x0a//swap(a[r-1,a[i+1-1]);\x0d\x0atemp=a[r-1];\x0d\x0aa[r-1]=a[i+1-1];\x0d\x0aa[i+1-1]=temp;\x0d\x0a\x0d\x0areturni+1;\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0apublicstaticvoidQuickSort(inta[],intp,intr){\x0d\x0a\x0d\x0aif(p
参考代码如下,可以按需求自己修改
import java.util.Date;
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) (Math.random()*1000000)+11;
int[] ary= new int[n];
System.out.println("n的值是" + n);
for (int i = 0; i ary.length; i++) {
ary[i] = (int) (Math.random()*100000);
}
return ary;
}
}
//冒泡排序
class MaoPao implements Runnable {
int[] ary;
public MaoPao(int[] ary) {
this.ary = ary;
}
@Override
public void run() {
long st = System.currentTimeMillis();
System.out.println(new Date() + "冒泡排序线程:开始执行排序");
for (int i = 0; i ary.length - 1; i++) {
for (int j = 0; j ary.length - i - 1; j++) {
if (ary[j] ary[j + 1]) {
int temp = ary[j];
ary[j] = ary[j + 1];
ary[j + 1] = temp;
}
}
}
long et = System.currentTimeMillis();
System.out.println(new Date() + "冒泡排序线程完成排序,耗费时间" + (et - st) + "毫秒");
for (int i = 0; i ary.length; i++) {
System.out.println(ary[i]+" ");
}
}
}
//快速排序
class KuaiSu implements Runnable {
int[] ary;
public KuaiSu(int[] ary) {
this.ary = ary;
}
@Override
public void run() {
long st = System.currentTimeMillis();
System.out.println(new Date() + "快速排序线程:开始执行排序");
quickSort(ary, 1, ary.length);
long et = System.currentTimeMillis();
System.out.println(new Date() + "快速排序线程排序完成,耗费时间" + (et - st) + "毫秒");
for (int i = 0; i ary.length; i++) {
System.out.println(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);
}
}
}
① 代码:
public class quicksortdemo {
private int array[];
private int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
// Divide into two arrays
while (i = j) {
while (array[i] pivot) {
i++;
}
while (array[j] pivot) {
j--;
}
if (i = j) {
swap(i, j);
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndex j)
quickSort(lowerIndex, j);
if (i higherIndex)
quickSort(i, higherIndex);
}
private void swap(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
quicksortdemo sorter = new quicksortdemo();
int[] input = {24,2,45,20,56,75,2,56,99,53,12};
sorter.sort(input);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
}
② 运行:
c:\java quicksortdemo
2 2 12 20 24 45 53 56 56 75 99