选择排序(java)
2024-09-07 12:02:19
选择排序的思想是:内外两层循环,第一层循环从第一个数开始到倒数第一个数,
第二层循环从上一层的数开始, 与上一层循环的数比较,如果小于则交换位置、
代码如下:
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。
最新文章
- Java内部DNS查询实现和参数设置
- 写shell脚本速查笔记
- java之RTTI和反射的理解
- Oracle基础<;4>;--程序包
- 第三十七节,hashlib加密模块
- SourceTree 将本地已有的git项目推送到远程git仓库
- python交互模式下tab键自动补全
- 【游戏周边】Unity,UDK,Unreal Engine4或者CryENGINE——我应该选择哪一个游戏引擎
- Redis的并发竞争问题的解决方案总结
- 显著性检测(saliency detection)评价指标之sAUC(shuffled AUC)的Matlab代码实现
- dbexpress连接mysql提示Operation not allowed on a unidirectional dataset
- Python设计模式 - 创建型 - 单例模式(Singleton) - 十种
- 持续集成工具-Jenkins 使用介绍
- scala-泛型
- [LeetCode] 598. Range Addition II_Easy tag: Math
- Mybatis简介、环境搭建和详解
- External component has thrown an exception
- 20155222 2016-2017-2 《Java程序设计》第8周学习总结
- hdoj 2717 Catch That Cow
- c# 滚动字幕的实现
热门文章
- 多通道(比方RGB三通道)卷积过程
- redis启动错误-- Creating Server TCP listening socket *:6379: listen: UnKnown error
- Refused to set unsafe header
- php总结7——文件函数库、序列化数据、文件包含
- ProgressBar+WebView实现自定义浏览器
- install_driver(mysql) failed
- 应用索引技术优化SQL 语句(转)
- Exception of type &#39;System.OutOfMemoryException&#39; was thrown
- Java基础:hashCode与equals个人学习记录
- winform中通过事件实现窗体传值思路【待修改】