2012年10月5日

找出无序数组中第k小的数

摘要: 题目描述:给定一个无序整数数组,返回这个数组中第k小的数。解析:最平常的思路是将数组排序,最快的排序是快排,然后返回已排序数组的第k个数,算法时间复杂度为O(nlogn),空间复杂度为O(1)。使用快排的思想,但是每次只对patition之后的数组的一半递归,这样可以将时间复杂度将为O(n)。代码实现:#include <iostream>#include <string>#include <cstring>#include <vector>#include <algorithm>using namespace std;void sw 阅读全文

posted @ 2012-10-05 01:37 as_ 阅读(4428) 评论(3) 推荐(2) 编辑

导航