当前位置:网站首页>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 */
}
}
边栏推荐
- 基于flask写的一个小商城mall项目
- [image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
- Peking University open classes are coming! Welcome to the "AI for science" class
- 使用Tenserboard可视化深度学习训练过程
- 考完PMP后有什么益处
- 共建共享数字世界的根:阿里云打造全面的云原生开源生态
- 精通音视频开发是真的可以为所欲为
- The interviewer training courseware (very practical in-house training courseware)
- Is this it?TypeScript actually not difficult!(recommended collection)
- 『知识集锦』一文搞懂mysql索引!!(建议收藏)
猜你喜欢

文件上传漏洞

AI model risk assessment Part 2: core content

Starrocks technology insider: how to have both real-time update and fast query

微信云托管入门与实践

Alibaba architects spent a year sorting out the "Lucene advanced document", and you are also a big factory employee!

解决idea在debug模式下变得非常慢的问题

自采集在线电脑壁纸php源码v2.0自适应端

2022年企业直播行业发展洞察

8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice

ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching
随机推荐
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
KRYSTAL:审计数据中基于知识图的战术攻击发现框架
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主
【图像处理】基于中轴变换实现图像骨架提取附matlab代码
The interviewer training courseware (very practical in-house training courseware)
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
Lucky draw system with background source code
考完PMP后有什么益处
Summer vacation training week1
Learning with Recoverable Forgetting readings
Alibaba architects spent a year sorting out the "Lucene advanced document", and you are also a big factory employee!
Std:: vector copy, append, nested access
力扣sql刷题(四)
TCP和UDP
PHP basics uses arrays to save data
INVALID_ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
Flink UDF 函数汇总
Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
QWidget、QDialog、QMainWindow 的异同点
精通音视频开发是真的可以为所欲为