选择排序,快排,冒排
//基本思想:选择排序(Selection-sort)是一种简单直观的排序算法。
//它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,
//然后,再从剩余未排序元素中继续寻找最小(大)元素,
//然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕
在待排序数据中,选出最小的一个数与第一个位置的数交换;然后在剩下的数中选出最小的数与第二个数交换;依次类推,直至循环到只剩下两个数进行比较为止。
[7, 6, 5, 3, 1, 9, 4]
------------------------
[1, 6, 5, 3, 7, 9, 4]
[1, 3, 5, 6, 7, 9, 4]
[1, 3, 4, 6, 7, 9, 5]
[1, 3, 4, 5, 7, 9, 6]
[1, 3, 4, 5, 6, 9, 7]
[1, 3, 4, 5, 6, 7, 9]
[1, 3, 4, 5, 6, 7, 9]
public static void main(String[] args) {
int[] arr = {7, 6, 5, 3, 1, 9, 4};
System.out.println(Arrays.toString(arr));
System.out.println("------------------------");
for (int i = 0; i < arr.length; i++) {
int minIndex = i;
for (int j = i; j < arr.length; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
System.out.println(Arrays.toString(arr));
}
}
堆排序
https://juejin.im/post/5d7c63f25188252139764923
交换排序
https://blog.csdn.net/NathanniuBee/article/details/83413879
public class BubbleSort {
public static void bubbleSort(Integer[] arr, int n) {
if (n <= 1) return; //如果只有一个元素就不用排序了
for (int i = 0; i < n; ++i) {
// 提前退出冒泡循环的标志位,即一次比较中没有交换任何元素,这个数组就已经是有序的了
boolean flag = false;
for (int j = 0; j < n - i - 1; ++j) { //此处你可能会疑问的j arr[j + 1]) { //即这两个相邻的数是逆序的,交换
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true;
}
}
System.out.println(Arrays.toString(arr));
if (!flag) break;//没有数据交换,数组已经有序,退出排序
}
}
public static void main(String[] args) {
Integer arr[] = {2, 4, 7, 6, 8, 5, 9};
System.out.println(Arrays.toString(arr));
bubbleSort(arr, arr.length);
}
快速排序
通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
1.设置 low=0, high=N-1。
2.选择一个基准元素赋值给temp,即temp=a[low]。
3.从high开始向前搜索,即由后开始向前搜索(high--),找到第一个小于temp的值,将a[high]和a[low]交换。
4.从low开始向前后搜索,即由前开始向后搜索(low++),找到第一个大于temp的值,将a[high]和a[low]交换。
5.重复第3步和第4步,直到 lowhigh ,3,4步中,若没找到符合条件的值,执行 high-- 或 low++ ,直到找到为止。进行交换时 low和high的位置不变。当lowhigh时循环结束。
基准点的选取:固定切分、随机切分、三取样切分。
快速排序是不稳定的
快速排序在序列元素很少时,效率比较低。因此,元素较少时,可选择使用插入排序。
public class QuickSort {
public static void quickSort(int[] arr,int low,int high){
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
//temp就是基准位
temp = arr[low];
while (i=arr[i]&&i
归并排序
分而治之(divide - conquer);每个递归过程涉及三个步骤
第一, 分解: 把待排序的 n 个元素的序列分解成两个子序列, 每个子序列包括 n/2 个元素.
第二, 治理: 对每个子序列分别调用归并排序MergeSort, 进行递归操作
第三, 合并: 合并两个排好序的子序列,生成排序结果.
[60, 7, 44, 27, 62, 8, 59, 19]
[60, 7, 44, 27, 62, 8, 59, 19]
[60, 7, 44, 27, 62, 8, 59, 19]
[7, 60, 44, 27, 62, 8, 59, 19]
[7, 60, 44, 27, 62, 8, 59, 19]
[7, 60, 44, 27, 62, 8, 59, 19]
[7, 60, 27, 44, 62, 8, 59, 19]
[7, 27, 44, 60, 62, 8, 59, 19]
[7, 27, 44, 60, 62, 8, 59, 19]
[7, 27, 44, 60, 62, 8, 59, 19]
[7, 27, 44, 60, 8, 62, 59, 19]
[7, 27, 44, 60, 8, 62, 59, 19]
[7, 27, 44, 60, 8, 62, 59, 19]
[7, 27, 44, 60, 8, 62, 19, 59]
[7, 27, 44, 60, 8, 19, 59, 62]
[7, 8, 19, 27, 44, 59, 60, 62]
Process finished with exit code 0
public class MergeSort {
public static int[] mergeSort(int[] a, int low, int high){
int mid = (low+high)/2;
if(low