输入n个整数,找出其中最小的K个数
package interview_offer_b;
import java.util.Arrays;
public class FTopK {
public int[] topK(int[] a, int k){
int len = a.length;
if(len<1 || k<1||k>len)
return null;
Arrays.sort(a);
int[] acopy = new int[k];
System.arraycopy(a, 0, acopy, 0, k);
return acopy;
}
public int[] topK1(int[] a, int k){
int len = a.length;
if(len<1 || k<1||k>len)
return null;
for(int j=0;j<k;j++){
int min = j;
for(int i=j+1;i<len;i++){
if(a[i]<a[min]){
min = i;
}
}
swap(a,j,min);
}
int[] acopy = new int[k];
System.arraycopy(a, 0, acopy, 0, k);
return acopy;
}
public int[] topK2(int[] a, int k){
int len = a.length;
if(len<1 || k<1||k>len)
return null;
int low = 0;
int high = len-1;
int pos = partition(a,low,high);
int[] res = new int[k];
if(k-1==pos){
System.arraycopy(a, 0, res, 0, k);
return res;
}
while(k-1<pos){
pos = partition(a,low,pos-1);
}
while(k-1>pos){
pos = partition(a,pos+1,high);
}
System.arraycopy(a, 0, res, 0, k);
return res;
}
private int partition(int[] a, int low, int high){
int tmp = a[low];
int i = low;
int j = high;
while(i<j){
while(i<j && a[i]<=tmp)
i++;
while(a[j]>tmp)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,low,j);
return j;
}
private void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
public int[] topK3(int[] a, int k){
int len = a.length;
if(len<1 || k<1||k>len)
return null;
int[] heap = this.createHeap(a, k);
for(int i=k;i<len;i++){
if(a[i]<heap[0]){
this.insert(heap, a[i]);
}
}
return heap;
}
private void insert(int[] a, int v){
a[0] = v;
int parent = 0;
int lena = a.length;
while(parent<lena){
int lchild = 2*parent+1;
int rchild = 2*parent+2;
int maxIndex = parent;
if(lchild<lena&&a[lchild]>a[parent])
maxIndex = lchild;
if(rchild<lena&&a[rchild]>a[maxIndex])
maxIndex = rchild;
if(maxIndex==parent)
break;
else{
int tmp = a[parent];
a[parent] = a[maxIndex];
a[maxIndex] = tmp;
}
}
}
private int[] createHeap(int[] a, int k){
int[] res = new int[k];
for(int i=0;i<k;i++){
res[i] = a[i];
}
for(int j=1;j<k;j++){
int child = j;
int parent = (j-1)/2;
int tmp = res[j];
while(parent>=0&&child!=0&&res[parent]<tmp){
res[child] = res[parent];
child = parent;
parent = (parent-1)/2;
}
res[child] = tmp;
}
return res;
}
private void printArray(int[] a){
if(a==null)
return;
int len = a.length;
for(int i=0;i<len;i++){
System.out.print(a[i]);
if(i!=len-1)
System.out.print(",");
}
}
public static void main(String[] args) {
FTopK tk = new FTopK();
System.out.println("------topK-----");
int[] a = {4,5,1,6,2,7,3,8};
System.out.println("before sort:");
tk.printArray(a);
int[] res = tk.topK(a,4);
System.out.println("\nafter sort:");
tk.printArray(res);
System.out.println("\n-----topK1-----");
int[] a1 = {4,5,1,6,2,7,3,8};
System.out.println("before sort:");
tk.printArray(a1);
int[] res1 = tk.topK1(a1,4);
System.out.println("\nafter sort:");
tk.printArray(res1);
System.out.println("\n-----topK2-----");
int[] a2 = {5,5,1,6,9,7,3,12};
System.out.println("before sort:");
tk.printArray(a2);
int[] res2 = tk.topK2(a2,4);
System.out.println("\nafter sort:");
tk.printArray(res2);
System.out.println("\n-----topK3-----");
int[] a3 = {5,5,1,6,9,7,3,12};
System.out.println("before sort:");
tk.printArray(a3);
int[] res3 = tk.topK3(a3, 4);
System.out.println("\nafter sort:");
tk.printArray(res3);
}
}