目录
1、简单选择排序
表现最稳定的排序算法之一,因为无论什么数据进去都是O(n2)的时间复杂度,所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。理论上讲,选择排序可能也是平时排序一般人想到的最多的排序方法了吧。
选择排序(Selection-sort)是一种简单直观的排序算法。它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
2.1 算法描述
n个记录的直接选择排序可经过n-1趟直接选择排序得到有序结果。具体算法描述如下:
- 初始状态:无序区为R[1..n],有序区为空;
- 第i趟排序(i=1,2,3…n-1)开始时,当前有序区和无序区分别为R[1..i-1]和R(i..n)。该趟排序从当前无序区中-选出关键字最小的记录 R[k],将它与无序区的第1个记录R交换,使R[1..i]和R[i+1..n)分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区;
- n-1趟结束,数组有序化了。
2.2 动图演示
2.3 代码实现
/**
* 简单选择排序算法
*/
public class ChooseSort {
public static void sort(int[] nums){
if(nums == null || nums.length == 0)
return;
for(int i = 0; i < nums.length; ++i){
int mixIndex = i; //记录最小数字的先下标
for(int j = i; j < nums.length; ++j){ //找出最小数字的先下标
if(nums[j] < nums[mixIndex]){
mixIndex = j;
}
}
int temp = nums[i]; //交换数字
nums[i] = nums[mixIndex];
nums[mixIndex] = temp;
}
}
public static void main(String args[]){
int[] nums = new int[]{5,69,54,21,5,36,7,84};
ChooseSort.sort(nums);
for(int num : nums){
System.out.print(num + " ");
}
}
}
运行结果:
5 5 7 21 36 54 69 84
2.4 算法性能分析
算法 | 平均时间 | 最优时间 | 最坏时间 | 额外空间 | 稳定性 |
简单选择排序 | O(n2) | O(n2) | O(n2) | 1 | 不确定,一般认为不稳定(看实现方式) |
2.5 优化
可以考虑一趟循环中确定一个最大值和一个最小值,这样可以减少循环次数到 n/2 次。对上题优化代码如下:
/**
* 简单排序算法优化
*/
public class TwoChooseSort {
public static void sort(int[] nums){
if(nums == null || nums.length == 0){
return;
}
int minIndex;
int maxIndex;
for(int i = 0; i <= nums.length / 2 ; ++i){
minIndex = i; //记录最小数字下标
maxIndex = i; //记录最大数字下标
for(int j = i + 1; j < nums.length - i ; ++j){
if(nums[j] > nums[maxIndex]){
maxIndex = j; //如果大,标记
continue; //不会是最小,跳过
}
if(nums[j] < nums[minIndex]){
minIndex = j;
}
}
int tempMin = nums[i]; //交换位置
nums[i] = nums[minIndex];
nums[nums.length - i - 1] = nums[maxIndex];
nums[minIndex] = tempMin;
int tempMax = nums[nums.length - i - 1];
nums[maxIndex] = tempMax;
}
}
public static void main(String args[]){
int[] nums = new int[]{5,69,54,21,5,36,7,84};
TwoChooseSort.sort(nums);
for(int num : nums){
System.out.print(num + " ");
}
}
}
运行结果:
5 5 7 21 36 54 69 84
2、堆排序
堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父节点。
-------------------------------------堆结构解析------------------------------
2.1 算法描述
- 将初始待排序关键字序列(R1,R2….Rn)构建成大顶堆,此堆为初始的无序区;
- 将堆顶元素R[1]与最后一个元素R[n]交换,此时得到新的无序区(R1,R2,……Rn-1)和新的有序区(Rn),且满足R[1,2…n-1]<=R[n];
- 由于交换后新的堆顶R[1]可能违反堆的性质,因此需要对当前无序区(R1,R2,……Rn-1)调整为新堆,然后再次将R[1]与无序区最后一个元素交换,得到新的无序区(R1,R2….Rn-2)和新的有序区(Rn-1,Rn)。不断重复此过程直到有序区的元素个数为n-1,则整个排序过程完成。
2.2 算法动态解图
2.3 算法分析
算法 | 平均时间 | 最优时间 | 最坏时间 | 额外空间 | 稳定性 |
堆排序 | O(nlogn) | O(nlogn) | O(nlogn) | 1 | 不稳定 |
2.4 算法实现
package 十大排序算法;
/**
* @description: 堆排序
* @author: Mr.Wang
* @create: 2020-04-28 16:30
**/
public class HeapSort {
public static void main(String[] args) {
// int[] arr = {5, 1, 7, 3, 1, 6, 9, 4};
int[] arr = {16, 7, 3, 20, 17, 8};
heapSort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}
/**
* 创建堆,
* @param arr 待排序列
*/
private static void heapSort(int[] arr) {
//创建堆
for (int i = (arr.length - 1) / 2; i >= 0; i--) {
//从第一个非叶子结点从下至上,从右至左调整结构
adjustHeap(arr, i, arr.length);
}
//调整堆结构+交换堆顶元素与末尾元素
for (int i = arr.length - 1; i > 0; i--) {
//将堆顶元素与末尾元素进行交换
int temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
//重新对堆进行调整
adjustHeap(arr, 0, i);
}
}
/**
* 调整堆
* @param arr 待排序列
* @param parent 父节点
* @param length 待排序列尾元素索引
*/
private static void adjustHeap(int[] arr, int parent, int length) {
//将temp作为父节点
int temp = arr[parent];
//左孩子
int lChild = 2 * parent + 1;
while (lChild < length) {
//右孩子
int rChild = lChild + 1;
// 如果有右孩子结点,并且右孩子结点的值大于左孩子结点,则选取右孩子结点
if (rChild < length && arr[lChild] < arr[rChild]) {
lChild++;
}
// 如果父结点的值已经大于孩子结点的值,则直接结束
if (temp >= arr[lChild]) {
break;
}
// 把孩子结点的值赋给父结点
arr[parent] = arr[lChild];
//选取孩子结点的左孩子结点,继续向下筛选
parent = lChild;
lChild = 2 * lChild + 1;
}
arr[parent] = temp;
}
}
运行结果:
3 7 8 16 17 20
参考:
https://www.cnblogs.com/guoyaohua/p/8600214.html