当前位置:网站首页>选择排序
选择排序
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
边栏推荐
- Talk about go code coverage technology and best practices
- PHP generates unique strings
- WebGL 水波及焦散(刻蚀)的渲染总结
- What is forsage Ethereum smart contract? What is the global decline of Ethereum
- 总结: 10月海外DeFi新项目,更多资管策略来了!
- 【杂谈】JS相关的线程模型整理
- Process thread coroutine
- Flink的sink实战之一:初探
- jsliang 求职系列 - 07 - Promise
- I used Python to find out all the people who deleted my wechat and deleted them automatically
猜你喜欢
随机推荐
C + + opencv4.3 sift matching
接口测试用例思路总结
API生命周期的5个阶段
Proficient in high concurrency and multithreading, but can't use ThreadLocal?
IT行业薪资一直遥遥领先!十年后的程序员,是否还是一个高薪职业?
RSA非对称加密算法
总结: 10月海外DeFi新项目,更多资管策略来了!
Jsliang job series - 07 - promise
Travel notes of Suzhou
Flink series (0) -- Preparation (basic stream processing)
C/C++知识分享: 函数指针与指针函数,看完这篇你还能不懂?
To introduce to you, this is my flow chart software—— draw.io
框架-SPI四种模式+通用设备驱动实现-源码
go语言参数传递到底是传值还是传引用?
Development of uni app imitating wechat app
Awk implements SQL like join operation
前后端分离跨域问题解决方案
Common memory leakage scenarios in JS
abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!






