当前位置:网站首页>快速排序,查询序列的第K大的数
快速排序,查询序列的第K大的数
2022-06-29 02:46:00 【疯疯癫癫才自由】
主元:主元左边的元素不大于它,右边的元素都大于它
int randPartition(int *a,int l,int r) //随机选择主元,避免快速排序退化到O(n^2)
{
srand((unsigned int) time(NULL)); //初始化随机数列,防止每次rand()返回相同的值
int pos=rand()/(RAND_MAX*1.0)*(r-l)+l; // pos=[l,r);
swap(a[l],a[pos]);
int temp=a[l];
while(l<r) //基于two point 的思想
{
while(l<r&&a[r]>temp)
--r;
a[l]=a[r];
while(l<r&&a[l]<=temp)
++l;
a[r]=a[l];
}
a[l]=temp;
return l;}
int rand(void):stdlib.h下的函数,返
回一个0到rand_max之间的伪随机数;
为了避免程序每次运行获得相同的随机数列,可以调用srand函数,用他的参数对
随机数发生器进行初始化,一般是srand((unsigned int) time(NULL));
time 函数:
time_t time(time_t timer):
当timer是NULL时,得到当前时间(从1970-01-01 00:00:00 到现在的秒数);
void quicksort(int *a,int l,int r) //调用随机选择主元函数进行快速排序
{
if(l<r) //当前区间的长度二分
{
int pos=randPartition(a,l,r);
quicksort(a,l,pos-1); //pos左边的序列进行排序
quicksort(a,pos+1,r); //pos右边的元素进行排序
}
}
int randselect(int *a,int l, int r,int k) //从序列中选择第k大的数,时间复杂度O(n),是一个非常出色的算法了
{
if(l==r)
return a[l]; //递归边界,这一步是必须要的,否则当查询的k比序列长度还大的时候,会造成错误
int pos=randPartition(a,l,r);
int th=pos-l+1; //此时的第pos号位是该序列的第 th 大
if(th==k)
return a[pos];
else if(th<k)
randselect(a,pos+1,r,k-th);
else
randselect(a,l,pos-1,k);
}
/**
3)快速排序
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
/**
int my_partition(int *a,int l,int r) //选择第一个元素作为主元进行排序,主元左边的元素不大于它,右边的元素都大于它
{
int temp=a[l];
while(l<r)
{
while(l<r&&a[r]>temp)
--r;
a[l]=a[r];
while(l<r&&a[l]<=temp)
++l;
a[r]=a[l];
}
a[l]=temp;
return l;
}
*/
//void quicksort(int *a,int l,int r) //调用主元函数进行快速排序
//{
// if(l<r)
// {
// int pos=my_partition(a,l,r);
// quicksort(a,l,pos-1);
// quicksort(a,pos+1,r);
// }
//}
/**
int rand(void):stdlib.h下的函数,返回一个0到rand_max之间的伪随机数;
为了避免程序每次运行获得相同的随机数列,可以调用srand函数,用他的参数对
随机数发生器进行初始化,一般是srand((unsigned int) time(NULL));
time 函数:
time_t time(time_t timer):
当timer是NULL时,得到当前时间(从1970-01-01 00:00:00 到现在的秒数);
*/
int randPartition(int *a,int l,int r) //随机选择主元,避免快速排序退化到O(n^2)
{
srand((unsigned int) time(NULL)); //初始化随机数列,防止每次rand()返回相同的值
int pos=rand()/(RAND_MAX*1.0)*(r-l)+l; // pos=[l,r);
swap(a[l],a[pos]);
int temp=a[l];
while(l<r)
{
while(l<r&&a[r]>temp)
--r;
a[l]=a[r];
while(l<r&&a[l]<=temp)
++l;
a[r]=a[l];
}
a[l]=temp;
return l;
}
void quicksort(int *a,int l,int r) //调用随机选择相主元函数进行快速排序
{
if(l<r)
{
int pos=randPartition(a,l,r);
quicksort(a,l,pos-1);
quicksort(a,pos+1,r);
}
}
int randselect(int *a,int l, int r,int k) //从序列中选择第k大的数,时间复杂度O(n),是一个非常出色的算法了
{
if(l==r)
return a[l]; //递归边界,这一步是必须要的,否则当查询的k比序列长度还大的时候,会造成错误
int pos=randPartition(a,l,r);
int th=pos-l+1;
if(th==k)
return a[pos];
else if(th<k)
randselect(a,pos+1,r,k-th);
else
randselect(a,l,pos-1,k);
}
int main()
{
int n;
cin >> n;
int a[n];
for(int i=0;i<n;++i)
cin >> a[i];
cout << randselect(a,0,n-1,11) << endl;
quicksort(a,0,n-1);
for(auto b: a)
cout << b<< endl;
cout << "Hello world!" << endl;
return 0;
}下面给一个例题:
题目:给定一个由整数组成的集合,集合中的整数各不相同,现在要把它分成两个集合,
使得这两个集合的并为原集合,交为空集,同时要使两个子集合的元素个数n1和n2的绝对值
尽可能小,各自的元素之和尽可能大。输出两个集合各自元素的和的差的绝对值。
data:
13
1 6 33 18 4 0 10 5 12 7 2 9 3
分析:当N是偶数时,集合各自选排好序时的前一半和后一半作为自身的元素;当是奇数时,判读第 (N+1)/2 是正数还是负数,若是正数,则放在后一个集合中,若是负数,则放在
前一个集合当中。
/**
题目:给定一个由整数组成的集合,集合中的整数各不相同,现在要把它分成两个集合,
使得这两个集合的并为原集合,交为空集,同时要使两个子集合的元素个数n1和n2的绝对值
尽可能小,各自的元素之和尽可能大。输出两个集合各自元素的和的差的绝对值。
分析:当N是偶数时,集合各自选排好序时的前一半和后一半作为自身的元素;当是奇数时,
判读第 (N+1)/2 是正数还是负数,若是正数,则放在后一个集合中,若是负数,则放在
前一个集合当中
data:
13
1 6 33 18 4 0 10 5 12 7 2 9 3
*/
/**
3)快速排序
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
/**
int rand(void):stdlib.h下的函数,返回一个0到rand_max之间的伪随机数;
为了避免程序每次运行获得相同的随机数列,可以调用srand函数,用他的参数对
随机数发生器进行初始化,一般是srand((unsigned int) time(NULL));
time 函数:
time_t time(time_t timer):
当timer是NULL时,得到当前时间(从1970-01-01 00:00:00 到现在的秒数);
*/
int randPartition(int *a,int l,int r) //随机选择主元,避免快速排序退化到O(n^2)
{
srand((unsigned int) time(NULL)); //初始化随机数列,防止每次rand()返回相同的值
int pos=rand()/(RAND_MAX*1.0)*(r-l)+l; // pos=[l,r);
swap(a[l],a[pos]);
int temp=a[l];
while(l<r)
{
while(l<r&&a[r]>temp)
--r;
a[l]=a[r];
while(l<r&&a[l]<=temp)
++l;
a[r]=a[l];
}
a[l]=temp;
return l;
}
/**这个函数是不需要的
void quicksort(int *a,int l,int r) //调用随机选择相主元函数进行快速排序
{
if(l<r)
{
int pos=randPartition(a,l,r);
quicksort(a,l,pos-1);
quicksort(a,pos+1,r);
}
}
*/
int randselect(int *a,int l, int r,int k) //从序列中选择第k大的数,时间复杂度O(n),是一个非常出色的算法了
{
if(l==r)
return a[l]; //递归边界,这一步是必须要的,否则当查询的k比序列长度还大的时候,会造成错误
int pos=randPartition(a,l,r);
int th=pos-l+1;
if(th==k)
return a[pos];
else if(th<k)
return randselect(a,pos+1,r,k-th);
else
return randselect(a,l,pos-1,k);
}
int main()
{
int n,sum=0;
cin >> n;
int a[n];
for(int i=0;i<n;++i)
cin >> a[i];
if(n&1)
{
int k=(n+1)/2;
int val=randselect(a,0,n-1,k);
if(val>0)
{
for(int i=k-1;i<n;++i) //这儿需要特别注意,下标是从0开始的
sum+=a[i];
for(int i=0;i<k-1;++i)
sum-=a[i];
}
else
{
for(int i=k;i<n;++i)
sum+=a[i];
for(int i=0;i<k;++i)
sum-=a[i];
}
}
else
{
int k=n/2;
randselect(a,0,n-1,k);
for(int i=k;i<n;++i)
sum+=a[i];
for(int i=0;i<k;++i)
sum-=a[i];
}
cout << sum << endl;
for(auto b: a)
cout << b<< endl;
return 0;
}
边栏推荐
- Install kibana
- PWN攻防世界guess_num
- PMP Business Analysis Overview
- mgalcu-a509
- Eight difficulties of embedded C language
- Sysbench Pressure Test Oracle (installation and use examples)
- 99 multiplication table
- PWN attack and defense world level2
- The meaning of cross multiplication and dot multiplication (simple formula memory method)
- 【無標題】
猜你喜欢
![[线性代数] 1.1 二阶与三阶行列式](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[线性代数] 1.1 二阶与三阶行列式

深入解析 Apache BookKeeper 系列:第三篇——读取原理

Understanding and design of high concurrency

【無標題】

thinkphp5.1 runtime文件改成777权限了, 还是无法写入

對補wasm環境的一些測試

Use photoshop2022 to create a wonderful gradient effect for pictures

"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
![[linear algebra] 1.2 total permutation and commutation](/img/04/18fc358c6c426e10c8598bcee9cd43.png)
[linear algebra] 1.2 total permutation and commutation

LabVIEW generate application (exe) and installer
随机推荐
Google Maps API v3~ simply turn off infoindow- Google Map API v3 ~ Simply Close an infowindow?
Talk about SQL optimization
PHP SimpleXML
Day10 enumeration class and annotation
Pytoch Learning Series: Introduction
手机开户股票开户安全吗?开户很难么?
Ctfhub web SQL injection - integer injection
Install kibana
matlab习题 —— 图像绘制练习
微信小程序自定义组件
Bluetooth solution | Lenz technology Amazon direct connected magic lantern solution
[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre
18. `bs對象.節點名.next_sibling` 獲取兄弟節點
安装mysql5.7 并修改密码
矩阵特征值和特征向量求解——特征值分解(EVD)
Use photoshop2022 to create a wonderful gradient effect for pictures
The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written
String method exercise
Mipi d-phy -- contents of HS and LP agreements
How to optimize databases and tables