国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

快速排序Java實現(xiàn)

2019-11-17 04:04:49
字體:
供稿:網(wǎng)友
 package quickSort;

import java.util.Random;
import java.util.Scanner;

public class QuickSort {
public static int n = 0;
public static int[] array = null;

public static void main(String[] args) {
  System.out.PRintln("請輸入比較的數(shù)的個數(shù)n: ");
  Scanner sc = new Scanner(System.in);
  n = sc.nextInt();
  array = new int[n];
  Random r = new Random();
  for(int i=0; i<n;) {
   array[i] = r.nextInt(6000);
   //System.out.print(array[i] + "  ");
   i++;
   /*if(i%10 == 0) {
    System.out.println();
   }*/
  }
  long t = System.currentTimeMillis();
  qSort(0, n-1);
  System.out.println("排序所用時間為: " + (System.currentTimeMillis() - t) + "ms");
  /*System.out.println("排序后的數(shù)如下:");
  for(int i=0; i<n;) {
   System.out.print(array[i] + "  ");
   i++;
   if(i%10 == 0) {
    System.out.println();
   }
  }*/
}

public static void qSort(int p, int r) {
  if(p < r) {
   int pt = partition(p, r);
   qSort(p, pt-1);
   qSort(pt+1, r);
  }
}

public static int partition(int p, int r) {
  int pt = (int) (p + Math.random()*(r-p));//產(chǎn)生處于p和r之間的隨機數(shù)
  int flag = array[pt];
  int i=p-1;
  int j=r+1;
  while(true) {
   while(array[++i] > flag);
   while(array[--j] < flag);
   if(i>=j) break;
   int swap = array[i];
   array[i] = array[j];
   array[j] = swap;
  }
  array[i] = flag;
  return i;
}
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 雅安市| 彭山县| 武胜县| 建德市| 泗阳县| 红原县| 玉山县| 七台河市| 沂南县| 平谷区| 利津县| 望谟县| 泸定县| 固始县| 尉氏县| 无棣县| 绍兴市| 海兴县| 邢台市| 黄龙县| 怀宁县| 峨眉山市| 海阳市| 利津县| 龙山县| 榆林市| 灵武市| 明溪县| 昭通市| 张家界市| 靖西县| 百色市| 宁阳县| 修武县| 吉安县| 开鲁县| 定远县| 五寨县| 商城县| 汝南县| 方正县|