当前位置:网站首页>七大排序之直接选择排序
七大排序之直接选择排序
2022-07-30 07:52:00 【威威沁沁】
目录
时间复杂度:
空间复杂度:
稳定性: 不稳定
1、基本思想
直接选择排序是选择排序的一种,其思想就是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。
2、代码讲解和实现
跟插入排序一样,直接选择排序也是双循环,不过有区别的是选择排序外循环和内循环都是从前往后遍历数组。
假设以遍历 i,和j 分别代表外内循环的变量,i 从 0 小表循环到 (array.length-2) 下标,i 每变化一次,j 就从 i+1下标 循环到 (array.length-1)下标。
创建一个变量 minIndex 记录 每次循环中的最小值的下标,外循环每循环一次,就把 i 赋值给 minIndex。然后内循环找 i 下标之后的最小值,并把最小值的下标 赋值该 minIndex。
最后交换 i下标 和 minIndex下标的值。一直重复上述操作,直到外循环结束。

public static void selectSort(int[] array){
for(int i =0 ; i< array.length-1;i++){
//每次将 i 下标的 值 当做是 最小值
int minIndex = i;
for (int j = i+1; j < array.length; j++) {
if(array[j] < array[minIndex]){
minIndex = j;
}
}
swap(array,i,minIndex);
}
}
/**
* 交换函数
* @param array
* @param x
* @param y
*/
private static void swap(int[] array,int x,int y){
int tmp = array[x];
array[x] = array[y];
array[y] = tmp;
}3、特性总结
1. 直接选择排序思考非常好理解,但是效率不是很好。实际很少使用
2. 直接选择排序对数据不敏感,不管是有序还是无序,时间复杂度都是这样 为
3. 直接选择排序是内部排序,空间复杂度为
4. 直接选择排序是一个不稳定的排序
边栏推荐
- typescript3-ts对比js的差别
- SQL的substring_index()用法——MySQL字符串截取
- 【科普向】5G核心网架构和关键技术
- 联想笔记本 如何更改Win10系统开机logo图标
- Why does typescript2-typescript add type support to js
- Detailed explanation of 4D words: C language three-point chess advanced + N-piece chess recursive dynamic judgment of winning or losing
- js柯里化
- typescript6 - simplify the steps to run ts
- 39.【vector动态数组定义及初始化】
- JS中如何阻止事件冒泡和默认行为
猜你喜欢

leetcode经典问题——11.盛水最多的容器

【Kotlin 中的类和继承】

The difference between typescript3-ts and js

OA项目之待开会议&历史会议&所有会议

数据分发服务 (DDS) 内置主题

看完这100个客户需求,我终于知道企业文档管理的秘密

Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises

剖析SGI STL空间配置器(一 、辅助接口函数)

How to Assemble a Registry
![[Mini Program Column] Summarize the development specifications of uniapp to develop small programs](/img/7b/110d324eba00652e4987bc623a5bc6.png)
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs
随机推荐
最右的一道面试算法题,--特殊基因
typescript5-编译和安装ts代码
Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises
Field interpretation under "Surgical variables (RX SUMM-SURG OTH REG/DIS)" in SEER database
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs
typescript8-类型注解
智能存储柜——解决您的存储需求
解构的运用
Judging from the Internet:
HashSet和LinkedHashSet
test3
HashSet and LinkedHashSet
typescript6 - simplify the steps to run ts
【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)
C语言经典练习题(3)——“汉诺塔(Hanoi)“
Hands-on teaching OneOS FOTA upgrade
SQL window function
ES报错处理-mapper [xx.xx] of different type, current_type [text], merged_type [keyword]
Risk Register
SQL row-column conversion