1 package com.tang.login; 2 import java.util.*; 3 4 public class SortTest { 5 6 //冒泡排序 和 快速排序 速度比较 7 public static void main(String[] args) { 8 9 int [] a =new int[1000000] ; 10 for(int i=0;i<1000000;i++){ 11 int random = (int) (Math.random()*1000000); 12 a[i]=random; 13 14 } 15 16 long startTime1=System.currentTimeMillis(); 17 18 ooo(a); 19 // quickSort(a ,0,a.length-1); 20 21 long endTime1=System.currentTimeMillis(); 22 23 System.out.println("程序运行时间: "+(endTime1-startTime1)+"毫秒"); 24 25 } 26 27 28 /** 29 * 冒泡排序ooo 30 * @param a 31 */ 32 public static void ooo(int [] a){ 33 34 int n=a.length; 35 for (int j = 0; j < n; j++) { 36 37 38 for(int i=1;i<n;i++){ 39 if(a[i]<a[i-1]){ 40 int k; 41 k=a[i-1]; 42 a[i-1]=a[i]; 43 a[i]=k; 44 45 } 46 47 } 48 49 n--; 50 } 51 52 53 54 } 55 56 57 58 59 //快速排序算法 60 61 /** 62 * 快速排序quickSort 63 * @param a 64 * @param low 65 * @param high 66 */ 67 public static void quickSort(int[] a ,int low,int high){ 68 if(low<high){ 69 int pivot =partition(a,low,high); 70 quickSort(a, low,pivot-1); 71 quickSort(a,pivot+1,high); 72 73 } 74 75 } 76 77 public static int partition(int a[],int low,int high){ 78 int key = a[low]; 79 int temp; 80 while(low<high){ 81 82 83 for(;low<high;high--){ 84 if(a[high]<key){ 85 temp = a[high]; 86 a[high]=a[low]; 87 a[low]=temp; 88 89 break; 90 } 91 } 92 for(;low<high;low++){ 93 if(a[low]>key){ 94 temp = a[high]; 95 a[high]=a[low]; 96 a[low]=temp; 97 break; 98 } 99 100 } 101 102 } 103 return low; 104 } 105 106 107 }