转载:求第k小的数、第k大的数、中位数的三种算法(nth_element()、快速选择算法、直接排序法)

转载:求第k小的数、第k大的数、中位数的三种算法(nth_element()、快速选择算法、直接排序法)

题目描述:给定一个n个元素的无序序列,求出第k小的数、第k大的数、中位数。
注意:其实要求出第k大的数,只需要求出第 (n+1)-k 小的数即可,中位数同理,如果n为偶数,就是求出(第 n/2 小的数+第 n/2+1 小的数) / 2,如果n为奇数,就是求出第 n/2 + 1小的数。

下面给出三种解法

  • nth_element()
  • 快速选择法
  • 直接排序法

方法1:nth_element()函数

利用c++11自带的函数nth_element(a1,a2,a3):比如给定一个范围a.begin() 和a.end()作为参数1和3,参数2为a.begin()+5,就能求出该数组第6小的数,其实也就是排个正序以后位于下标位置5的数。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e5+5;

int main()
{
/*
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
*/

vector &lt;int&gt; arr;
for(int i = 0; i &lt; 10; i++)
{
    arr.push_back(i+1);
}

cout &lt;&lt; "正序数组:" &lt;&lt; endl;
for(int i = 0; i &lt; 10; i++)
{
    cout &lt;&lt; arr[i] &lt;&lt; " ";
}
cout &lt;&lt; endl;

cout &lt;&lt; "乱序数组:" &lt;&lt; endl;
random_shuffle(arr.begin(),arr.end());
for(int i = 0; i &lt; 10; i++)
{
    cout &lt;&lt; arr[i] &lt;&lt; " ";
}
cout &lt;&lt; endl;

/** 方法1:调用nth_element()函数*/

/* 求第k小的数
for(int k = 1; k &lt;= 10; k++)
{
    cout &lt;&lt; "使得第" &lt;&lt; k &lt;&lt; "小的数,位于第" &lt;&lt; k  &lt;&lt; "个位置:" &lt;&lt; endl;
    nth_element(arr.begin(),arr.begin()+(k-1),arr.end());
    for(int i = 0; i &lt; 10; i++)
    {
        cout &lt;&lt; arr[i] &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
    cout &lt;&lt; "第" &lt;&lt; k &lt;&lt; "小的数为" &lt;&lt; arr[k-1] &lt;&lt; endl &lt;&lt; endl;
}*/

/* 求第k大的数
for(int k = 1; k &lt;= 10; k++)
{
    cout &lt;&lt; "使得第" &lt;&lt; k &lt;&lt; "大的数,位于第" &lt;&lt; 10-k+1  &lt;&lt; "个位置:" &lt;&lt; endl;
    // 第k大的数即为第n-k+1小的数
    nth_element(arr.begin(),arr.begin()+(10-k+1 -1),arr.end());
    for(int i = 0; i &lt; 10; i++)
    {
        cout &lt;&lt; arr[i] &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
    cout &lt;&lt; "第" &lt;&lt; k &lt;&lt; "大的数为" &lt;&lt; arr[10-k+1 -1] &lt;&lt; endl &lt;&lt; endl;
}*/

/* 求n个数的中位数
int n = 10;
double mid = 0;
if(n % 2 == 0)
{
    // 偶数个数,中位数:(第n/2大的数 + 第n/2+1大的数) / 2
    int k = n - (n/2) + 1;
    nth_element(arr.begin(),arr.begin()+(k-1),arr.end());
    mid += arr[k-1];
    k = n - (n/2+1) + 1;
    nth_element(arr.begin(),arr.begin()+(k-1),arr.end());
    mid += arr[k-1];
    mid /= 2.0;
}
else
{
    // 奇数个数,中位数:第n/2+1大的数
    int k = n - (n/2+1) + 1;// 对应的第k小的数
    nth_element(arr.begin(),arr.begin()+(k-1),arr.end());
    mid = arr[k-1];
}
cout &lt;&lt; mid &lt;&lt; endl;*/
return 0;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

方法2:快速选择法

其实和快速排序差不多,但是注意:只是思路差不太多,但是快排的时间复杂度为O(nlogn),快速选择算法的平均时间复杂度为O(n),最差时间复杂度和快速排序一样都是O(n2)。
具体思路:
每次Partition一次,返回得到一个分支点pivot,这个点对应的左边的元素小于 arr[pivot],右边的元素大于它,所以 arr[pivot] 对应的就是序列的第 pivot+1 小的元素。
此时要求第k小的元素:
如果k == pivot+1,直接返回即可,
如果k > pivot+1,说明要找的元素更大,就往右边递归,
如果k < pivot+1,说明要找的元素更小,就往左边递归。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e5+5;

void Swap(vector <int>& arr,int i,int j)
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}

int Partition(vector <int>& arr,int left,int right)
{
/* 以arr[right]作为划分基准
int j = left - 1;
for(int i = left; i < right; i++)
{
if (arr[i] <= arr[right])
Swap(arr, i, ++j);
}
Swap(arr,++j,right);
return j;
*/

/* 以arr[left]作为基准 */
int val = arr[left];
while(left &lt; right)
{
    while(left &lt; right &amp;&amp; arr[right] &gt;= val)
        right--;
    arr[left] = arr[right];
    while(left &lt; right &amp;&amp; arr[left] &lt;= val)
        left++;
    arr[right] = arr[left];
}
arr[left] = val;
return left;

}

int quickSelect(vector <int>& arr,int left,int right,int k)
{
// 在arr数组的left ~ right 的范围找出第k小的数
while(left <= right)
{
int pivot = Partition(arr,left,right);
// 比如k=5,那对应的第五小的数就在下标位置为4的地方(经过一次partition后,piovt左边的数都小于arr[pivot]、右边的数都大于arr[pivot])
if(k-1 == pivot)
{
return pivot;// pivot对应的位置就是第k小的元素对应的位置
}
if(k-1 > pivot)
{
left = pivot+1;// 要找大一点的元素,要在右边找
}
else
{
right = pivot-1;// 要找小一点的元素,要在左边找
}
}
return -1;
}

int main()
{
/*
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
*/

vector &lt;int&gt; arr;
for(int i = 0; i &lt; 10; i++)
{
    arr.push_back(i+1);
}

cout &lt;&lt; "正序数组:" &lt;&lt; endl;
for(int i = 0; i &lt; 10; i++)
{
    cout &lt;&lt; arr[i] &lt;&lt; " ";
}
cout &lt;&lt; endl;

cout &lt;&lt; "乱序数组:" &lt;&lt; endl;
random_shuffle(arr.begin(),arr.end());
for(int i = 0; i &lt; 10; i++)
{
    cout &lt;&lt; arr[i] &lt;&lt; " ";
}
cout &lt;&lt; endl;

/** 方法2:手写快速选择算法(1.应用了快速排序的思想 2.但其实就是跟nth_element差不多) */
/* 求第k小的数 */
int n = 10;
for(int k = 1; k &lt;= n; k++)
{
    cout &lt;&lt; "第" &lt;&lt; k &lt;&lt; "小的数为" &lt;&lt; arr[quickSelect(arr,0,9,k)] &lt;&lt; endl;
    for(int i = 0; i &lt; n; i++)
    {
        cout &lt;&lt; arr[i] &lt;&lt; " ";
    }
    cout &lt;&lt; endl &lt;&lt; endl;
}

/* 求第k大的数 */
for(int k = 1; k &lt;= n; k++)
{
    cout &lt;&lt; "第" &lt;&lt; k &lt;&lt; "大的数为" &lt;&lt; arr[quickSelect(arr,0,9,(n+1)-k)] &lt;&lt; endl &lt;&lt; endl;
    for(int i = 0; i &lt; n; i++)
    {
        cout &lt;&lt; arr[i] &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
}

/*  求中位数:
    奇数,n/2+1小的数
    偶数:(n/2小的数 + n/2+1小的数) / 2
*/
double mid;
if(n % 2 == 0)
{
    double tmp1 = arr[quickSelect(arr,0,n-1,n/2)];
    double tmp2 = arr[quickSelect(arr,0,n-1,n/2 + 1)];
    mid = (tmp1+tmp2)/2;
}
else
{
    mid = arr[quickSelect(arr,0,n-1,n/2 + 1)];
}
cout &lt;&lt; mid &lt;&lt; endl;


return 0;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159

方法3:

将数组排个序就可以了,然后直接O(1)取值,不过排序的时间复杂度也要算进来,快排的话就是O(nlogn),我就不写了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值