选择排序 - SelectionSort
来源:互联网 发布:java web项目 license 编辑:程序博客网 时间:2024/06/08 17:29
选择排序-SelectionSort
选择排序是一种很直观的排序算法,就是每次选出最小(大)的元素,依次放在序列的前面。
基本思想
在要排序的一组数中,选出最小(或者最大)的一个数与第1个位置的数 交换(swap);然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素(最后一个数)比较为止。
操作方法:
第一趟,从n 个记录中找出关键码最小的记录与第一个记录交换;
第二趟,从第二个记录开始的n-1 个记录中再选出关键码最小的记录与第二个记录交换;
以此类推…..
第i 趟,则从第i 个记录开始的n-i+1 个记录中选出关键码最小的记录与第i 个记录交换,
直到整个序列按关键码有序。
算法实现
/** * 选择排序 双层循环,每次找出剩下序列中最小的放到合适位置 * * @author QT * */public class SelectionSortDemo { public static void main(String[] args) { int[] a = new int[] { 12, 43, 3, 61, 25, 31, 23 }; selectSort(a, a.length); for (int i : a) { System.out.print(i + " "); } } public static void selectSort(int[] a, int length) { // 每次在剩下的序列中找到最小的,放到最前面 for (int i = 0; i < length - 1; i++) { // 临时变量,保存最小值 int min = a[i]; // 记录最小值的index int k = i; for (int j = i + 1; j < length; j++) { if (a[j] < min) { min = a[j]; k = j; } } // 找到最小的,交换元素 int Temp = a[i]; a[i] = a[k]; a[k] = Temp; } }}
运行结果
1 0
- 选择排序SelectionSort
- 选择排序-SelectionSort
- 选择排序 - SelectionSort
- 选择排序SelectionSort
- 选择排序(1)SelectionSort
- SelectionSort -- 选择排序(C++)
- c/c++选择排序(SelectionSort)
- 2-1选择排序 selectionSort
- 排序算法 之 选择排序SelectionSort
- 经典算法-选择排序(SelectionSort)
- Java排序算法(七)--简单选择排序(SelectionSort)
- 【算法导论学习-003】选择排序(SelectionSort)
- 快速排序(SelectionSort)
- Java排序再巩固--SelectionSort
- SelectionSort
- selectionSort
- selectionSort
- selectionSort
- Move Zeroes
- 定位 python image not found问题
- shared_ptr的一些尴尬
- 【NOIP2014提高组】【Day1】【解题报告】
- FancyCoverFlow(3d画廊)
- 选择排序 - SelectionSort
- 函数指针的赋值与调用
- Maven Sonatype Nexus return 401
- GPS串口数据格式
- WeakPtr 学习
- 外网访问 虚拟机下的web服务器 设置主机端口与虚拟机ip和端口的映射
- About email
- 黑马程序员 Java基础——反射
- Android实战 - 个人App 乐逗项目 开启篇