当前位置:网站首页>选择排序
选择排序
2020-11-08 18:38:00 【程序猿欧文】
选择排序算法的原理是找到数组中最小的元素,然后将它和数组第一个元素交换,接着在剩下的元素中找到最小的元素,接着和数组第二个元素交换,以此类推,直到将所有元素排序完毕
简单实现如下:
1 package 选择排序; 2 3 public class DemoMain { 4 public static void main(String[] args) { 5 Integer[] a = {5, 2, 6, 3, 4, 7}; 6 sort(a); 7 System.out.println(isSorted(a)); 8 show(a); 9 }10 11 /**12 * 排序13 * @param a 排序的数组14 */15 public static void sort(Comparable[] a) {16 for (int i = 0; i < a.length - 1; i++) {17 //获取当前要交换元素的下标18 int index = i;19 //找到最小的元素,将下标赋值给index20 for (int j = i + 1; j < a.length; j++) {21 if (less(a[index], a[j])) {22 index = j;23 }24 }25 //传递要交换的元素的下标i和最小值的下标index26 exch(a, i, index);27 }28 }29 30 /**31 * 比较大小,v > w的值大于032 * 使用compareTo是因为引用类型实现了Comparable接口33 * @param v 排序方法中传递的参数134 * @param w35 * @return 返回判断结果36 */37 private static b.........
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4708302
边栏推荐
- net.sf.json . jsonobject's format processing of time stamp
- 线程池运用不当的一次线上事故
- Proficient in high concurrency and multithreading, but can't use ThreadLocal?
- RSA非对称加密算法
- [random talk] JS related thread model sorting
- PAT_甲级_1056 Mice and Rice
- Express framework
- Learn to record and analyze
- What are the necessary laws and regulations to know when entering the Internet?
- 使用K3S创建本地开发集群
猜你喜欢

进程 线程 协程

构建者模式(Builder pattern)

C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?

Summary of interface test case ideas

习题五

git操作与分支管理规范

Package subsystem in Simulink

API生命周期的5个阶段

VIM configuration tutorial + source code

How much disk IO does a byte of read file actually take place?
随机推荐
LiteOS-消息队列-实战
Brief VIM training strategy
使用K3S创建本地开发集群
SQL 速查
Framework - SPI four modes + general device driver implementation - source code
微信小程序相关
Chapter 2 programming exercises
C + + opencv4.3 sift matching
TypeScript(1-2-2)
Python 列表的11个重要操作
latex入门
RSA asymmetric encryption algorithm
进入互联网得知道的必备法律法规有哪些?
Tencent: Although Ali's Taichung is good, it is not omnipotent!
python开发qt程序读取图片的简单流程
Development of uni app imitating wechat app
Swagger介绍和应用
read文件一个字节实际会发生多大的磁盘IO?
Flink's sink: a preliminary study
学会了volatile,你变心了,我看到了