一、冒泡排序
public class BubbleSort {
public static void main(String[] args) {
// int[] arrays = {1, 4, 2, 3, 6, 8, 3, 21, 32, 5};
// int[] result = bubble(arrays);
// System.out.println(Arrays.toString(result));
//速度测试 23001
long begin = System.currentTimeMillis();
int[] array = new int[80000];
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 8000000);
}
int[] results = bubbleSort(array);
long end = System.currentTimeMillis();
System.out.println("用时:"+(end - begin));
System.out.println(Arrays.toString(results));
}
/**
* 冒泡排序
* 时间复杂度为:O(n^2)
*
* @param arr
* @return
*/
public static int[] bubbleSort(int[] arr) {
//临时变量
int temp;
//标识变量,表示是否进行过交换
boolean flag = false;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
//若当前数比后一个数字大,则进行位置交换
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true;
}
}
// System.out.println("第"+(i+1)+"次:"+Arrays.toString(arr));
//若一次排序过程中没有进行排序则退出
if (!flag) {
break;
} else {
//重置flag进行下一次的判断
flag = false;
}
}
return arr;
}
}
二、选择排序
public class SelectSort {
public static void main(String[] args) {
// int[] arrays = {1, 4, 16, 3, 10, 8, 7, 21, 32, 5};
// int[] result = selectSort(arrays);
// System.out.println(Arrays.toString(result));
//速度测试 7831
long begin = System.currentTimeMillis();
int[] array = new int[80000];
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 8000000);
}
int[] results = selectSort(array);
long end = System.currentTimeMillis();
System.out.println("用时:"+(end - begin));
System.out.println(Arrays.toString(results));
}
public static int[] selectSort(int[] arrays) {
for (int i = 0; i < arrays.length - 1; i++) {
int minIndex = i;
int min = arrays[i];
for (int j = i + 1; j < arrays.length; j++) {
if(min > arrays[j]){
min = arrays[j];
minIndex = j;
}
}
if(i != minIndex){
arrays[minIndex] = arrays[i];
arrays[i] = min;
}
// System.out.println((i+1)+":"+Arrays.toString(arrays));
}
return arrays;
}
}
三、插入排序
public class InsertSort {
public static void main(String[] args) {
// int[] arrays = {101, 34, 119, 1};
// int[] result = insertSort(arrays);
// System.out.println(Arrays.toString(result));
//速度测试 8049
long begin = System.currentTimeMillis();
int[] array = new int[80000];
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 8000000);
}
int[] results = insertSort(array);
long end = System.currentTimeMillis();
System.out.println("用时:"+(end - begin));
System.out.println(Arrays.toString(results));
}
public static int[] insertSort(int[] arrays) {
int temp;
for (int i = 1; i < arrays.length; i++) {
//找到当前元素的插入位置
for (int j = 0;j < i;j++){
if(arrays[i] < arrays[j]){
temp = arrays[i];
arrays[i] = arrays[j];
arrays[j] = temp;
}
}
// System.out.println(i+":"+Arrays.toString(arrays));
}
return arrays;
}
}
四、希尔排序
移位法比交换法快的多
希尔排序 交换法
public static int[] shellSortExchange(int[] arrays) {
int temp = 0;
//增量gap(步长),逐步缩小增量
for (int gap = arrays.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < arrays.length; i++) {
for (int j = i - gap; j >= 0; j -= gap) {
if (arrays[j] > arrays[j + gap]) {
temp = arrays[j];
arrays[j] = arrays[j + gap];
arrays[j + gap] = temp;
}
}
}
}
return arrays;
}
希尔排序 移位法
public static int[] shellSortMove(int[] arrays) {
int temp = 0;
int j;
//增量gap(步长),逐步缩小增量
for (int gap = arrays.length / 2; gap > 0; gap /= 2) {
//从第gap个元素,逐个对其所在的组直接进行插入排序
for (int i = gap; i < arrays.length; i++) {
j = i;
temp = arrays[j];
while (j - gap >= 0 && temp < arrays[j - gap]) {
//开始移动
arrays[j] = arrays[j - gap];
j -= gap;
//当退出while循环后,就给temp找到插入的位置
}
arrays[j] = temp;
}
}
return arrays;
}