选择排序(Selection Sort)
来源:互联网 发布:听小说的软件 编辑:程序博客网 时间:2024/05/21 08:58
1.选择排序算法原理
选择排序是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。选择排序是不稳定的排序方法。
2.选则排序算法的基本步骤
(1)从待排序序列中,找到关键字最小的元素;(2)如果最小元素不是待排序序列的第一个元素,将其和第一个元素互换;(3)从余下的N-1个元素中,找出关键字最小的元素,重复(1)、(2)步,直到排序结束。
3.算法实现的示例
从上图可知,每趟排序中,将当前第i小的元素放在位置i上
4.算法代码实现(Java)
public class SelectionSort { public static void main(String[] args) { int[] array = new int[]{9, 1, 2, 5, 7, 4, 8, 6, 3, 5}; selectionSort(array); for(Integer i : array){ System.out.print(i + " "); } } public static void selectionSort(int[] array){ for(int i = 0; i < array.length-1; i++){ int temp = 0;//记录临时值 int index = i;//用来保存最小值的索引 //寻找第i个小的数值 for(int j = i+1; j < array.length; j++){ if(array[index] > array[j]){ index = j; } } //将找到的第i个小的数值放到第i个位置上 temp = array[index]; array[index] = array[i]; array[i] = temp; } }}排序结果:1 2 3 4 5 5 6 7 8 9
0 0
- 选择排序(selection sort)
- 选择排序(Selection Sort)
- selection sort 选择排序
- 选择排序 Selection Sort
- 选择排序(Selection Sort)
- 选择排序(Selection Sort)
- 选择排序 (Selection Sort)
- 选择排序(Selection sort)
- 选择排序 (Selection sort)
- 选择排序Selection sort
- Selection Sort(选择排序)
- 选择排序(Selection Sort)
- Selection Sort选择排序
- selection sort 选择排序
- selection sort(选择排序)
- Selection sort 选择排序
- 选择排序Selection sort
- 选择排序(Selection Sort)
- maven pom文件解析
- 直播学习一链接
- uva 1543 Telescope
- java.security.InvalidKeyException: Illegal key size
- nyoj214_单调递增子序列(2)二分查找 nlogn
- 选择排序(Selection Sort)
- cocoaPods安装详解
- 17.词法分析和语法分析
- AngularJS 表单元素值绑定
- uploadify 上传出现 security error错误的解决方法
- win10 vs2015 freetype2编译
- javascript深入理解js闭包
- C#
- 100. Same Tree