当前位置:网站首页>Byte interview programming question: the minimum number of K
Byte interview programming question: the minimum number of K
2022-04-23 15:09:00 【White horse is not a horse·】
1. Fast algorithm
Front of output k The lowest decimal , All ordered , The time complexity is O(nlogn), Space for O(logn)
package demo1;
import java.util.Arrays;
public class demo2 {
public static void main(String[] args) {
// Fast algorithm
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){
// A special case
if(first>last) return ;
// General situation
//1) Find a random number , Exchange on this basis
int left=first;
int right=last;
int temp=data[first]; // The selected benchmark
while(left<right){
//1) Find the object to exchange ( If the left side is the datum , So start on the right and look for )
while(left<right && data[right]>temp) right--;
while(left<right && data[left]<=temp) left++;
//2) Exchange when you find it
if(left<right){
int temp1=data[left];
data[left]=data[right];
data[right]=temp1;
}
}
// Change the position of the benchmark
data[first]=data[left];
data[left]=temp;
// Continue recursion ( With left For boundaries )
if(left+1<k){
fastsort(data,left+2,last, k-left-1);
}else if(left==k){
return;
}else{
fastsort(data,first,left,k);
}
}
}
2. Heap sort
1) Small top heap sorting algorithm ( Through an array , Big pile top , A complete binary tree with child nodes greater than or equal to child nodes )
2) Call to realize the collection of large top heap PriorityQueue, The default is small top heap , Rewritable Comparator Class in compare, Realize the big top reactor .
3) The time complexity is O(nlogn), Spatial complexity 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. Improved fast scheduling algorithm
- Fast scheduling algorithm ( Front of output k The lowest decimal , disorder ), The time complexity is O(n), Space is a constant space
- Learning from the idea of fast platoon , There are multiple choices in recursive operations
package demo1;
import java.util.Arrays;
public class demo3 {
public static void main(String[] args) {
// rewrite : Fast algorithm
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){
// A special case
if(begin>end) return;
// General situation
int left=begin;
int right=end;
int temp=begin;
while(left<right){
// Find something to exchange ( The reference value is... On the left , Look, start on the right )
while(left<right && data[right]>data[temp]) right--;
while(left<right && data[left]<=data[temp]) left++;
// Find it and exchange it
if(left<right){
int tem1=data[right];
data[right]=data[left];
data[left]=tem1;
}
}
// Then the reference value is placed in the position where it should be placed
int tem2=data[left];
data[left]=data[begin];
data[begin]=tem2;
System.out.println("data="+ Arrays.toString(data));
// Perform the following recursive operations ( Core code , Finding the middle position is the reference value
if(left+1<k){
fastsort(data,left+1,end,k);
}else if(left==k || left+1==k ){
return;
}else{
fastsort(data,begin,left,k);
}
}
}
It's easy to get wrong :
1) In the method , First of all, it should be right begin>end Discuss the situation
2) When the appropriate exchange value is found , If the reference value is the first , Then start from the right , Then look to the left
3) Notice the case of recursive selection .
版权声明
本文为[White horse is not a horse·]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231407525012.html
边栏推荐
- LeetCode165-比较版本号-双指针-字符串
- Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
- SSH connects to the remote host through the springboard machine
- [stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
- C语言超全学习路线(收藏让你少走弯路)
- Set onedrive or Google drive as a drawing bed in upic for free
- Adobe Illustrator menu in Chinese and English
- Thread synchronization, life cycle
- LeetCode 练习——396. 旋转函数
- win10 任务栏通知区图标不见了
猜你喜欢
Tun model of flannel principle
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
8.3 language model and data set
Leetcode151 - invert words in string - String - simulation
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
Set onedrive or Google drive as a drawing bed in upic for free
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
MySQL error packet out of order
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
Lotus DB design and Implementation - 1 Basic Concepts
随机推荐
Modify the default listening IP of firebase emulators
Flink DataStream 类型系统 TypeInformation
Three uses of kprobe
Llvm - generate if else and pH
Leetcode exercise - 396 Rotation function
牛客网数据库SQL实战详细剖析(26-30)
【thymeleaf】处理空值和使用安全操作符
脏读、不可重复读和幻读介绍
1n5408-asemi rectifier diode
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
冰冰学习笔记:一步一步带你实现顺序表
Basic operation of sequential stack
Have you really learned the operation of sequence table?
LeetCode162-寻找峰值-二分-数组
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
Nacos program connects to mysql8 0+ NullPointerException
Async void caused the program to crash
API gateway / API gateway (II) - use of Kong - load balancing
买卖股票的最佳时机系列问题
JUC learning record (2022.4.22)