当前位置:网站首页>数组中的最大值,最小值,冒泡排序
数组中的最大值,最小值,冒泡排序
2022-07-27 04:09:00 【弱冠初心】
求数组最大值
思路:
1)假设数组中的第一个元素(下标为0的元素)为最大值
2)依次取出后面的元素与假设的最大值进行比较,如果后面的元素比假设的最大值大,取代它成为新的最大值,如果比假设的最大值小,不进行替换,继续比较下一个元素
3)一直比较到最后一个元素,就可以获得数组中的最大值
实例:
int[] nums = { 11, 56, 78, 95, 46, 88, 13 ,199};
int max = nums[0];
for(int i =0;i<nums.length;i++){
if(nums[i]>max){
max=nums[i];
}
}
System.out.println("数组中的最大值是:"+max);
最大值为:运行结果

求最小值就是把if()里面的大于换成小于
int[] nums = { 11, 56, 78, 95, 46, 88, 13 ,199};
int min = nums[0];
for(int i =0;i<nums.length;i++){
if(nums[i]<min){
min=nums[i];
}
}
System.out.println("数组中的最小值是:"+min);
运行结果
冒泡排序
冒泡排序其基本思路是,对于一组要排序的元素列,依次比较相邻的两个数,将比较小的数放在前面,比较大的数放在后面,如此继续,直到比较到最后的两个数,将小数放在前面,大数放在后面,重复步骤,直至全部排序完成。
算法步骤
比较相邻的元素。如果第一个比第二个大,就交换他们两个。
对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。
针对所有的元素重复以上的步骤,除了最后一个。
持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
实例:
public static void main(String[] args) {
// 冒泡排序
int[] nums = {11,56,94,78,33};
//排序前输出数组
System.out.println("排序前的数组:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i]+" ");
}
System.out.println();
//外层循环控制行数(外控循环控制比较的轮数)
for(int i =0;i<nums.length-1;i++){
//内层循环控制列数(内层循环控制每一轮的比较次数)
for(int j =0;j<nums.length-1-i;j++){
//比较:如果前面的元素比后面的元素大,交换位置
if(nums[j]>nums[j+1]){
int temp = nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
System.out.println("排序后的数组:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i]+" ");
}
}
边栏推荐
- 使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
- 华为入局商用市场:趋势使然,挑战颇多
- 【day02】数据类型转换、运算符、方法入门
- JS three methods of traversing arrays: map, foreach, filter
- Deep analysis - dynamic memory management
- 使用Unity做一个艺术字系统
- QString转换char*
- 【C语言】递归详解汉诺塔问题
- The difference between ArrayList and LinkedList
- The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
猜你喜欢

微服务的feign调用header头被丢弃两种解决方案(附源码)
![[C language] recursively explain the tower of Hanoi problem](/img/a6/bbf1f19fc2a663df155cca53f2538a.png)
[C language] recursively explain the tower of Hanoi problem

网工知识角|只需四个步骤,教会你使用SecureCRT连接到eNSP,常用工具操作指南必看

Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)

Prometheus node exporter common monitoring indicators

JMeter学习笔记004-CSV文件行数控制循环次数

Dino paper accuracy, and analyze the variant of its model structure & Detr
深度学习领域图像分割FCN(Fully Convolutional Networks for Semantic Segmentation)

2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in

Echart柱状图中数据显示在图上方
随机推荐
Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
JVM调优中的一些常见指令
JS three methods of traversing arrays: map, foreach, filter
Pinia入门到精通,Pinia使用全流程,包含state,actions,getters,以及如何解构,进行响应,actions使用的多种方法
数据库泰斗王珊:努力创新,精心打磨优质的数据库产品
[day02] Introduction to data type conversion, operators and methods
深度剖析 —— 动态内存管理
People don't talk much, engineers don't talk much
Oracle数据库字段date怎么才能走索引?
The new Internet era has come. What new opportunities will Web 3.0 bring us
Elastic open source community: Developer Recruitment
Brightcove appoints Dan Freund as chief revenue Officer
Understand kingbasees V9 in one picture
The difference between ArrayList and LinkedList
[untitled]
shel自动设置目录权限
Prometheus Node Exporter 常用监控指标
使用Unity做一个艺术字系统
一张图看懂KingbaseES V9
人很话不多,工程师不耍嘴皮子