SelectionSort
来源:互联网 发布:2016最新淘宝网店书籍 编辑:程序博客网 时间:2024/06/06 01:50
选择排序的核心思想每一次循环将最小的那个数找出来放在右边,首先将数组中的元素都比较一遍,记录下较小的元素的小标,然后将改下标与随后的元素进行相比,最后交换第一各元素和最小元素的下标的值。
class SelectionSort { public static void main(String[] args) { int[] a = new int[]{1,5,2,6,4}; int[] c = a; int[] b = new int[a.length]; for (int i = 0;i<a.length ;i++ ) { int min = i; for (int j =i+1;j<a.length ;j++ ) { if (a[min]>a[j]) { min = j;//将最较小值元素的下标保存下来 } } exch(a,i,min);//得到的最小值的下标,对于一个循环只交换一次 } printArr(c); } public static void printArr(int[] b) { for (int i= 0;i<b.length; i++ ) { if (i==0) { System.out.print("{"+b[i]+","); } else if (i<b.length-1) { System.out.print(b[i]+","); } else System.out.println(b[i]+"}"); } } public static void exch(int[] a,int i,int j) { int temp= a[i]; a[i]=a[j]; a[j]=temp; }}
阅读全文
0 0
- SelectionSort
- selectionSort
- selectionSort
- selectionSort
- selectionSort
- SelectionSort
- selectionSort
- Golang - SelectionSort
- 选择排序SelectionSort
- 快速排序(SelectionSort)
- 选择排序-SelectionSort
- 选择排序 - SelectionSort
- 算法基础复习-SelectionSort
- 选择排序SelectionSort
- 【代码积累】SelectionSort
- bubblesort and selectionsort
- 选择排序(1)SelectionSort
- SelectionSort -- 选择排序(C++)
- kernel driver probe sequence
- oracle中的左右外连接
- 20个高级Java面试题汇总
- 简易使用Xshell5查看生产日志
- linux led驱动例程(杂项设备)
- SelectionSort
- 二叉搜索树的后序遍历序列
- IOS仿最新版爱奇异loading动画
- PAT 甲级 1020. Tree Traversals (25)
- json和xml的系统比较
- 天佑回应杨幂事件 MC天佑:只是简单的目的帮助毛不易晋级
- 大明A+B(JAVA大数)
- 关于产生随机数函数
- WPF 图片无法显示……关于Application及SiteOfOrigin的路径问题