当前位置:网站首页>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 */
}
}
边栏推荐
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
- Applied practical skills of deep reinforcement learning
- MySQL高级_视图
- Hutool日期时间
- IPV6基础
- Peking University open classes are coming! Welcome to the "AI for science" class
- How to use grep to find pattern matching across multiple lines
- std::vector 拷贝、追加、嵌套访问
- 【Unity3D】场景切换、退出全屏、退出游戏
- 【Untitled】
猜你喜欢
PaddleLite 编译以及代码跑通复盘
Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
puzzle(017.5)联动归位
QWidget、QDialog、QMainWindow 的异同点
如何使用“COPY –link”加速 Docker 构建和优化缓存
Learning with Recoverable Forgetting readings
【Unity3D】角色控制器(CharacterController)
IPV6基础
Qt 之自定义界面(实现无边框、可移动)
解决idea在debug模式下变得非常慢的问题
随机推荐
大伟 Golang之路
Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition
【Untitled】
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
基于flask写的一个小商城mall项目
面试官培训课件(非常实用的企业内训课件)
微信怎么知道别人删除了你?批量检测方法(建群)
IPv6 Foundation
After connect and SQL join in on conditions and where
AI model risk assessment Part 2: core content
QML(一):自定义圆角按钮的处理
黑马四小时入门学习记录-3|网络应用
HMS Core Discovery 16 review | with tiger mound, embracing new AI "voice" state
Function comparison between report control FastReport and stimulus soft
"Knowledge Collection" article to understand mysql index!!(recommended collection)
如何使用“COPY –link”加速 Docker 构建和优化缓存
golang 实现文件上传下载
Deep understanding of c # delegate into the fast lanes
three.js 报错信息 RGBELoader.js:46 RGBELoader Bad File Format: bad initial token
Hutool日期时间