选择排序
来源:互联网 发布:java伪装ip发起请求 编辑:程序博客网 时间:2024/04/29 22:05
考虑到冒泡排序的移动次数o(N^2),选择排序设置标志位,每一轮排序只移动一次。
public class SelectSort {
public void selectSort(int[] as){
int min = 0;
for(int i=0;i<as.length;i++){
//设置最小的初始位置
min = i;
for(int j=i+1;j<as.length;j++){
if(as[min]<as[j]){
min = j;
}
}
swap(as,min,i);
}
}
private void swap(int[] as,int aIndex,int bIndex){
int temp = as[aIndex];
as[aIndex] = as[bIndex];
as[bIndex]=temp;
}
private void printDatas(int[] as){
System.out.println("------------------------>");
for(int i:as){
System.out.println(i);
}
}
public static void main(String[] args) {
SelectSort t = new SelectSort();
int[] as = new int[]{3,2,8,6,1};
t.selectSort(as);
t.printDatas(as);
}
}
0 0
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序:
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- NandFlash系列之一:NorFlash与NandFlash对比
- caffe python visualization程序解析
- 关于ArrayList 通过循环添加重复异常
- redis学习
- apache + tomcat 集群
- 选择排序
- 所学即为用
- Altium Designer PCB设计规则中英对照
- NandFlash系列之二:S3C2410读写Nand Flash分析
- 【转】c# TabControl 中TabPages放左边显示文字
- linux brctl
- uva 213 Message Decoding
- 01_MyBatis EHCache集成及所需jar包,ehcache.xml配置文件参数配置及mapper中的参数配置
- html实现复杂表头