当前位置:网站首页>力扣刷题日记/day8/7.1
力扣刷题日记/day8/7.1
2022-07-04 16:33:00 【bobo洁厕灵】
新手村
冒泡排序:
实现数组中的元素按照数值大小有序排列(升序,降序)
实现思想:(以降序为例)
1.将第一个数据和第二个数据相比较,如果第一个数据小于第二个数据,将两个数据交换位置
2.指针由第一个数据指向第二个数据,第二个数据和第三个数据相比较,如果第二个数据小于第三个数据,将两个数据交换位置
3.依次类推,完成第一轮排序,第一轮排序结束后,最大的元素被移到了最右边
4.重复以上过程,每排完一轮,比较的次数就少一次
编码思路:需要两层循环,第一层循环i表示排序的轮数,第二层循环j表示比较的次数
例如简一点的冒泡排序,将第一个数字和后面的数字逐个比较大小,如果小于,则互换位置,大于则不动。此时,第一个数为数组中的最大数。然后再将第二个数与后面的数逐个比较,以次类推。
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; // 两个数交换位置
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
}
例题:

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;
}
}
边栏推荐
- 超标量处理器设计 姚永斌 第5章 指令集体系 摘录
- Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
- 同事悄悄告诉我,飞书通知还能这样玩
- Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
- Recast of recastnavigation
- 项目通用环境使用说明
- 谷粒商城(一)
- Stars open stores, return, return, return
- Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
- Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
猜你喜欢

机器学习概念漂移检测方法(Aporia)

Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection

"In Vietnam, money is like lying on the street"

【Hot100】32. Longest valid bracket

TCP waves twice, have you seen it? What about four handshakes?

使用3DMAX制作一枚手雷

超标量处理器设计 姚永斌 第6章 指令解码 摘录

78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报

创业两年,一家小VC的自我反思

How to improve development quality
随机推荐
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
数学分析_笔记_第7章:多元函数的微分学
ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
Wuzhicms code audit
无心剑中译伊丽莎白·毕肖普《一门技艺》
Load test practice of pingcode performance test
android使用SQLiteOpenHelper闪退
Easy to use map visualization
怎么开户才是安全的,
Redis master-slave replication
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
Android uses sqliteopenhelper to flash back
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
华为云ModelArts的使用教程(附详细图解)
【Hot100】31. 下一个排列
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
Russia arena data releases PostgreSQL based products
“在越南,钱就像躺在街上”