1. 交换排序
import java.util.Arrays;
import java.util.Random;
public class Exchange {
public static void bubbleSort(int[] arr) {
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 tmp = arr[j];arr[j] = arr[j+1];arr[j+1] = tmp;
}
}
public static void quickSort(int[] arr, int left, int right) {
if(left < right) {
int privotLoc = partition(arr, left, right);
quickSort(arr, left, privotLoc);
quickSort(arr, privotLoc + 1, right);
}
}
private static int partition(int[] arr, int left, int right) {
int privotKey = arr[left];
while(left < right) {
while(left < right && arr[right] >= privotKey) --right;
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
while(left < right && arr[left] < privotKey) ++left;
tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
return left;
}
public static void main(String[] args) {
int[] arr = new Random().ints(12, 1, 30).toArray();
System.out.println(Arrays.toString(arr));
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
}
2. 插入排序
public class Insert {
public static void straightInsertion(int[] arr) {
for(int i = 1;i < arr.length;i++) {
if(arr[i] < arr[i - 1]) {
int index = i - 1;
int x = arr[i];
arr[i] = arr[i - 1];
while(index >= 0) {
if(x < arr[index])
arr[index + 1] = arr[index];
else
break;
index--;
}
arr[index + 1] = x;
}
System.out.println(i + ": " + Arrays.toString(arr));
}
}
public static void shellInsertion(int[] arr) {
int dis = arr.length/2;
while(dis >= 1) {
shellInsertion(arr, dis);
System.out.println(dis + ": " + Arrays.toString(arr));
dis--;
}
}
private static void shellInsertion(int[] arr, int dis) {
for(int i = dis;i < arr.length;i++) {
if(arr[i] < arr[i - dis]) {
int index = i - dis;
int x = arr[i];
while(index >= 0) {
if(x < arr[index])
arr[index + dis] = arr[index];
else
break;
index -= dis;
}
arr[index + dis] = x;
}
}
}
public static void main(String[] args) {
int[] arr = new int[]{49,38,65,97,76,13,27};
System.out.println(Arrays.toString(arr));
shellInsertion(arr);
System.out.println(Arrays.toString(arr));
}
}
3. 归并排序
import java.util.Arrays;
import java.util.Random;
public class Merge {
public static void mergeSort(int[] arr, int low, int high) {
int mid = (low + high)/2;
if(low < high) {
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
private static void merge(int[] arr, int low, int mid, int high) {
int[] tmp = new int[high - low + 1];
int i = low;
int j = mid + 1;
int k = 0;
while(i <= mid && j <= high) {
if(arr[i] < arr[j])
tmp[k++] = arr[i++];
else
tmp[k++] = arr[j++];
}
while(i<=mid) tmp[k++] = arr[i++];
while(j<=high) tmp[k++] = arr[j++];
for(int x = 0;x < tmp.length;x++)
arr[x + low] = tmp[x];
}
public static void main(String[] args) {
int[] arr = new Random().ints(12, 1, 30).toArray();
System.out.println(Arrays.toString(arr));
mergeSort(arr, 0, arr.length);
System.out.println(Arrays.toString(arr));
}
}
4. 选择排序
import java.util.Arrays;
public class Select {
public static void simpleSelection(int[] arr) {
int index = -1;
for(int i = 0;i<arr.length;i++) {
index = minIndex(arr, i);
if(index != i) {
int tmp = arr[index];
arr[index] = arr[i];
arr[i] = tmp;
}
System.out.println(i + 1 + ":" + Arrays.toString(arr));
}
}
private static int minIndex(int[] arr, int start) {
int res = start;
for(int i = start + 1;i < arr.length;i++)
if(arr[i] < arr[res])
res = i;
return res;
}
public static void heapSelection(int[] arr) {
initHeap(arr);
for(int i = arr.length - 1;i>=0;i--) {
int temp = arr[i]; arr[i] = arr[0]; arr[0] = temp;
adjustHeap(arr, 0, i);
}
}
private static void initHeap(int[] heap) {
for(int i = (heap.length - 1)/2;i >= 0;i--)
adjustHeap(heap, i, heap.length);
}
private static void adjustHeap(int[] heap, int i, int length) {
int tmp = heap[i];
int child = 2 * i + 1;
while(child < length) {
if(child + 1 < length && heap[child] < heap[child + 1])
++child;
if(heap[i] < heap[child]) {
heap[i] = heap[child];
i = child;
child = 2 * i + 1;
} else
break;
heap[i] = tmp;
}
}
public static void main(String[] args) {
int[] arr = new int[]{49,38,65,97,76,13,27};
System.out.println(Arrays.toString(arr));
heapSelection(arr);
System.out.println(Arrays.toString(arr));
}
}
5. 桶排序(LSD)
import java.util.Arrays;
import java.util.Random;
public class Radix {
/**
* 这是《算法》里的LSD
* @param a
*/
public static void radixSort(int[] a) {
final int BITS = 32;
final int R = 1 << 8;
final int MASK = R - 1;
final int w = BITS/8;
int n = a.length;
int[] aux = new int[n];
for(int d = 0;d < w;d++) {
int[] count = new int[R + 1];
for(int i = 0;i < n;i++) {
int c = (a[i] >> 8*d) & MASK;
count[c + 1]++;
}
for (int r = 0; r < R; r++)
count[r+1] += count[r];
System.out.println("cumu:" + Arrays.toString(count));
if (d == w-1) {
int shift1 = count[R] - count[R/2];
int shift2 = count[R/2];
for (int r = 0; r < R/2; r++)
count[r] += shift1;
for (int r = R/2; r < R; r++)
count[r] -= shift2;
}
System.out.println("sign:" + Arrays.toString(count));
for (int i = 0; i < n; i++) {
int c = (a[i] >> 8*d) & MASK;
aux[count[c]++] = a[i];
}
System.out.println("move:" + Arrays.toString(aux));
for (int i = 0; i < n; i++)
a[i] = aux[i];
}
}
public static void main(String[] args) {
int[] arr = new Random().ints(10, 1, 20).toArray();
System.out.println(Arrays.toString(arr));
radixSort(arr);
System.out.println(Arrays.toString(arr));
}
}