当前位置:网站首页>选择排序
选择排序
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
边栏推荐
- Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
- One minute comprehensive understanding of forsage smart contract global shared Ethereum matrix plan
- 学会了volatile,你变心了,我看到了
- write文件一个字节后何时发起写磁盘IO
- 使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
- python开发qt程序读取图片的简单流程
- Simulink中封装子系统
- experiment
- Awk implements SQL like join operation
- [open source]. Net uses ORM to access Huawei gaussdb database
猜你喜欢

PHP generates unique strings

契约式设计(Dbc)以及其在C语言中的应用

如果把编程语言当武功绝学!C++是九阴真经,那程序员呢?

When to write disk IO after one byte of write file

python开发qt程序读取图片的简单流程

构建者模式(Builder pattern)

学会了volatile,你变心了,我看到了

接口测试工具Eolinker进行post请求

AI香水来了,你会买吗?

Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
随机推荐
vim-配置教程+源码
abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
一分钟全面看懂forsage智能合约全球共享以太坊矩阵计划
后缀表达式转中缀表达式
微信小程序相关
框架-SPI四种模式+通用设备驱动实现-源码
Summary: in October, more investment management strategies have come to the new overseas defi project!
Express framework
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
关于adb连接手机offline的问题解决
RabbitMQ之Helloworld
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
SQL quick query
如果把编程语言当武功绝学!C++是九阴真经,那程序员呢?
VirtualBox安装centos7
Elasticsearch learning one (basic introduction)
How to deploy pytorch lightning model to production
What are the necessary laws and regulations to know when entering the Internet?
Tencent: Although Ali's Taichung is good, it is not omnipotent!
python开发qt程序读取图片的简单流程