Selection sort的java语言实现
来源:互联网 发布:删除表多个字段sql 编辑:程序博客网 时间:2024/05/21 11:00
选择排序(Selection Sort)的实现
思想如下:
- First,find the smallest element in the array,and exchange it with the element in the first position
- Then ,find the second smallest element in the array.and exchange it with the element in the second position
- continue in this way until the entire array is sorted
java 实现代码如下:
public class SelectionSort { public static void main(String[] args) { //选择排序的实现 int[] a={9,8,7,6,5,4,3,2,1,0}; System.out.println("排序前的数组如下:"+Arrays.toString(a)); int min; for(int i=0;i<a.length-1;i++){ min=i; int j=i+1; for(;j<a.length;j++){//在数组中没有排序的部分中寻找的最小值 if(a[min]>a[j]){ min=j; } } //每次循坏找到最小值后就与当前位置进行交换 swap(a,i,min); } System.out.println("排序后的数组如下:"+Arrays.toString(a)); } public static void swap(int [] a,int i,int j){ int temp; if(i!=j){ temp=a[i]; a[i]=a[j]; a[j]=temp; } }}
总结
- 选择排序至多会交换n次
- 选择排序的时间复杂度为O(n^2);
So, Selection sort can be useful when memory write is a costly operation.
关于选择排序的C语言实现,可以查看这里
1 0
- Selection sort的java语言实现
- 选择排序的php实现 Selection Sort
- 选择排序(Selection Sort)原理及Java实现
- 简单选择排序算法(Simple Selection Sort) Java实现
- Merge Sort的java语言实现
- java version: Simple Selection Sort
- java选择排序Selection Sort
- 选择排序(selection sort)算法实现
- 选择排序(selection sort)算法实现
- Selection sort
- Selection Sort
- Selection sort
- Selection Sort
- selection sort
- selection sort
- Selection sort
- Java实例7 - 选择排序 Selection Sort
- Java实例7 - 选择排序 Selection Sort
- sgu242:Student's Morning(网络流)
- LeakCanary Eclipse
- HDU2084
- 欢迎使用CSDN-markdown编辑器
- 那些n位数
- Selection sort的java语言实现
- 第十三周项目三——立体类族共有的抽象类
- 第七章 实现炫酷效果—图像和动画(1)
- java中用jsoup抓取网页源码,并批量下载图片
- POJ 2976 Dropping tests 01分数规划
- 操作系统--进程管理
- 第七章 实现炫酷效果—图像和动画(2)
- 关于 java JSONArray JSONObject转换问题
- javaweb-servlet开发