# JAVA中的冒泡排序，选择排序与快速排序

public static void main(String[] args) throws Exception {
int length = 200000;
long startTime = System.currentTimeMillis();
int[] bubblingArr = createArr(length);
bubblingSort(bubblingArr);
long bubblingTime = System.currentTimeMillis();
logger.info("bubblingSort最终耗时为:" + (bubblingTime - startTime));
int[] choiceArr = createArr(length);
choiceSort(choiceArr);
long choiceTime = System.currentTimeMillis();
logger.info("choiceSort最终耗时为:" + (choiceTime - bubblingTime));
int[] quickArr = createArr(length);
quickSort(quickArr, 0, quickArr.length-1);
long quickTime = System.currentTimeMillis();
logger.info("quickSort最终耗时为:" + (quickTime - choiceTime));
}

private static int[] createArr(int length) {
int[] array = new int[length];
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(800);
}
return array;
}

public static void quickSort(int[] arr, int _left, int _right) {
int left = _left;
int right = _right;
int temp;
if (left <= right) {   //待排序的元素至少有两个的情况
temp = arr[left];  //待排序的第一个元素作为基准元素
while (left != right) {   //从左右两边交替扫描，直到left = right
while (right > left && arr[right] >= temp)
right--;        //从右往左扫描，找到第一个比基准元素小的元素
arr[left] = arr[right];  //找到这种元素arr[right]后与arr[left]交换
while (left < right && arr[left] <= temp)
left++;         //从左往右扫描，找到第一个比基准元素大的元素
arr[right] = arr[left];  //找到这种元素arr[left]后，与arr[right]交换

}
arr[right] = temp;    //基准元素归位
quickSort(arr, _left, left - 1);  //对基准元素左边的元素进行递归排序
quickSort(arr, right + 1, _right);  //对基准元素右边的进行递归排序
}
}

private static void choiceSort(int[] ins) {
int n = ins.length - 1;//经过n-1次提取最小最大值
for (int i = 0; i < n; i++) {//控制选择的次数
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (ins[j] < ins[i]) min_index = j;
}
if (min_index != i) {
int temp = ins[i];
ins[i] = ins[min_index];
ins[min_index] = temp;
}
}
}

private static void bubblingSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
boolean flag = true;
for (int j = 0; j < a.length - i - 1; j++) { // 内存循环，是每一轮中进行的两两比较
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = false;
}
}
if (flag) break;
}
}


## 结论

03-15 278
01-14 93
07-19 368
09-12 459
07-02 315
08-14 297
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客