排序算法
(1) 快速排序(quick sort)
public static void quickSort(int[] array, int left, int right) {
if (array == null || array.length == 0) return;
if (left > right) return;
int key = array[left];
int l = left;
int r = right;
while (l != r) {
while (array[r] >= key && l < r) {
r--;
}
while (array[l] <= key && l < r) {
l++;
}
if (l < r) {
int tmp = array[l];
array[l] = array[r];
array[r] = tmp;
}
}
array[left] = array[l];
array[l] = key;
quickSort1(array, left, l - 1);
quickSort1(array, l + 1, right);
}
}
(2) 冒泡排序
public static void bubbleSort(int[] array){
int i,j,tmp;
boolean flag;
for (i=0;i< array.length-1;i++){
for (j=0,flag=true;j<array.length-1-i;j++){
if (array[j]>array[j+1]){
tmp=array[j];
array[j]=array[j+1];
array[j+1]=tmp;
flag=false;
}
if (flag==true) continue;
}
}
}
(3) 選擇排序
public static void selectSort(int[] array) {
int i, j, min, tmp;
for (i = 0; i < array.length - 1; i++) {
for (min = i, j = i + 1; j < array.length; j++) {
if (array[min] > array[j]) {
min = j;
}
}
tmp = array[i];
array[i] = array[min];
array[min] = tmp;
}
}
(4) 插入排序
public static void insertSort(int[] array) {
int tmp, i, j;
for (i = 1; i < 10; i++) {
if (array[i] < array[i - 1]) {
tmp = array[i];
for (j = i - 1; j >= 0 && array[j] > tmp; j--) {
array[j + 1] = array[j];
}
array[j + 1] = tmp;
}
}
}
(5) 堆排序
public static void swap(int[] array, int node1, int node2) {
int tmp;
tmp = array[node1];
array[node1] = array[node2];
array[node2] = tmp;
}
public static void heapify(int[] tree, int n, int i) {
if (i >= n) {
return;
}
int c1 = i * 2 + 1;
int c2 = i * 2 + 2;
int max = i;
if (c1 < n && tree[c1] > tree[max]) {
max = c1;
}
if (c2 < n && tree[c2] > tree[max]) {
max = c2;
}
if (max != i) {
swap(tree, max, i);
heapify(tree, n, max);
}
}
public static void build_heap(int[] tree, int n) {
int last_node = n - 1;
int parent = (last_node - 1) / 2;
int i;
for (i = parent; i >= 0; i--) {
heapify(tree, n, i);
}
}
public static void heapSort(int[] tree) {
int n = tree.length;
build_heap(tree, n);
int i;
for (i = n - 1; i >= 0; i--) {
swap(tree, i, 0);
heapify(tree, i, 0);
}
}
(6) 歸并排序
講解視頻:https://www.bilibili.com/video/BV1ZC4y1872h?p=2&spm_id_from=pageDriver
public class MergeSortDemo {
public static void mergeSort(int[] array, int low, int high) {
if (low == high) {
return;
}
int mid = (low + high) >>> 1;
mergeSort(array, low, mid);
mergeSort(array, mid + 1, high);
merge(array, low, mid, high);//合并
}
private static void merge(int[] array, int low, int mid, int high) {
int s1 = low;
int s2 = mid + 1;
int[] tmp = new int[high - low + 1];//臨時數(shù)組
int i = 0; //臨時數(shù)組tmp的下標
while (s1 <= mid && s2 <= high) {//說明有數(shù)據(jù)
if (array[s1] < array[s2]) {
tmp[i++] = array[s1++];
} else {
tmp[i++] = array[s2++];
}
}
while (s1 <= mid) {
tmp[i++] = array[s1++];
}
while (s2 <= high) {
tmp[i++] = array[s2++];
}
System.out.println(tmp.length);//2 2 4 2 2 4
for (int j = 0; j < tmp.length; j++) {//復制數(shù)組婆咸,需注意起始下標加上low邮屁,否則每次都是從0號下標開始
array[j + low] = tmp[j];
}
}
public static void main(String[] args) {
int[] array = {3, 6, 1, 4, 8, 0, 2, 5};
mergeSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
}
}
復雜度一覽