当前位置:网站首页>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
边栏推荐
- Difference between like and regexp
- Llvm - generate for loop
- Mds55-16-asemi rectifier module mds55-16
- [thymeleaf] handle null values and use safe operators
- 让阿里P8都为之着迷的分布式核心原理解析到底讲了啥?看完我惊了
- How to upload large files quickly?
- Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
- How to design a good API interface?
- Adobe Illustrator menu in Chinese and English
- Error: unable to find remote key "17f718f726"“
猜你喜欢

Introduction to distributed transaction Seata
![[NLP] HMM hidden Markov + Viterbi word segmentation](/img/9a/b39a166320c2f2001f10913f789c90.png)
[NLP] HMM hidden Markov + Viterbi word segmentation

如何设计一个良好的API接口?

Leetcode151 - invert words in string - String - simulation
![Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]](/img/d4/9ee62772b42fa77dfd68a41bde1371.png)
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
![Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]](/img/c6/5241de0d670da3dae136a3047c6160.jpg)
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]

setcontext getcontext makecontext swapcontext

Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
![Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]](/img/07/c534238c2b5405bbe4655e51cfee51.png)
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]

8.5 concise implementation of cyclic neural network
随机推荐
How to design a good API interface?
Go basic reflection
Leetcode exercise - 396 Rotation function
Nuxt project: Global get process Env information
adobe illustrator 菜单中英文对照
JUC学习记录(2022.4.22)
async关键字
Unity_ Code mode add binding button click event
MySQL sync could not find first log file name in binary log index file error
Async keyword
eolink 如何助力遠程辦公
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
My raspberry PI zero 2W toss notes to record some problems and solutions
Introduction to dirty reading, unrepeatable reading and phantom reading
Five data types of redis
Error: unable to find remote key "17f718f726"“
Thread synchronization, life cycle
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
如何设计一个良好的API接口?