时间复杂度
算法 |
最好 |
最坏 |
平均 |
冒泡 |
O(n) |
O(n2) |
O(n2) |
选择 |
O(n2) |
O(n2) |
O(n2) |
插入 |
O(n) |
O(n2) |
O(n2) |
快速 |
O(nlog2n) |
O(n2) |
O(nlog2n) |
归并 |
O(nlog2n) |
O(nlog2n) |
O(nlog2n) |
冒泡/选择/插入
package com.denk.test;
/**
* @author: denk
* desc:
* date: 2018/3/21
*/
public class Sort {
public static void main(String[] args) {
int[] arr = {37, -10, 99, 4, 7, 9, 10, 20, 30, 50, 66, 77, 2, 100, -100, 1};
// bubbleSort(arr);
// selectSort(arr);
insertSort(arr);
print(arr);
}
/**
* 插入排序
*
* @param arr
*/
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int j = i;
int key = arr[i];
while (j > 0 && key < arr[j - 1]) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = key;
}
}
/**
* 选择排序
*
* @param arr
*/
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
swap(arr, i, min);
}
}
/**
* 冒泡排序
*
* @param arr
*/
public static void bubbleSort(int[] arr) {
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]) {
swap(arr, j, j + 1);
}
}
}
}
/**
* 交换数组中的第i个元素和第j哥元素的值
*
* @param arr
* @param i
* @param j
*/
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/**
* 打印arr中所有元素的值
*
* @param arr
*/
public static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
快速
package com.denk.test;
/**
* @author: denk
* desc:
* date: 2018/3/21
*/
public class Sort {
public static void main(String[] args) {
int[] arr = {37, -10, 99, 4, 7, 9, 10, 20, 30, 50, 66, 77, 2, 100, -100, 1};
quickSort(arr, 0, arr.length - 1);
print(arr);
}
public static void quickSort(int[] arr, int low, int high) {
if (low >= high) return;
int posi = findPost(arr, low, high);
quickSort(arr, low, posi - 1);
quickSort(arr, posi + 1, high);
}
/**
* 找到分隔数及位置,分隔点左边所有小于分隔数,右边所有大于分隔数
*
* @param arr
* @param low
* @param high
* @return
*/
public static int findPost(int[] arr, int low, int high) {
int mid = (low + high) / 2;//可随机
swap(arr, mid, high);
int small = low - 1;
for (int i = low; i < high; i++) {
if (arr[i] < arr[high]) {
++small;
if (small != i) {
swap(arr, i, small);
}
}
}
swap(arr, ++small, high);
return small;
}
/**
* 交换数组中的第i个元素和第j哥元素的值
*
* @param arr
* @param i
* @param j
*/
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/**
* 打印arr中所有元素的值
*
* @param arr
*/
public static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
归并
package com.denk.test;
/**
* @author: denk
* desc:
* date: 2018/3/21
*/
public class Sort {
public static void main(String[] args) {
int[] arr = {37, -10, 99, 4, 7, 9, 10, 20, 30, 50, 66, 77, 2, 100, -100, 1};
int[] temp = new int[arr.length];
mergeSort(arr, 0, arr.length - 1, temp);
print(arr);
}
/**
* 归并排序,先将两边各自排好序,然后合并到一起
*
* @param arr
* @param low
* @param high
* @param temp
*/
public static void mergeSort(int[] arr, int low, int high, int[] temp) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid, temp);
mergeSort(arr, mid + 1, high, temp);
merge(arr, low, mid, high, temp);
}
}
/**
* 将排好序的两个子数组合并成一个有序的大数组
*
* @param arr
* @param left
* @param mid
* @param right
* @param temp
*/
public static void merge(int[] arr, int left, int mid, int right, int[] temp) {
int i = left;
int m = mid;
int j = mid + 1;
int n = right;
int k = 0;
while (i <= m && j <= n) {
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= m) {
temp[k++] = arr[i++];
}
while (j <= n) {
temp[k++] = arr[j++];
}
for (i = 0; i < k; i++) {
arr[left + i] = temp[i];
}
}
/**
* 打印arr中所有元素的值
*
* @param arr
*/
public static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}