基本介绍
选择式排序也属于内部排序法,是从欲排序的数据中,按指定的规则选出某一元素,再依规定交换位置后达到排序的目的。
选择排序思想:
选择排序(select sorting)也是一种简单的排序方法。它的基本思想是:第一次从arr[0]arr[n-1]中选取最小值,与arr[0]交换,第二次从arr[1]arr[n-1]中选取最小值,与arr[1]交换,第三次从arr[2]arr[n-1]中选取最小值,与arr[2]交换,…,第i次从arr[i-1]arr[n-1]中选取最小值,与arr[i-1]交换,…, 第n-1次从arr[n-2]~arr[n-1]中选取最小值,与arr[n-2]交换,总共通过n-1次,得到一个按排序码从小到大排列的有序序列。
选择排序思路分析图
选择排序的思路图解
原始的数组 : 101, 34, 119, 1
第一轮排序 : 1, 34, 119, 101
第二轮排序 : 1, 34, 119, 101
第三轮排序 : 1, 34, 101, 119
说明:
1. 选择排序一共有 数组大小 - 1 轮排序
2. 每1轮排序,又是一个循环, 循环的规则(代码)
2.1先假定当前这个数是最小数
2.2 然后和后面的每个数进行比较,如果发现有比当前数更小的数,就重新确定最小数,并得到下标
2.3 当遍历到数组的最后时,就得到本轮最小数和下标
2.4 交换 [代码中再继续说 ]
代码实现
package cn.icanci.datastructure.sort;
import java.util.Arrays;
/**
* @Author: icanci
* @ProjectName: AlgorithmAndDataStructure
* @PackageName: cn.icanci.datastructure.sort
* @Date: Created in 2020/3/6 21:20
* @ClassAction: 选择排序
*/
public class SelectSort {
public static void main(String[] args) {
int[] arr = {101, 34, 119,2323,324,45,65,34,-1,-54 ,1};
System.out.println("排序之前");
System.out.println(Arrays.toString(arr));
System.out.println("开始排序");
// selectSort(arr);
System.out.println("==========优化=============");
selectSortEnd(arr);
}
public static void selectSortEnd(int[] arr) {
for (int i = 0; i < arr.length -1; i++){
int minIndex = i;
int minVal = arr[minIndex];
//嘉定最小 数的索引
for (int j = i + 1; j < arr.length; j++) {
if (minVal > arr[j]) {
//此时假定的不是最小的
minVal = arr[j];
minIndex = j;
}
}
//第二轮 把最小的 放在arr[1]
if (minIndex != i) {
arr[minIndex] = arr[i];
arr[i] = minVal;
}
System.out.println(Arrays.toString(arr));
}
}
public static void selectSort(int[] arr) {
//逐渐推倒
//原始数组 101 34 119 1
//第一轮的排序 1 34 119 101
//算法 先简单=>复杂 就是可以把复杂的算法 拆分为简单的问题 逐步解决
//嘉定最小 数的索引
int minIndex = 0;
int minVal = arr[minIndex];
for (int j = 0 + 1; j < arr.length; j++) {
if (minVal > arr[j]) {
//此时假定的不是最小的
minVal = arr[j];
minIndex = j;
}
}
//第二轮 把最小的 放在arr[1]
if (minIndex != 0) {
arr[minIndex] = arr[0];
arr[0] = minVal;
}
System.out.println(Arrays.toString(arr));
//嘉定最小 数的索引
minIndex = 1;
minVal = arr[minIndex];
for (int j = 1 + 1; j < arr.length; j++) {
if (minVal > arr[j]) {
//此时假定的不是最小的
minVal = arr[j];
minIndex = j;
}
}
//把最小的 放在arr[0]
if (minIndex != 1) {
arr[minIndex] = arr[1];
arr[1] = minVal;
}
System.out.println(Arrays.toString(arr));
//嘉定最小 数的索引
minIndex = 2;
minVal = arr[minIndex];
for (int j = 2 + 1; j < arr.length; j++) {
if (minVal > arr[j]) {
//此时假定的不是最小的
minVal = arr[j];
minIndex = j;
}
}
//把最小的 放在arr[2]
if (minIndex != 2) {
arr[minIndex] = arr[2];
arr[2] = minVal;
}
System.out.println(Arrays.toString(arr));
}
}
测试
排序之前
[101, 34, 119, 2323, 324, 45, 65, 34, -1, -54, 1]
开始排序
==========优化=============
[-54, 34, 119, 2323, 324, 45, 65, 34, -1, 101, 1]
[-54, -1, 119, 2323, 324, 45, 65, 34, 34, 101, 1]
[-54, -1, 1, 2323, 324, 45, 65, 34, 34, 101, 119]
[-54, -1, 1, 34, 324, 45, 65, 2323, 34, 101, 119]
[-54, -1, 1, 34, 34, 45, 65, 2323, 324, 101, 119]
[-54, -1, 1, 34, 34, 45, 65, 2323, 324, 101, 119]
[-54, -1, 1, 34, 34, 45, 65, 2323, 324, 101, 119]
[-54, -1, 1, 34, 34, 45, 65, 101, 324, 2323, 119]
[-54, -1, 1, 34, 34, 45, 65, 101, 119, 2323, 324]
[-54, -1, 1, 34, 34, 45, 65, 101, 119, 324, 2323]
测试8万条数据
package cn.icanci.datastructure.sort;
import java.util.Arrays;
/**
* @Author: icanci
* @ProjectName: AlgorithmAndDataStructure
* @PackageName: cn.icanci.datastructure.sort
* @Date: Created in 2020/3/6 21:20
* @ClassAction: 选择排序
*/
public class SelectSort {
public static void main(String[] args) {
// int[] arr = {101, 34, 119,2323,324,45,65,34,-1,-54 ,1};
System.out.println("排序之前");
System.out.println("开始排序");
long start = System.currentTimeMillis();
// selectSort(arr);
int[] arr = new int[80000];
for (int i =0;i<80000;i++){
arr[i] = (int) (Math.random() * 8000000);
}
System.out.println("==========优化=============");
selectSortEnd(arr);
System.out.println(System.currentTimeMillis() -start +":ms");
}
public static void selectSortEnd(int[] arr) {
for (int i = 0; i < arr.length -1; i++){
int minIndex = i;
int minVal = arr[minIndex];
//嘉定最小 数的索引
for (int j = i + 1; j < arr.length; j++) {
if (minVal > arr[j]) {
//此时假定的不是最小的
minVal = arr[j];
minIndex = j;
}
}
//第二轮 把最小的 放在arr[1]
if (minIndex != i) {
arr[minIndex] = arr[i];
arr[i] = minVal;
}
// System.out.println(Arrays.toString(arr));
}
}
}
测试时间
排序之前
开始排序
==========优化=============
2561:ms