当前位置:网站首页>选择排序
选择排序
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
边栏推荐
- Liteos message queuing
- Package subsystem in Simulink
- latex入门
- uni-app实战仿微信app开发
- 二叉树的四种遍历方应用
- How to deploy pytorch lightning model to production
- 使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
- SQL 速查
- IT行业薪资一直遥遥领先!十年后的程序员,是否还是一个高薪职业?
- C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?
猜你喜欢
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
二叉树的四种遍历方应用
c++ opencv4.3 sift匹配
IT industry salary has been far ahead! Ten years later, is the programmer still a high paying profession?
Package subsystem in Simulink
To introduce to you, this is my flow chart software—— draw.io
Design by contract (DBC) and its application in C language
experiment
进入互联网得知道的必备法律法规有哪些?
机械硬盘随机IO慢的超乎你的想象
随机推荐
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
聊聊Go代码覆盖率技术与最佳实践
read文件一个字节实际会发生多大的磁盘IO?
Mac环境安装Composer
Liteos message queuing actual combat
进程 线程 协程
LiteOS-消息队列
PAT_ Grade A_ 1056 Mice and Rice
Builder pattern
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
If the programming language as martial arts unique! C++ is Jiu Yin Jing. What about programmers?
Summary of rendering of water wave and caustics (etching) in webgl
一分钟全面看懂forsage智能合约全球共享以太坊矩阵计划
AI香水来了,你会买吗?
An online accident caused by improper use of thread pool
佛萨奇forsage以太坊智能合约是什么?以太坊全球滑落是怎么回事
Framework - SPI four modes + general device driver implementation - source code
第五章编程题
Travel notes of Suzhou