当前位置:网站首页>排序方式(1)==>冒泡排序,简单的选择排序
排序方式(1)==>冒泡排序,简单的选择排序
2022-04-21 06:34:00 【萱萱不会秃头】
排序方式
1.冒泡排序
代码:
public static void main(String[] args) {
int[] arr = new int[] {5,9,2,1,7,6,3,4,8,0};
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr.length-1;j++) {
if(arr[j]>arr[j+1]) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("冒泡排序法的输出结果为:");
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+",");
}
}
}
输出结果

2.简单的选择排序
步骤:
①定义:第一个值为最小值
②定义一个指针,从前往后遍历,找到真正的最小值
③真正的最小值和我们认为的最小值进行位置互换
代码:
public static void main(String[] args) {
int[] arr = new int[] {1,5,8,11,6};
for(int i=0;i<arr.length;i++) {
int min=arr[i];
int minIndex=0;
for(int j=i+1;j<arr.length;j++) {
if(min>arr[j]) {
min=arr[j];
minIndex=j;
arr[minIndex]=arr[i];
arr[i]=min;
}
}
}
System.out.println("简单选择排序的输出结果为:");
for( int i = 0;i<arr.length;i++) {
System.out.print(arr[i]+",");
}
}
输出结果

版权声明
本文为[萱萱不会秃头]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_53954158/article/details/119274295
边栏推荐
猜你喜欢

TX2上运行ENet

CS5801规格书|CS5801HDMI转EDP转换方案|HDMI转DP转接板设计,HDMI2.0转EDP1.4,支持向下兼容

Idea 2021.1 Useful settings

Jfinal framework easyexcel plug-in export with pictures

Hardware codec acceleration CSI interface camera on TX2

Word XML space character

音频功率放大器,纳芯威功放,NS4165B单通道5.3W AB\D类功放,主要替换智浦芯的CS8571、CS8871、CS5218海栎创的HAA2018
![[hand shape and pose estimation from a single RGB image]](/img/df/099acf25a3062a84b1c2ae284b50c6.png)
[hand shape and pose estimation from a single RGB image]

有关imap-tools模块实现邮件移动的问题

MS12_020漏洞
随机推荐
Method and principle of obtaining the CPU core of the last execution of the process
Use of checkbox in thymleaf engine
Huatian OA vulnerability reproduction manual
Any user registration & any user password modification
Previous blog posts in blog Park
弱口令-20211221
Use Baidu map POI to crawl the required data
nodered连接数据库
Lua programming -- Notes
[hand pose estimation] open source data set - Collection - rgbd IR depth
Unity List使用Find或FindAll查找特定元素以及特定元素个数
金融信息安全实训——22/4/18
Steps of connecting hbuilderx to nocturnal simulator
MySQL的安装与配置——详细教程
任意用户注册&任意用户密码修改
龙讯系列视频转换,LT9211、LT8918,功能:lvds转BT656,lvds转mipi(CSI\DSI)RGB转MIPI(DSI\CSI) BT656\601\1120转HDMI1.4\DVI
分片上传解决单文件上传的问题
用友OA漏洞复现手册
Hardware codec acceleration CSI interface camera on TX2
钢条切割问题最优解