Home > front end >  Front end algorithm: input n integers, find the minimum number of K. Enter 4,5,1,6,2,7,3,8 this eigh
Front end algorithm: input n integers, find the minimum number of K. Enter 4,5,1,6,2,7,3,8 this eigh

Time:01-07

 
The function sortMin (input, k) {
If (k & gt; Input the length | | k==0) {
Return [];
}
let arr=[];//the smallest number inside
Arr=searchMin (input, 0, input length 1, k). The slice (0, k);
return arr;
}
//by using local quick sort (since)
The function quickSort (a, start=0, end=a. ength - 1) {

Let I=start;
Let j=end;
Let baseVal=a [end];
While (I & lt; J) {
While (I & lt; J & amp; & A [I] <={baseVal)
i++;
}
A [j]=a, [I].
While (j & gt; I & amp; & A [j] & gt;={baseVal)
j--;
}
A [I]=a, [j].
}
A [j]=baseVal;
Return j;
}

Function searchMin (input, start=0, end=input. The length 1, k) {
If (start & gt; {
=end)return;
}

Let the index=quickSort (input, start, end);
console.log(index);
//if k is a sentinel
If (k & lt; Index + 1)={
Input. Slice (0, k);
//the console. The log (arr);
Input. Sort ((a, b)=& gt; A - b);
} else {
Return searchMin (input, index + 1, end, k);
}
Return the input;
}

CodePudding user response:

This problem is the most efficient solution should be made the most of the priority queue,

Of course, regardless of the efficiency of ordering the whole array, before taking k element