暂做笔记,以后再详细整理。
一、插入排序
1、直接插入排序
public class InsertSort {
public static void insertSort(int[] nums) {
int size = nums.length;
for (int i = 1; i < size; i++) {
if (nums[i] < nums[i - 1]) {
int j = i;
int tmp = nums[j];
while (j > 0 && tmp < nums[j - 1]) {
nums[j] = nums[j - 1];
j--;
}
nums[j] = tmp;
}
}
}
public static void main(String[] args) {
int[] a = {1,0,63,2,58,42,6,31,12,13,2,13,0,15};
//InsertSort.insertSort(a);
ShellSort.shellSort(a);
System.out.println(Arrays.toString(a));
}
}
2、希尔排序
public class ShellSort {
public static void shellSort(int[] nums) {
int gap = nums.length / 2;
while (gap >= 1) {
InsertInShell(nums, gap);
gap /= 2;
}
}
private static void InsertInShell(int[] nums, int gap) {
int size = nums.length;
for (int i = gap; i < size; i++) {
if (nums[i] < nums[i - gap]) {
int j = i;
int tmp = nums[j];
while (j - gap >= 0 && tmp < nums[j - gap]) {
nums[j] = nums[j - gap];
j -= gap;
}
nums[j] = tmp;
}
}
}
public static void main(String[] args) {
int[] a = {1,0,63,2,58,42,6,31,12,13,2,13,0,15};
//InsertSort.insertSort(a);
ShellSort.shellSort(a);
System.out.println(Arrays.toString(a));
}
}
二、选择排序
1、简单选择
public class SimpleSelectSort {
public static void simpleSelectSort(int[] nums) {
int size = nums.length;
for (int i = 0; i < size; i++) {
int k = selectMin(nums, i, size);
if (k != i) {
int tmp = nums[i];
nums[i] = nums[k];
nums[k] = tmp;
}
}
}
private static int selectMin(int[] nums,int start, int end) {
int k = start;
for (int i = start + 1; i < end; i++) {
if (nums[k] > nums[i]) {
k = i;
}
}
return k;
}
public static void main(String[] args) {
int[] a = {1,0,63,2,58,42,6,31,12,13,2,13,0,15};
//SimpleSelectSort.simpleSelectSort(a);
SimpleSelectSort.selectSort2(a);
System.out.println(Arrays.toString(a));
}
}
双向简单选择
public class SimpleSelectSort {
public static void selectSort2(int[] nums) {
int min = 0, max = 0, tmp;
int size = nums.length;
for (int i = 0; i <= size/2; i++) {
max = i;
min = i;
for (int j = i; j < size - i; j++) {
if (nums[max] < nums[j]) {
max = j;
continue;
}
if (nums[min] > nums[j]) {
min = j;
}
}
//最大值在区间最左边,最小值在区间最右边,直接交换两个值就可以了。
if (max == i && min == size - i - 1) {
tmp = nums[min];
nums[min] = nums[max];
nums[max] = tmp;
} else if (max == i) {
//最大值在区间最左边,最小值在区间内。
// 这时候是最左、最右、最小三个值交换
tmp = nums[min];
nums[min] = nums[size - i - 1];
nums[size - i - 1] = nums[max];
nums[max] = tmp;
} else if (min == size - i - 1) {
//最小值再区间最右边,最大值在区间内。
// 这时候也是三个值交换
tmp = nums[max];
nums[max] = nums[i];
nums[i] = nums[min];
nums[min] = tmp;
} else {
//最大值、最小值均在区间内。这时候是四个值交换
tmp = nums[i];
nums[i] = nums[min];
nums[min] = tmp;
tmp = nums[size - i - 1];
nums[size - i - 1] = nums[max];
nums[max] = tmp;
}
}
}
public static void main(String[] args) {
int[] a = {1,0,63,2,58,42,6,31,12,13,2,13,0,15};
//SimpleSelectSort.simpleSelectSort(a);
SimpleSelectSort.selectSort2(a);
System.out.println(Arrays.toString(a));
}
}
2、堆排序
public class HeapSort {
public static void heapSort(int[] nums) {
int size = nums.length - 1;
for (int i = (size - 1) / 2; i >= 0; i--) {
adjustHeap(nums, size, i);
}
for (int i = size - 1; i > 0; i--) {
int tmp = nums[i];
nums[i] = nums[0];
nums[0] = tmp;
adjustHeap(nums, i, 0);
}
}
private static void adjustHeap(int[] nums, int size, int pos) {
int lchild = pos * 2 + 1;
int rchild = lchild + 1;
int position = pos;
if (lchild < size && nums[lchild] > nums[pos]) {
position = lchild;
}
if (rchild < size && nums[rchild] > nums[position]) {
position = rchild;
}
if (position != pos) {
int tmp = nums[pos];
nums[pos] = nums[position];
nums[position] = tmp;
adjustHeap(nums, size, position);
}
}
public static void main(String[] args) {
int[] a = {1,0,63,2,58,42,6,31,12,13,2,13,0,15};
HeapSort.heapSort(a);
System.out.println(Arrays.toString(a));
}
}
三、交换排序
1、冒泡排序(包括优化后的冒泡和双向冒泡)
public class BubbleSort {
public static void bubbleSort(int[] nums) {
int size = nums.length;
boolean exchange = true;
int band = size - 1;
while (exchange) {
exchange = false;
int k = 0;
for (int i = 0; i < band; i++) {
if (nums[i] > nums[i + 1]) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
k = i;
exchange = true;
}
}
band = k;
}
}
public static void doubleBubbleSort(int[] nums) {
int low = 0;
int high = nums.length - 1;
while (low < high) {
for (int i = low; i < high; i++) {
if (nums[i] > nums[i + 1]) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
}
}
high--;
for (int i = high; i > low; i--) {
if (nums[i] < nums[i - 1]) {
int tmp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = tmp;
}
}
low++;
}
}
public static void main(String[] args) {
int[] a = {1,0,63,2,58,42,6,31,12,13,2,13,0,15};
//bubbleSort(a);
doubleBubbleSort(a);
System.out.println(Arrays.toString(a));
}
}
2、快速排序
public class QuickSort {
public static void quickSort(int[] nums) {
sort(nums, 0, nums.length - 1);
}
private static void sort(int[] nums, int left, int right) {
if (left < right) {
int low = left;
int high = right;
int key = nums[left];
while (low < high) {
while (low < high && key <= nums[high]) {
high--;
}
nums[low] = nums[high];
while (low < high && key >= nums[low]) {
low++;
}
nums[high] = nums[low];
}
nums[low] = key;
sort(nums, left, low - 1);
sort(nums, low + 1, right);
}
}
public static void main(String[] args) {
int[] a = {1,0,63,2,58,42,6,31,12,13,2,13,0,15};
QuickSort.quickSort(a);
System.out.println(Arrays.toString(a));
}
}
四、归并排序
public class MergeSort {
public static void mergeSort(int[] nums, int first, int end, int[] tmp) {
if (first < end) {
int mid = (first + end) / 2;
mergeSort(nums, first, mid, tmp);
mergeSort(nums, mid + 1, end, tmp);
merge(nums, first, mid, end, tmp);
}
}
public static void merge(int[] nums, int first, int mid, int end, int[] tmp) {
int i = first, m = mid, j = mid + 1, n = end;
int k = 0;
while (i <= m && j <= n) {
if (nums[i] <= nums[j]) {
tmp[k++] = nums[i++];
} else {
tmp[k++] = nums[j++];
}
}
while (i <= m) {
tmp[k++] = nums[i++];
}
while (j <= n) {
tmp[k++] = nums[j++];
}
for (int p = 0; p < k; p++) {
nums[first + p] = tmp[p];
}
}
public static void main(String[] args) {
int[] a = {1,0,63,2,58,42,6,31,12,13,2,13,0,15};
MergeSort.mergeSort(a, 0, a.length - 1, new int[a.length]);
System.out.println(Arrays.toString(a));
}
}
五、拓扑排序
public class Topological {
public boolean canFinish(int numCourses, int[][] prerequisites) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int i = 0; i < numCourses; i++) {
graph.put(i, new ArrayList());
}
for (int[] prerequisite : prerequisites) {
graph.get(prerequisite[1]).add(prerequisite[0]);
}
int[] preNum = new int[numCourses];
for (int i = 0; i < numCourses; i++) {
for (Integer integer : graph.get(i)) {
preNum[integer]++;
}
}
for (int i = 0; i < numCourses; i++) {
int j;
for (j = 0; j < numCourses; j++) {
if (preNum[j] == 0) {
break;
}
}
if (j == numCourses) {
return false;
}
preNum[j] = -1;
for (Integer integer : graph.get(j)) {
preNum[integer]--;
}
}
return true;
}
}