当前位置:网站首页>力扣刷题日记/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;
}
}
边栏推荐
- Weima, which is going to be listed, still can't give Baidu confidence
- General environmental instructions for the project
- 俄罗斯 Arenadata 发布基于PostgreSQL的产品
- ISO27001 certification process and 2022 subsidy policy summary
- LD_LIBRARY_PATH 环境变量设置
- Talk about seven ways to realize asynchronous programming
- People in the workplace with a miserable expression
- Interview summary of large factory Daquan II
- Dynamic programming stock problem comparison
- Russia arena data releases PostgreSQL based products
猜你喜欢
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
7 RSA Cryptosystem
爬虫初级学习
Self reflection of a small VC after two years of entrepreneurship
Talk about seven ways to realize asynchronous programming
2022 national CMMI certification subsidy policy | Changxu consulting
Numpy 的仿制 2
The controversial line of energy replenishment: will fast charging lead to reunification?
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)
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
随机推荐
Grain Mall (I)
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
Heartless sword Chinese translation of Elizabeth Bishop's a skill
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
【Hot100】31. Next spread
爬虫初级学习
Introduction of time related knowledge in kernel
Make a grenade with 3DMAX
超标量处理器设计 姚永斌 第6章 指令解码 摘录
ISO27001认证办理流程及2022年补贴政策汇总
Face_ Attendance statistics of recognition face recognition
使用3DMAX制作一枚手雷
[211] go handles the detailed documents of Excel library
TCP waves twice, have you seen it? What about four handshakes?
Load test practice of pingcode performance test
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
Machine learning concept drift detection method (Apria)
Easy to use map visualization
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
7 RSA Cryptosystem