当前位置:网站首页>排序第二节——选择排序(选择排序+堆排序)(两个视频讲解)
排序第二节——选择排序(选择排序+堆排序)(两个视频讲解)
2022-08-09 06:50:00 【学习追求高效率】
2.2 选择排序
2.2.1基本思想:
每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完 。
2.2.2 直接选择排序 O(N^2) (不稳定)
***(视频讲解 直接选择排序+优化 14分钟)
直接选择排序+优化
- 在元素集合array[i]–array[n-1]中选择关键码最大(小)的数据元素
- 若它不是这组元素中的最后一个(第一个)元素,则将它与这组元素中的最后一个(第一个)元素交换
- 在剩余的array[i]–array[n-2](array[i+1]–array[n-1])集合中,重复上述步骤,直到集合剩余1个元素
【直接选择排序的特性总结】
- 直接选择排序思考非常好理解,但是效率不是很好。实际中很少使用
- 时间复杂度:O(N^2)
- 空间复杂度:O(1)
- 稳定性:不稳定
模板(选择排序,一个循环找一个)
/** * 直接选择排序 * 时间复杂度:O(n^2) -> 对数据不敏感 不管你是有序的还是无序的 时间复杂度都是这样的 * 空间复杂度:O(1) * 稳定性:不稳定的排序 * @param array */
public static void selectSort(int[] array) {
for (int i = 0; i < array.length; i++) {
//每次将i下标的 值 当做是 最小值
int minIndex = i;
for (int j = i+1; j < array.length; j++) {
if(array[j] < array[minIndex]) {
minIndex = j;
}
}
swap(array,minIndex,i);
}
}
/** * 交换函数 * @param array * @param i * @param j */
private static void swap(int[] array,int i,int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
*优化:一次找两个极值
/** * O(N^2) * @param array */
public static void selectSort2(int[] array) {
int left = 0;
int right = array.length-1;
while (left < right) {
int minIndex = left;
int maxIndex = left;
for (int i = left+1; i <= right ; i++) {
if(array[i] < array[minIndex]) {
minIndex = i;
}
if(array[i] > array[maxIndex]) {
maxIndex = i;
}
}
swap(array,left,minIndex);
//修正 max的下标
if(left == maxIndex) {
maxIndex = minIndex;
}
swap(array,right,maxIndex);
left++;
right--;
}
}
2.2.3 堆排序**O(NlogN) (不稳定)
视频讲解
堆排序
堆排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种。它是通过堆来进行选择数据。需要注意的是排升序要建大堆,排降序建小堆。
【直接选择排序的特性总结】
- 堆排序使用堆来选数,效率就高了很多。
- 时间复杂度:O(N*logN)
- 空间复杂度:O(1)
- 稳定性:不稳定
模板
思想:
- 建立一个大根堆(堆顶一定是 最大的元素)
- 堆顶和堆低互换,然后再重新根据 堆顶建堆(循环)
/** * 时间复杂度:O(n*logn) * N^0.5 logn * 空间复杂度:O(1) * 稳定性:不稳定的 * @param array */
public static void heapSort(int[] array) {
createBigHeap(array);
int end = array.length-1;
while (end >= 0) {
swap(array,0,end);
shiftDown(array,0,end);
end--;
}
}
private static void createBigHeap(int[] array) {
for (int parent = (array.length-1-1)/2; parent >= 0 ; parent--) {
shiftDown(array,parent,array.length);
}
}
private static void shiftDown(int[] array,int parent,int len) {
int child = 2*parent+1;
//最起码保证有左孩子
while (child < len) {
if(child+1 < len && array[child] < array[child+1]) {
child++;
}
if(array[child] > array[parent]) {
swap(array,child,parent);
parent = child;
child = 2*parent+1;
}else {
break;
}
}
}
边栏推荐
- failed (13: Permission denied) while connecting to upstream
- 网络学习总结
- Flask failed to create database without error
- Output method of list string print(*a) print(““.join(str(c) for c in a) )
- Silently start over, the first page is also a new page
- Singleton DCL (double check the lock) full han mode and the hungry
- Service
- DDD 领域驱动设计
- TCP段重组PDU
- 常用Oracle命令
猜你喜欢

ByteDance Written Exam 2020 (Douyin E-commerce)

Distributed id generator implementation

leetcode 之 零移位

图论,二叉树,dfs,bfs,dp,最短路专题

idea中PlantUML插件使用

Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)

【Oracle 11g】Redhat 6.5 安装 Oracle11g

买口罩(0-1背包)

当酷雷曼VR直播遇上视频号,会摩擦出怎样的火花?

代码目录结构
随机推荐
如何 认识与学习BASH
【Docker】Docker安装MySQL
Mysql实操
高项 04 项目整体管理
Flask failed to create database without error
细谈VR全景:数字营销时代的宠儿
Explain the wait() function and waitpid() function in C language in detail
Use baidu EasyDL intelligent bin
Error: flask: TypeError: 'function' object is not iterable
Built-in macros in C language (define log macros)
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
变压器的工作原理(图解,原理图讲解,一看就懂)
Thread Pool Summary
The AD in the library of library file suffix. Intlib. Schlib. Pcblib difference
【ROS2原理8】节点到参与者的重映射
如何操作数据库
APP商品详情源数据接口(淘宝/京东/拼多多/苏宁/抖音等平台详情数据分析接口)代码对接教程
虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection
way of thinking problem-solving skills
leetcode:55. 跳跃游戏

