一、排序(Sort)
- 基本概念:
排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。
平时的上下文中,如果提到排序,通常指的是排升序(非降序)。
通常意义上的排序,都是指的原地排序(in place sort)。 - 稳定性(重点):
两个相等的数据,如果经过排序后,排序算法能保证其相对位置不发生变化,则我们称该算法是具备稳定性的排序 算法。
二、常见排序
- 插入排序
a. 直接插入排序
每次选择无序区间的第一个元素,在有序区间内选择合适的位置插入
import java.util.Arrays;
public class InsertionSort {
public static void insertSort(int[] array) {
//直接插入排序
//时间复杂度:O(N^2) 空间复杂度:O(1)
//稳定性:稳定排序
//有序区间:[0, bound) 无序区间:[bound, size);
for (int bound = 1; bound < array.length; bound++) {
//处理 bound 对应的元素往前插入
int tmp = array[bound];
//需要从后往前,找到合适的位置进行插入
int cur = bound - 1;
for(; cur >= 0; cur--) {
// 如果 cur >= tmp, 则为不稳定排序
if(array[cur] > tmp) {
array[cur + 1] = array[cur];
}else {
break;
}
}
array[cur + 1] = tmp;
}
}
public static void main(String[] args) {
int[] array = {5, 7, 2, 9, 4, 3, 6, 8};
insertSort(array);
System.out.println(Arrays.toString(array));
}
}
b. 希尔排序
希尔排序法又称缩小增量法。
希尔排序法的基本思想是:先选定一个整数,把待排序文件中所有记录分成个组,所有距离相等的记录分在同一组内,并对每一组内的记录进行排序。重复上述分组和排序的工作。当到达 gap =1 时,所有记录在统一组内排好序。
希尔排序是对直接插入排序的优化。
当 gap > 1 时都是预排序,目的是让数组更接近于有序。
当 gap = 1 时,数组已经接近有序的了,这样排序就会很快。
这样整体而言,可以达到优化的效果。
import java.util.Arrays;
public class ShellSort {
//希尔排序:
//若合理选择 gap 序列,希尔排序的时间复杂度可以达到O(N^1.3) (最高效)
//实现 希尔排序 时,使用希尔序列 O(N^2):size/2, size/4, size/8 ... 1
//gap = 1 时,希尔排序代码 = 直接插入排序代码
public static void shellSort(int[] array) {
int gap = array.length / 2;
while(gap > 1) {
insertSortGap(array, gap);
gap = gap / 2;
}
//当 gap = 1 时,最终插排一次
insertSortGap(array, 1);
}
private static void insertSortGap(int[] array, int gap) {
//从每组下标为 1 的元素开始
for (int bound = gap; bound < array.length; bound++) {
int tmp = array[bound];
int cur = bound - gap;
// bound 位置中相邻的前一个元素下标
for (; cur >= 0 ; cur -= gap) {
//分组情况下,同组的相邻元素下标差 gap
// 把 cur 位置的元素搬到 cur +gap 位置
if (array[cur] > tmp) {
array[cur + gap] = array[cur];
} else {
break;
}
}
array[cur + gap] = tmp;
}
}
public static void main(String[] args) {
int[] array = {5, 7, 2, 9, 4, 3, 6, 8};
shellSort(array);
System.out.println(Arrays.toString(array));
}
}
- 选择排序
a. 选择排序
每一次从无序区间选出最大(或最小)的一个元素,存放在无序区间的最后(或最前),直到全部待排序的数据元 素排完 。
import java.util.Arrays;
public class SeclctShell {
//选择排序(循环找最小值放在前面(升序排序为例))
//时间复杂度:O(N^2) 空间复杂度:O(1)
//稳定性:不稳定排序
public static void seclctShell(int[] array) {
for (int bound = 0; bound < array.length; bound++) {
//此时数组区间为:
//已排序:[0, bound) 未排序:[bound, size)
for (int cur = bound; cur < array.length; cur++) {
if(array[cur] < array[bound]) {
//找最小值
swap(array, cur, bound);
}
}
}
}
public static void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
public static void main(String[] args) {
int[] array = {4, 7, 9, 1, 5, 6, 3, 8};
seclctShell(array);
System.out.println(Arrays.toString(array));
}
}
b. 堆排序
基本原理也是选择排序,只是不在使用遍历的方式查找无序区间的最大的数,而是通过堆来选择无序区间的最大的 数。
注意: 排升序要建大堆;排降序要建小堆。
import java.util.Arrays;
public class HeapSort {
//堆排序
//时间复杂度:O(NlogN) [比 O(N^2) 快] 空间复杂度:O(1)
//稳定性:不稳定排序
//排升序要建大堆;排降序要建小堆
public static void heapSort(int[] array) {
//1.先建立堆
creatHeap(array);
int heapSize = array.length;
for (int i = 0; i < array.length; i++) {
swap(array, 0, heapSize - 1);
//交换堆顶元素和最后一个元素
heapSize --;
//删除堆中最后一个元素
shiftDown(array, heapSize, 0);
//重新向下调整
}
}
private static void creatHeap(int[] array) {
for (int i = (array.length - 1) / 2; i >= 0; i--) {
shiftDown(array, array.length, i);
}
}
public static void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
private static void shiftDown(int[] array, int size, int index) {
int parent = index;
int child = 2 * parent + 1;
while (child < size) {
if (child + 1 < size && array[child + 1] > array[child]) {
child = child + 1;
}
if (array[child] > array[parent]) {
swap(array, child, parent);
}else {
break;
}
parent = child;
child = 2 * parent + 1;
}
}
public static void main(String[] args) {
int[] array = {4, 7, 9, 1, 5, 6, 3, 8, 2};
heapSort(array);
System.out.println(Arrays.toString(array));
}
}