当前位置:网站首页>选择排序
选择排序
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
边栏推荐
- markdown使用
- WebGL 水波及焦散(刻蚀)的渲染总结
- 【杂谈】JS相关的线程模型整理
- Tencent: Although Ali's Taichung is good, it is not omnipotent!
- Jsliang job series - 07 - promise
- 如果把编程语言当武功绝学!C++是九阴真经,那程序员呢?
- Regular backup of WordPress website program and database to qiniu cloud
- abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
- SQL quick query
- vim-配置教程+源码
猜你喜欢
随机推荐
net.sf.json . jsonobject's format processing of time stamp
11 important operations of Python list
Apache Kylin远程代码执行漏洞复现(CVE-2020-1956)
Interesting article sharing: what is the difference between C language and C + +, C?
Chapter 5 programming
实验
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
How much disk IO does a byte of read file actually take place?
Part I - Chapter 1 Overview
Implementation of warehouse management system with ABP (net core) + easyUI + efcore
RSA asymmetric encryption algorithm
如何将PyTorch Lightning模型部署到生产中
Brief VIM training strategy
How much faster is a server equipped with a SSD than a mechanical hard disk
Simple process of reading pictures by QT program developed by Python
Express框架
第五章编程题
Express framework
构建者模式(Builder pattern)
Liteos message queuing