场景:
快速: 大量 无序
归并:大数据 外排序
是否基本有序: 有 - > 归并 无 ->快速
时间复杂度计算:拜托,面试别再问我时间复杂度了!!!_架构师之路_的博客-CSDN博客
快速排序(NlogN)
- 适合场景:无序,对稳定性没有要求时
- 特点:速度快,但是不稳定
- 核心思想
- 找到基准数,第一次以第一个元素为基准
- 从后往前比,比基准数小的,移到数组头部
- 从前往后比,比基准数大的,移到数组尾部
- 把基准数放入中间位置
-
public class QuickSort { public static void main(String[] args) { int[] nums = {4,2,4,2,5,7,3,9,4,5}; quick(nums); for (int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } } /** * 快速排序 * @param numbers 带排序数组 */ public static void quick(int[] numbers) { if(numbers.length > 0) //查看数组是否为空 { quickSort(numbers, 0, numbers.length-1); } } /** * @param numbers 带排序数组 * @param low 开始位置 * @param high 结束位置 */ public static void quickSort(int[] numbers, int low, int high) { if (low < high) { int middle = getMiddle(numbers, low, high); //将numbers数组进行一分为二 quickSort(numbers, low, middle - 1); //对低字段表进行递归排序 quickSort(numbers, middle + 1, high); //对高字段表进行递归排序 } } /** * 查找出中轴(默认是最低位low)的在numbers数组排序后所在位置 * * @param numbers 带查找数组 * @param low 开始位置 * @param high 结束位置 * @return 中轴所在位置 */ public static int getMiddle(int[] numbers, int low, int high) { int temp = numbers[low]; //数组的第一个作为中轴 while (low < high) { while (low < high && numbers[high] >= temp) { high--; } numbers[low] = numbers[high];//比中轴小的记录移到低端 while (low < high && numbers[low] <= temp) { low++; } numbers[high] = numbers[low]; //比中轴大的记录移到高端 } numbers[low] = temp; //中轴记录到尾 return low; // 返回中轴的位置 } }
归并排序(外排序)(NlogN)
- 适用场景:对多个有序数组合并为一个有序数组
- 特点:速度快且稳定,但是需要的辅助空间大
- 核心思想:
- 将大数组拆分成多个小数组,直到数组只有1个元素
- 对左右数组分别递归排序
- 将2个排好序的数组整合为大的有序数组
-
public class MergeSort { public static void main(String[] args) { int[] a = {2,4,5,6,2,3,5,8,0,1,3}; int[] sort = sort(a, 0, a.length); for (int i = 0; i < sort.length; i++) { System.out.println(sort[i]); } } /** * 归并排序 * 简介:将两个(或两个以上)有序表合并成一个新的有序表 即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列 * 时间复杂度为O(nlogn) * 稳定排序方式 * @param nums 待排序数组 * @return 输出有序数组 */ public static int[] sort(int[] nums, int low, int high) { int mid = (low + high) / 2; if (low < high) { // 左边 sort(nums, low, mid); // 右边 sort(nums, mid + 1, high); // 左右归并 merge(nums, low, mid, high); } return nums; } /** * 将数组中low到high位置的数进行排序 * @param nums 待排序数组 * @param low 待排的开始位置 * @param mid 待排中间位置 * @param high 待排结束位置 */ public static void merge(int[] nums, int low, int mid, int high) { int[] temp = new int[high - low + 1]; int i = low;// 左指针 int j = mid + 1;// 右指针 int k = 0; // 把较小的数先移到新数组中 while (i <= mid && j <= high) { if (nums[i] < nums[j]) { temp[k++] = nums[i++]; } else { temp[k++] = nums[j++]; } } // 把左边剩余的数移入数组 while (i <= mid) { temp[k++] = nums[i++]; } // 把右边边剩余的数移入数组 while (j <= high) { temp[k++] = nums[j++]; } // 把新数组中的数覆盖nums数组 for (int k2 = 0; k2 < temp.length; k2++) { nums[k2 + low] = temp[k2]; } } }
选择排序(N方)
- 核心思想:
- 找最小值
- 放在左边(交换位置)
- 不断迭代
插入排序
适合场景: 有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序。
核心思想:
- 每步将一个待排序的记录,按其值的大小插入前面已经排序的文件中适当位置上
- 不断迭代
- 插入排序在大量数据有序的情况下,时间复杂度为0(N),和未排序的键值对有关
Shell排序
它是在插入排序基础上的优化,适合大量的无序的数据排序
思想:
- 先转换为部分有序(通过跳跃性逻辑分组【比如1,5,9,13】),
- 将每个小组进行排序
- 直到增量=1变为插入排序