当前位置:网站首页>数组中的最大值,最小值,冒泡排序
数组中的最大值,最小值,冒泡排序
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]+" ");
}
}
边栏推荐
- 华为入局商用市场:趋势使然,挑战颇多
- Word/excel has a fixed table size. When filling in the content, the table does not change with the cell content
- Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
- 【独立站建设】跨境电商出海开网店,首选这个网站建设!
- ELS square display principle
- 管理信息系统期末复习
- playwright网络爬虫实战案例分享
- 【HCIP】重发布、重分布、重分发实验
- C get UUID
- shel自动设置目录权限
猜你喜欢

Playwright web crawler actual battle case sharing

From scratch, C language intensive Lecture 4: array

项目参数做成可配置项,@ConfigurationProperties注解的使用

Shel automatically sets directory permissions

佳明手表怎么设置用户定制显示

Convolution neural network -- convolution of gray image

Detailed explanation of TCP protocol knowledge

数据分析师岗位分析

Deep analysis - dynamic memory management

从零开始C语言精讲篇4:数组
随机推荐
Convolution neural network -- a detailed introduction to convolution of 24 bit color images
[small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation
BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions
els 方块显示原理
sram、dram、sdram、ddr的区别和用途
Preliminary understanding of NiO
STM32基于HAL库的串口接受中断和空闲中断
标准C语言13
安全第四次课后练习
Hash (hash)
Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number
Shel automatically sets directory permissions
There are two solutions for the feign call header of microservices to be discarded (with source code)
pinia的持久化存储,pinia使用插件进行持久化存储。
Effect Hook
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in
Wechat applet rotation map
2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ ) type integer in
Network knowledge corner | it only takes four steps to teach you to use SecureCRT to connect to ENSP. You must see the operation guide of common tools
【HCIP】重发布、重分布、重分发实验