当前位置:网站首页>数组中的最大值,最小值,冒泡排序
数组中的最大值,最小值,冒泡排序
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]+" ");
}
}
边栏推荐
- P1438 boring sequence line segment tree + difference
- redux三大核心
- 一张图看懂KingbaseES V9
- 法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
- Convolution neural network -- a detailed introduction to convolution of 24 bit color images
- 微信小程序编辑头像
- Effect Hook
- 详解左值、右值、左值引用以及右值引用
- Chapter 6: cloud database
- F - Pre-order and In-order(Atcoder 255)
猜你喜欢

人很话不多,工程师不耍嘴皮子

Convolution neural network -- a detailed introduction to convolution of 24 bit color images

使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

The difference between ArrayList and LinkedList
![[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)](/img/6a/02c76f5bd35b2d89e4f471b9b688f5.png)
[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)
![Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量

Plato Farm全新玩法,套利ePLATO稳获超高收益

详解左值、右值、左值引用以及右值引用

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

佳明手表怎么设置用户定制显示
随机推荐
BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)
Wechat applet rotation map
redux三大核心
[competition reference] pytorch common code snippet and operation collection
华为入局商用市场:趋势使然,挑战颇多
Brightcove任命Dan Freund为首席营收官
Anonymous named pipes, understanding and use of interprocess communication in shared memory
无有线网络下安装并配置debian
Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
干货 | 独立站运营怎么提高在线聊天客户服务?
【C语言】递归详解汉诺塔问题
[day02] Introduction to data type conversion, operators and methods
Webpack packaging Vue project adds confusion to solve the cache problem
Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
[leetcode] day104 no overlapping interval
From scratch, C language intensive Lecture 4: array
IIC 通信协议 (一)
BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions
【机器学习网络】BP神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)