当前位置:网站首页>2.2选择排序
2.2选择排序
2022-07-29 11:41:00 【TUJC】
2.2、选择排序
2.2.1、基本介绍
选择排序(select sorting)也属于内部排序法,是从欲排序的数据中,按指定的规则选出某一元素,再依规定交换位置后达到排序的目的。
选择排序思想:
第一次从arr[0]~arr[n-1]中选取最小值,与 arr[0]交换;
第二次从arr[1]~arr[n-1]中选取最小值,与 arr[1]交换;
第三次从arr[2]~arr[n-1]中选取最小值,与arr[2l交换;
…,
第i次从arr[i-1]~arr[n-1]中选取最小值,与arr[i-1]交换;
…,
第n-l 次从 arr[n-2]~arr[n-1]中选取最小值,与arr[n-2]交换;
总共通过n-1次,得到一个按排序码从小到大排列的有序序列。

2.2.2、代码实例
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
//选择排序
public class SelectSort {
public static void main(String[] args) {
//int [] arr = {101, 34, 119, 1, -1, 90, 123};
//创建要给80000个的随机的数组
int[] arr = new int[80000];
for (int i = 0; i < 80000; i++) {
arr[i] = (int) (Math.random() * 8000000); // 生成一个[0, 8000000) 数
}
System.out.println("排序前");
//System.out.println(Arrays.toString(arr));
Date data1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1Str = simpleDateFormat.format(data1);
System.out.println("排序前的时间是=" + date1Str);
selectSort(arr);
Date data2 = new Date();
String date2Str = simpleDateFormat.format(data2);
System.out.println("排序前的时间是=" + date2Str);
//System.out.println("排序后");
//System.out.println(Arrays.toString(arr));
}
//选择排序
public static void selectSort(int[] arr) {
//选择排序时间复杂度是 O(n^2)
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
int min = arr[i];
for (int j = i + 1; j < arr.length; j++) {
if (min > arr[j]) {
// 说明假定的最小值,并不是最小
min = arr[j]; // 重置min
minIndex = j; // 重置minIndex
}
}
// 将最小值,放在arr[0], 即交换
if (minIndex != i) {
arr[minIndex] = arr[i];
arr[i] = min;
}
//System.out.println("第"+(i+1)+"轮后~~");
//System.out.println(Arrays.toString(arr));// 1, 34, 119, 101
}
/* //使用逐步推导的方式来,讲解选择排序 //第1轮 //原始的数组 : 101, 34, 119, 1 //第一轮排序 : 1, 34, 119, 101 //算法 先简单--》 做复杂, 就是可以把一个复杂的算法,拆分成简单的问题-》逐步解决 //第1轮 int minIndex = 0; int min = arr[0]; for(int j = 0 + 1; j < arr.length; j++) { if (min > arr[j]) { //说明假定的最小值,并不是最小 min = arr[j]; //重置min minIndex = j; //重置minIndex } } //将最小值,放在arr[0], 即交换 if(minIndex != 0) { arr[minIndex] = arr[0]; arr[0] = min; } System.out.println("第1轮后~~"); System.out.println(Arrays.toString(arr));// 1, 34, 119, 101 //第2轮 minIndex = 1; min = arr[1]; for (int j = 1 + 1; j < arr.length; j++) { if (min > arr[j]) { // 说明假定的最小值,并不是最小 min = arr[j]; // 重置min minIndex = j; // 重置minIndex } } // 将最小值,放在arr[0], 即交换 if(minIndex != 1) { arr[minIndex] = arr[1]; arr[1] = min; } System.out.println("第2轮后~~"); System.out.println(Arrays.toString(arr));// 1, 34, 119, 101 //第3轮 minIndex = 2; min = arr[2]; for (int j = 2 + 1; j < arr.length; j++) { if (min > arr[j]) { // 说明假定的最小值,并不是最小 min = arr[j]; // 重置min minIndex = j; // 重置minIndex } } // 将最小值,放在arr[0], 即交换 if (minIndex != 2) { arr[minIndex] = arr[2]; arr[2] = min; } System.out.println("第3轮后~~"); System.out.println(Arrays.toString(arr));// 1, 34, 101, 119 */
}
}
边栏推荐
- AMH6.X升级到AMH7.0后,登录后台提示MySQL连接出错怎么解决?
- 『知识集锦』一文搞懂mysql索引!!(建议收藏)
- 深入理解C# 可空类型
- 593. 有效的正方形 : 简单几何运用题
- c语言:来实现一个小程序n子棋(已五子棋为例)
- Insights into the development of the enterprise live broadcast industry in 2022
- LED透明屏和LED玻璃显示屏区别
- mysql单行,多行子查询
- Deep understanding of c # delegate into the fast lanes
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
猜你喜欢
随机推荐
Is this it?TypeScript actually not difficult!(recommended collection)
std::vector 拷贝、追加、嵌套访问
IPv6 Foundation
How to use grep to find pattern matching across multiple lines
大伟 Golang之路
three.js 报错信息 RGBELoader.js:46 RGBELoader Bad File Format: bad initial token
Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
Xiaoxiao authorization system V5.0 happy version
微信怎么知道别人删除了你?批量检测方法(建群)
自采集在线电脑壁纸php源码v2.0自适应端
迁徙数据平台简单介绍
Package delivery (greedy)
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
QML(二):设置自定义窗体
1.MySQL数据库的介绍
Design and implementation of gbase8s Informix dodker high availability cluster self recovery cluster startup command oninitdb
KRYSTAL:审计数据中基于知识图的战术攻击发现框架
RediSearch 发布 v2.4.10 & v2.4.11 版本
就这?TypeScript其实并不难!(建议收藏)
AI全流程开发难题破解之钥








