常见的排序算法
Posted 保护眼睛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常见的排序算法相关的知识,希望对你有一定的参考价值。
上篇👇
二叉树
冒泡排序
public static void bubbleSort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
}
选择排序
public static void selectSort(int array[]) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}
插入排序
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int tmp = array[i];
int j = i - 1;
for (; j >= 0; j--) {
if (array[j] > tmp) {
array[j + 1] = array[j];
} else {
break;
}
}
array[j + 1] = tmp;
}
}
shell排序
public static void shellSort(int[] array) {
int[] tmp = {5, 3, 1};
for (int i = 0; i < tmp.length; i++) {
shell(array, tmp[i]);
}
}
public static void shell(int array[], int gap) {
for (int i = gap; i < array.length; i++) {
int tmp = array[i];
int j = i - gap;
for (; j >= 0; j-=gap) {
if (array[j] > tmp) {
array[j + gap] = array[j];
} else {
break;
}
}
array[j + gap] = tmp;
}
}
堆排序
public static void heapSort(int[] array) {
creteHeap(array);
int end = array.length - 1;
while (end > 0) {
int tmp = array[0];
array[0] = array[end];
array[end] = tmp;
adjustDown(array, 0, end);
end--;
}
}
public static void creteHeap(int[] array) {
for (int p = (array.length - 1 - 1) / 2; p >= 0; p--) {
adjustDown(array, p, array.length);
}
}
public static void adjustDown(int[] array, int parent, int len) {
int child = parent * 2 + 1;
while (child < len) {
if (child + 1 < len && array[child + 1] > array[child]) {
child++;
}
if (array[child] > array[parent]) {
int tmp = array[child];
array[child] = array[parent];
array[parent] = tmp;
parent = child;
child = parent * 2 + 1;
} else {
break;
}
}
}
快速排序
public static void quickSort(int[] array) {
quick(array, 0, array.length - 1);
}
public static void quick(int[] array, int low, int high) {
if (low >= high) {
return;
}
int par = partition(array, low, high);
quick(array, low, par - 1);
quick(array, par + 1, high);
}
public static int partition(int[] array, int start, int end) {
int tmp = array[start];
while (start < end) {
while (start < end && array[end] >= tmp) {
end--;
}
if (start >= end) {
array[start] = tmp;
break;
} else {
array[start] = array[end];
}
while (start < end && array[start] <= tmp) {
start++;
}
if (start >= end) {
array[start] = tmp;
break;
} else {
array[end] = array[start];
}
}
return start;
}
快速排序非递归实现
public static void quickSortNotRec(int[] array) {
quick(array, 0, array.length - 1);
}
public static void quickNotRec(int[] array, int start, int end) {
start = 0;
end = array.length - 1;
int par = partition(array, start, end);
Stack<Integer> stack = new Stack<>();
if (start < par - 1) {
stack.push(start);
stack.push(par - 1);
}
if (end > par + 1) {
stack.push(par + 1);
stack.push(end);
}
while (!stack.isEmpty()) {
end = stack.pop();
start = stack.pop();
par = partition(array, start, end);
if (start < par - 1) {
stack.push(start);
stack.push(par - 1);
}
if (end > par + 1) {
stack.push(par + 1);
stack.push(end);
}
}
}
归并排序
public static void mergerSort(int[] array) {
merger(array, 0, array.length - 1);
}
public static void merger(int[] array, int low, int high) {
if (low >= high) return;
int mid = (high + low) >>> 1;
merger(array, low, mid);
merger(array, mid + 1, high);
mergerRec(array, low, mid, high);
}
public static void mergerRec(int[] array, int start, int mid, int end) {
int[] tmp = new int[end - start + 1];
int k = 0;
int s1 = start;
int e1 = mid;
int s2 = mid + 1;
int e2 = end;
while (s1 <= e1 && s2 <= e2) {
if (array[s1] < array[s2]) {
tmp[k++] = array[s1++];
} else {
tmp[k++] = array[s2++];
}
}
while (s1 <= e1) {
tmp[k++] = array[s1++];
}
while (s2 <= e2) {
tmp[k++] = array[s2++];
}
for (int i = 0; i < tmp.length; i++) {
array[i + start] = tmp[i];
}
}
非递归实现归并排序
public static void mergerSortNotRec(int[] array) {
for (int gap = 1; gap < array.length; gap *= 2) {
mergerNotRec(array, gap);
}
}
public static void mergerNotRec(int array[], int gap) {
int tmp[] = new int[array.length];
int s1 = 0;
int k = 0;
int e1 = s1 + gap - 1;
int s2 = e1 + 1;
int e2 = s2 + gap - 1 > array.length ? array.length - 1 : s2 + gap - 1;
while (s2 < array.length) {
while (s1 <= e1 && s2 <= e2) {
if (array[s1] <= array[s2]) {
tmp[k++] = array[s1++];
} else {
tmp[k++] = array[s2++];
}
}
while (s1 <= e1) {
tmp[k++] = array[s1++];
}
while (s2 <= e2) {
tmp[k++] = array[s2++];
}
s1 = e2 + 1;
e1 = s1 + gap - 1;
s2 = e1 + 1;
e2 = s2 + gap - 1 > array.length ? array.length - 1 : s2 + gap - 1;
}
while (s1 < array.length) {
tmp[k++] = array[s1++];
}
for (int i = 0; i < tmp.length; i++) {
array[i] = tmp[i];
}
}
欢迎指正,相互关注啊😄
以上是关于常见的排序算法的主要内容,如果未能解决你的问题,请参考以下文章