当前位置:网站首页>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;
}
}
边栏推荐
- “在越南,钱就像躺在街上”
- RecastNavigation 之 Recast
- Win32 API 访问路由的加密网页
- 【每日一题】556. 下一个更大元素 III
- Redis主从复制
- High school physics: force, object and balance
- The controversial line of energy replenishment: will fast charging lead to reunification?
- 【209】go语言的学习思想
- Large scale service exception log retrieval
- DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
猜你喜欢
Why are some online concerts always weird?
Easy to use map visualization
【Hot100】32. Longest valid bracket
使用3DMAX制作一枚手雷
简单易用的地图可视化
爬虫初级学习
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
Make a grenade with 3DMAX
[cloud native] what is the "grid" of service grid?
Weima, which is going to be listed, still can't give Baidu confidence
随机推荐
如何使用 wget 和 curl 下载文件
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
How to improve development quality
MVC mode and three-tier architecture
Implementation of shell script replacement function
Detectron2 installation method
2022年DCMM认证全国各地补贴政策汇总
用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
[211] go handles the detailed documents of Excel library
TCP两次挥手,你见过吗?那四次握手呢?
俄罗斯 Arenadata 发布基于PostgreSQL的产品
"In Vietnam, money is like lying on the street"
Large scale service exception log retrieval
Blue bridge: sympodial plant
蓝桥:合根植物
Is it safe to download the mobile version of Anxin securities and open an account online
【Hot100】32. Longest valid bracket
People in the workplace with a miserable expression
Superscalar processor design yaoyongbin Chapter 6 instruction decoding excerpt
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议