当前位置:网站首页>力扣刷题日记/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;
}
}
边栏推荐
- 俄罗斯 Arenadata 发布基于PostgreSQL的产品
- 提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
- [system disk back to U disk] record the operation of system disk back to U disk
- 超标量处理器设计 姚永斌 第6章 指令解码 摘录
- 爬虫初级学习
- Dynamic programming stock problem comparison
- Imitation of numpy 2
- Talk about seven ways to realize asynchronous programming
- 【系统盘转回U盘】记录系统盘转回U盘的操作
- How to test MDM products
猜你喜欢

7 RSA Cryptosystem

78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return

Weima, which is going to be listed, still can't give Baidu confidence

Talk about seven ways to realize asynchronous programming

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

The controversial line of energy replenishment: will fast charging lead to reunification?

比李嘉诚还有钱的币圈大佬,刚在沙特买了楼

DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply

“在越南,钱就像躺在街上”

The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
随机推荐
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
High school physics: force, object and balance
Dynamic programming stock problem comparison
【Hot100】31. 下一个排列
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
内核中时间相关的知识介绍
MySQL common add, delete, modify and query operations (crud)
[cloud native] what is the "grid" of service grid?
RecastNavigation 之 Recast
regular expression
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
如何提高开发质量
Clever use of curl command
[system disk back to U disk] record the operation of system disk back to U disk
Redis master-slave replication
uni-app与uviewUI实现仿小米商城app(附源码)
ITSS运维能力成熟度分级详解|一文搞清ITSS证书
线上MySQL的自增id用尽怎么办?
【每日一题】556. 下一个更大元素 III
“在越南,钱就像躺在街上”