当前位置:网站首页>字节面试编程题:最小的K个数
字节面试编程题:最小的K个数
2022-04-23 14:08:00 【白马非马·】
1. 快排算法
输出的前k个最小数,均有序,时间复杂度为O(nlogn),空间为O(logn)
package demo1;
import java.util.Arrays;
public class demo2 {
public static void main(String[] args) {
//快排算法
int[] data=new int[]{
1,4,5,3,2,6,7,9,8};
int k=5;
System.out.println(Arrays.toString(data));
fastsort(data,0,data.length-1,k);
System.out.println(Arrays.toString(data));
}
public static void fastsort(int[] data,int first,int last,int k){
//特殊情况
if(first>last) return ;
//一般情况
//1)找到一个随机数,以此为基准进行交换
int left=first;
int right=last;
int temp=data[first]; //挑选出来的基准
while(left<right){
//1)找到需要交换对象(如果左边为基准,那么从右边开始寻找)
while(left<right && data[right]>temp) right--;
while(left<right && data[left]<=temp) left++;
//2)找到之后就进行交换
if(left<right){
int temp1=data[left];
data[left]=data[right];
data[right]=temp1;
}
}
//把基准的位置也换一下
data[first]=data[left];
data[left]=temp;
//继续递归下去(以left为界限)
if(left+1<k){
fastsort(data,left+2,last, k-left-1);
}else if(left==k){
return;
}else{
fastsort(data,first,left,k);
}
}
}
2. 堆排序
1) 小顶堆排序算法(通过一个数组,大顶堆,子节点大于等于孩子节点的完全二叉树)
2)调用实现了大顶堆的集合PriorityQueue,默认为小顶堆,可重写Comparator中的类compare,实现大顶堆。
3)时间复杂度为O(nlogn),空间复杂度O(1)
package demo1;
import java.util.*;
public class demo4 {
public static void main(String[] args) {
List<Integer> list=new ArrayList<>(Arrays.asList(2,3,1,5,7,6,9,8,0));
int k=8;
PriorityQueue<Integer> queue=new PriorityQueue<>(new Comparator<Integer>(){
@Override
public int compare(Integer a,Integer b){
return a-b;
}
});
for(Integer i:list) queue.add(i);
System.out.println(queue);
for(int i=0;i<k;i++) System.out.print(queue.poll()+" ");
}
}
3. 快排改进算法
- 快排思想算法(输出的前k个最小数,无序),时间复杂度为O(n),空间为常量空间
- 借鉴了快排思想,在递归操作那里多了个选择
package demo1;
import java.util.Arrays;
public class demo3 {
public static void main(String[] args) {
//重写:快排算法
int[] data=new int[]{
2,6,7,9,8,1,4,5,3};
//int[] data=new int[]{3,2,4,1,5,8,7};
int k=4;
fastsort(data,0,data.length-1,k);
System.out.println("data="+ Arrays.toString(data));
int[] data1=Arrays.copyOfRange(data,0,k);
System.out.println("result="+ Arrays.toString(data1));
}
public static void fastsort(int[]data,int begin,int end,int k){
//特殊情况
if(begin>end) return;
//一般情况
int left=begin;
int right=end;
int temp=begin;
while(left<right){
//寻找到可以交换的(参考值为左边的,寻找从右边开始)
while(left<right && data[right]>data[temp]) right--;
while(left<right && data[left]<=data[temp]) left++;
//寻找到之后进行交换
if(left<right){
int tem1=data[right];
data[right]=data[left];
data[left]=tem1;
}
}
//然后参考值放到该放的位置上
int tem2=data[left];
data[left]=data[begin];
data[begin]=tem2;
System.out.println("data="+ Arrays.toString(data));
//进行下面的递归操作(核心代码,找到中间位置就是参考值
if(left+1<k){
fastsort(data,left+1,end,k);
}else if(left==k || left+1==k ){
return;
}else{
fastsort(data,begin,left,k);
}
}
}
易错点:
1)在方法中,首先应该对begin>end情况进行讨论
2)在找到合适交换值时,如果参考值为第一个,那么就从右边开始找,然后找左边
3)注意一下递归选择的情况。
版权声明
本文为[白马非马·]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42974034/article/details/124181969
边栏推荐
- JDBC和servlet写CRUD的接口总结
- redis数据库讲解二(redis高可用、持久化、性能管理)
- Some good articles on pthread multithreading
- RobotFramework 之 公共变量
- 云容灾是什么意思?云容灾和传统容灾的区别?
- 帆软中使用if else 进行判断-使用标题条件进行判断
- On September 8, the night before going to Songshan Lake
- Wechat applet communicates with esp8266 based on UDP protocol
- 01-nio basic ByteBuffer and filechannel
- Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)
猜你喜欢
云迁移的六大场景
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
MYSQL 主从同步避坑版教程
Easyexcel读取excel表地理位置数据,按中文拼音排序
Subscription number development of wechat applet (message push)
利用json-server在本地创建服务器请求
Jmeter设置环境变量支持在任意终端目录输入jmeter直接启动
RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
About the configuration and use of json5 in nodejs
随机推荐
RobotFramework 之 文件上传和下载
Wechat applet communicates with low-power Bluetooth - receives data sent by hardware (IV)
关于Jmeter启动闪退问题
JDBC和servlet写CRUD的接口总结
不同时间类型的执行计划计算
Easyexcel读取excel表地理位置数据,按中文拼音排序
mysql 5.1升级到5.66
Detailed tutorial on the use of setinterval timing function of wechat applet
Logback logger and root
关于密匙传递的安全性和数字签名
帆软中需要设置合计值为0时,一整行都不显示的解决办法
云容灾是什么意思?云容灾和传统容灾的区别?
krpano全景之vtour文件夹和tour
利用json-server在本地创建服务器请求
帆软之单元格部分字体变颜色
使用Executors类快速创建线程池
openstack理论知识
VMware 15pro mounts the hard disk of the real computer in the deepin system
Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)