当前位置:网站首页>Li Kou brush question diary /day8/7.1
Li Kou brush question diary /day8/7.1
2022-07-04 18:23:00 【Bobo toilet cleaning spirit】
Novice Village
Bubble sort :
Realize the orderly arrangement of elements in the array according to the numerical size ( Ascending , Descending )
Realization thought :( Take descending as an example )
1. Compare the first data with the second data , If the first data is smaller than the second data , Exchange the two data locations
2. The pointer points from the first data to the second data , The second data is compared with the third data , If the second data is smaller than the third data , Exchange the two data locations
3. By analogy , Complete the first round of sorting , After the first round of sorting , The largest element is moved to the far right
4. Repeat the above process , After each row , The number of comparisons is less than once
Coding ideas : It takes two cycles , Layer 1 cycle i Indicates the number of rounds of sorting , The second cycle j Indicates the number of comparisons
For example, a simpler bubble sort , Put the first number and the following number Compare sizes one by one , If it is less than , Then swap positions , Greater than, it will not move . here , The first number is the maximum number in the array . Then compare the second number with the following numbers one by one , And so on .
public class Test {
public static void main(String[] args) {
int [] array = {12,3,1254,235,435,236,25,34,23};
int temp;
for (int i = 0; i < array.length; i++) {
for (int j = i+1; j < array.length; j++) {
if (array[i] < array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp; // Two numbers exchange positions
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
}
Example :
class Solution {
public int findKthLargest(int[] nums, int k) {
for(int i=0;i<nums.length;i++){
int temp;
for(int j=i+1;j<nums.length;j++){
if(nums[i]<nums[j]){
temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
}
int ans=nums[k-1];
return ans;
}
}
边栏推荐
- Mysql5.7 installation tutorial graphic explanation
- 通过事件绑定实现动画效果
- 能源行业的数字化“新”运维
- Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
- 内核中时间相关的知识介绍
- General environmental instructions for the project
- Heartless sword Chinese translation of Elizabeth Bishop's a skill
- [test development] software testing - Basics
- 【211】go 处理excel的库的详细文档
- DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
猜你喜欢
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
TCP waves twice, have you seen it? What about four handshakes?
华为云ModelArts的使用教程(附详细图解)
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
Talk about seven ways to realize asynchronous programming
Weima, which is going to be listed, still can't give Baidu confidence
被忽视的问题:测试环境配置管理
Recast of recastnavigation
Thawte通配符SSL证书提供的类型有哪些
MySQL常用增删改查操作(CRUD)
随机推荐
Pytorch深度学习之环境搭建
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
Blue bridge: sympodial plant
2022 national CMMI certification subsidy policy | Changxu consulting
About the pit of firewall opening 8848 when Nacos is started
基于NCF的多模块协同实例
同事悄悄告诉我,飞书通知还能这样玩
【211】go 处理excel的库的详细文档
超标量处理器设计 姚永斌 第7章 寄存器重命名 摘录
表情包坑惨职场人
DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
Heartless sword Chinese translation of Elizabeth Bishop's a skill
[210] usage of PHP delimiter
Once the "king of color TV", he sold pork before delisting
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
【云端心声 建议征集】云商店焕新升级:提有效建议,赢海量码豆、华为AI音箱2!
为啥有些线上演唱会总是怪怪的?
MySQL common add, delete, modify and query operations (crud)
Clever use of curl command
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配