选择排序的思想是:内外两层循环,第一层循环从第一个数开始到倒数第一个数,

第二层循环从上一层的数开始, 与上一层循环的数比较,如果小于则交换位置、

代码如下:

public class SelectSort {
public int[] sort(int[] arrays) {
int temp = arrays[0];
for (int i = 0; i < arrays.length - 1; i++) {
for (int j = i + 1; j < arrays.length; j++) {
if (arrays[j] <= arrays[i]) {
temp = arrays[i];
arrays[i] = arrays[j];
arrays[j] = temp;
}
}
}
return arrays;
}
}

测试类如下:

package Test;

import org.omg.CORBA.Current;

import bubbleSort.BubbleSort;
import insertSort.InsertSort;
import quickSort.QuickSort;
import selectSort.SelectSort; public class Test {
public static void main(String[] args) { SelectSort selectSort = new SelectSort();
int[] array = createArray();
long ct1 = System.currentTimeMillis();
int[] arrays = selectSort.sort(array);
long ct2 = System.currentTimeMillis();
display(arrays);
System.out.println("所消耗的时间:" + (ct2 - ct1));
} public static void display(int[] arrays) {
System.out.println("排序后数据:");
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i] + "\t");
if ((i + 1) % 10 == 0) {
System.out.println();
}
}
System.out.println();
} public static int[] createArray() {
int[] array = new int[100000];
System.out.println("数组中元素是:");
for (int i = 0; i < 100000; i++) {
array[i] = (int) (Math.random() * 1000);
System.out.print(array[i] + "\t");
if ((i + 1) % 10 == 0) {
System.out.println();
}
} System.out.println();
return array;
}
}

算法效率和冒泡排序是一样的,进行100000个数的排序要14000ms。

最新文章

  1. Java内部DNS查询实现和参数设置
  2. 写shell脚本速查笔记
  3. java之RTTI和反射的理解
  4. Oracle基础&lt;4&gt;--程序包
  5. 第三十七节,hashlib加密模块
  6. SourceTree 将本地已有的git项目推送到远程git仓库
  7. python交互模式下tab键自动补全
  8. 【游戏周边】Unity,UDK,Unreal Engine4或者CryENGINE——我应该选择哪一个游戏引擎
  9. Redis的并发竞争问题的解决方案总结
  10. 显著性检测(saliency detection)评价指标之sAUC(shuffled AUC)的Matlab代码实现
  11. dbexpress连接mysql提示Operation not allowed on a unidirectional dataset
  12. Python设计模式 - 创建型 - 单例模式(Singleton) - 十种
  13. 持续集成工具-Jenkins 使用介绍
  14. scala-泛型
  15. [LeetCode] 598. Range Addition II_Easy tag: Math
  16. Mybatis简介、环境搭建和详解
  17. External component has thrown an exception
  18. 20155222 2016-2017-2 《Java程序设计》第8周学习总结
  19. hdoj 2717 Catch That Cow
  20. c# 滚动字幕的实现

热门文章

  1. 多通道(比方RGB三通道)卷积过程
  2. redis启动错误-- Creating Server TCP listening socket *:6379: listen: UnKnown error
  3. Refused to set unsafe header
  4. php总结7——文件函数库、序列化数据、文件包含
  5. ProgressBar+WebView实现自定义浏览器
  6. install_driver(mysql) failed
  7. 应用索引技术优化SQL 语句(转)
  8. Exception of type &#39;System.OutOfMemoryException&#39; was thrown
  9. Java基础:hashCode与equals个人学习记录
  10. winform中通过事件实现窗体传值思路【待修改】