测试用例
public static void main(String[] args) {
int[] ints = {13, 432, 4553, 322, 442, 5, 5, 0, 32,};
selectionSort(ints, ints.length);
System.out.println(Arrays.toString(ints));
}
1. 冒泡排序算法
public static void BubbleSort(int[] A, int N) {
int temp = 0;
boolean flag;
for (int j = N - 1; j > 0; j--) {
flag = true;
for (int i = 0; i < j; i++) {
if (A[i] > A[i + 1]) {
temp = A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
flag = false;
}
}
if (flag) {
break;
}
}
}
2. 插入排序
public static void InsertSort(int[] A, int N) {
int tmp, j;
for (int i = 1; i < N; i++) {
tmp = A[i];
for (j = i; j > 0 && A[j - 1] > tmp; j--) {
A[j] = A[j - 1];
}
A[j] = tmp;
}
}
3. 选择排序
public static void selectionSort(int[] A, int N) {
int minPosition;
for (int i = 0; i < N; i++) {
minPosition = scanForMin(A, i, N - 1);
swap(A, minPosition, i);
}
}
private static void swap(int[] a, int minPosition, int i) {
int tmp = a[i];
a[i] = a[minPosition];
a[minPosition] = tmp;
}
private static int scanForMin(int[] a, int i, int N) {
int minPosition = i;
for (int j = i + 1; j <= N; j++) {
if (a[j] < a[minPosition]) {
minPosition = j;
}
}
return minPosition;
}
4. 希尔排序
public static void shellSort(int[] A, int N) {
int d;
for (d = N / 2; d > 0; d /= 2) {
int tmp, j;
for (int i = d; i < N; i++) {
tmp = A[i];
for (j = i; j >= d && A[j - d] > tmp; j -= d)
A[j] = A[j - d];
A[j] = tmp;
}
}
}
5. 堆排序
5.1 方案一:
public static void heapSort(int[] A, int N) throws Exception {
int tempA[] = new int[N];
MinHeap minHeap = buildMinHeap(A, N);
for (int i = 0; i < N; i++) {
tempA[i] = deleteMin(minHeap);
}
for (int i = 0; i < N; i++) {
A[i] = tempA[i];
}
}
5.2 方案二:
public static void heapSort2(int[] A, int N) throws Exception {
for (int i = N / 2; i >= 0; i
percDownMax(A, N - 1, i, 0, 0, 0);
}
for (int i = N - 1; i > 0; i
swap(A, 0, i);
percDownMax(A, i - 1, 0, 0, 0, 0);
}
}
6.堆排序
private static int deleteMin(MinHeap minHeap) throws Exception {
if (minHeap.size < 0) {
throw new Exception("没了呀");
}
int min = minHeap.elements[0], tmp = 0, parent = 0, child = 0;
swap(minHeap.elements, 0, minHeap.size--);
percDownMin(minHeap.elements, minHeap.size, parent, parent, child, tmp);
return min;
}
private static MinHeap buildMinHeap(int[] a, int N) {
for (int i = N / 2; i >= 0; i--) {
percDownMin(a, N - 1, i, 0, 0, 0);
}
MinHeap minHeap = new MinHeap();
minHeap.elements = a;
minHeap.capacity = N;
minHeap.size = N - 1;
return minHeap;
}
private static void percDownMin(int[] a, int n, int beginIndex, int parent, int child, int tmp) {
tmp = a[beginIndex];
for (parent = beginIndex; parent < n && (2 * parent + 1) <= n; parent = child) {
child = 2 * parent + 1;
if ((child + 1) <= n && a[child] > a[child + 1])
child++;
if (a[child] < tmp)
a[parent] = a[child];
else break;
}
a[parent] = tmp;
}
private static void percDownMax(int[] a, int n, int beginIndex, int parent, int child, int tmp) {
tmp = a[beginIndex];
for (parent = beginIndex; parent < n && (2 * parent + 1) <= n; parent = child) {
child = 2 * parent + 1;
if ((child + 1) <= n && a[child] < a[child + 1])
child++;
if (a[child] > tmp)
a[parent] = a[child];
else break;
}
a[parent] = tmp;
}
private static void swap(int[] a, int minPosition, int i) {
int tmp = a[i];
a[i] = a[minPosition];
a[minPosition] = tmp;
}
static class MinHeap {
private int[] elements;
private int size;
private int capacity;
}
7.归并排序
7.1.方式一:按照递归的方式
private void merge(int[] A, int[] tmpA, int l, int r, int rightEnd) {
int leftEnd = r - 1;
int tmp = l;
int numElements = rightEnd - l - 1;
while (l <= leftEnd && r <= rightEnd) {
if (A[l] <= A[r]) tmpA[tmp++] = A[l++];
else tmpA[tmp++] = A[r++];
}
while (l <= leftEnd) {
tmpA[tmp++] = A[l++];
}
while (r <= rightEnd) {
tmpA[tmp++] = A[r++];
}
for (int i = 0; i < numElements; i++, rightEnd--) {
A[rightEnd] = tmpA[rightEnd];
}
}
private void mSort(int[] A, int[] tmpA, int l, int rightEnd) {
int center;
if (l < rightEnd) {
center = (rightEnd + l) / 2;
mSort(A, tmpA, l, center);
mSort(A, tmpA, center + 1, rightEnd);
merge(A, tmpA, l, center + 1, rightEnd);
}
}
public void mergeSort(int[] A, int n) {
int[] tmpA = new int[n];
mSort(A, tmpA, 0, n - 1);
}
7.2.方式二 非递归使用循环的方式
private void mergePass(int A[], int tmpA[], int n, int length) {
int i = 0;
for (; i < n - 2 * length; i += length) {
merge1(A, tmpA, i, i + length, i + 2 * length - 1);
}
if (i + length < n) {
merge1(A, tmpA, i, i + length, n - 1);
} else
for (int j = i; j < n; j++) tmpA[j] = A[j];
}
private void merge1(int[] A, int[] tmpA, int l, int r, int rightEnd) {
int leftEnd = r - 1;
int tmp = l;
int numElements = rightEnd - l - 1;
while (l <= leftEnd && r <= rightEnd) {
if (A[l] <= A[r]) tmpA[tmp++] = A[l++];
else tmpA[tmp++] = A[r++];
}
while (l <= leftEnd) {
tmpA[tmp++] = A[l++];
}
while (r <= rightEnd) {
tmpA[tmp++] = A[r++];
}
}
public void mergeSort1(int A[], int n) {
int length = 1;
int tmpA[] = new int[n];
while (length < n) {
mergePass(A, tmpA, n, length);
length *= 2;
mergePass(tmpA, A, n, length);
length *= 2;
}
}