具体原理有时间再写
冒泡排序:
package com.allqj.examination.examination.util;
/**
* @Author WF
* @DesCription 冒泡排序
* @Date 2019/7/14 11:33
**/
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {9, 5, 8, 7, 2, 6, 4, 3,11,15,20,16,13,18};
System.out.println(arr.length);
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int number : arr)
System.out.print(number + " ");
}
}
快速排序:
package com.allqj.examination.examination.util;
import java.util.Arrays;
/**
* @Author WF
* @DesCription 快速排序
* @Date 2019/7/14 14:16
**/
public class QuickSort {
private static void sort(int[] arr, int low, int high) {
if (low >= high)
return;
int index = arr[low];//基数
int i = low;
int j = high;
while (low != high) {
while (arr[high] >= index && low < high) {
high--;
}
while (arr[low] <= index && low < high) {
low++;
}
if (low < high) {
int temp = arr[low];
arr[low] = arr[high];
arr[high] = temp;
}
}
arr[i] = arr[low];
arr[low] = index;
sort(arr, i, low - 1);
sort(arr, low + 1, j);
}
public static void main(String[] args) {
int[] arr = {8, 9, 1, 2, 8, 5, 3, 4, 7, 6, 4, 8, 9, 2, 74, 6, 8, 6, 2};
sort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
}