最小K个数

来源:互联网 发布:php抓取百度搜索结果 编辑:程序博客网 时间:2024/05/16 15:13
#include<iostream>using namespace std;int partition(int a[],int low,int high){int prio = a[low];while (low < high){while (low < high && a[high] >= prio) high--;swap(a[high], a[low]);while (low < high && a[low] <= prio) low++;swap(a[high], a[low]);}return low;}void quicksort(int a[], int low,int high){if (low < high){int mid = partition(a, low, high);quicksort(a, 0, mid - 1);quicksort(a, mid + 1, high);}}void getLeastNum(int input[], int n, int *output, int k){int start = 0;int end = n - 1;int index = partition(input, start, end);while (index != k - 1){if (index > k - 1){end = index + 1;index = partition(input, start, end);}else{start = index + 1;index = partition(input, start, end);}}for (int i = 0; i < k; i++){output[i] = input[i];cout << input[i] << endl;}}void main(){int a[] = { 5,3,1,7,9,0,2,4,6,8 };int b[10];getLeastNum(a, 10, b, 2);/*for (int i = 0; i < 10;i++)cout << a[i] << endl;*/system("pause");}

0 0
原创粉丝点击