当前位置:网站首页>Quick sort, query the k-largest number of the sequence
Quick sort, query the k-largest number of the sequence
2022-06-29 02:48:00 【Madness makes freedom】
Principal component : The element on the left of the primary element is no larger than it , The elements on the right are larger than it
int randPartition(int *a,int l,int r) // Select the principal component randomly , Avoid a quick sort that degenerates into O(n^2)
{
srand((unsigned int) time(NULL)); // Initialize random sequence , Prevent every rand() Return the same value
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) // be based on two point Thought
{
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 Next function , return
Go back to one 0 To rand_max Between pseudorandom numbers ;
To avoid getting the same sequence of random numbers every time the program runs , You can call srand function , With his parameters
The random number generator is initialized , It's usually srand((unsigned int) time(NULL));
time function :
time_t time(time_t timer):
When timer yes NULL when , Get the current time ( from 1970-01-01 00:00:00 The number of seconds to now );
void quicksort(int *a,int l,int r) // Call the randomly selected primary function for quick sorting
{
if(l<r) // The length of the current interval is two minutes
{
int pos=randPartition(a,l,r);
quicksort(a,l,pos-1); //pos The sequence on the left is sorted
quicksort(a,pos+1,r); //pos The elements on the right are sorted
}
}
int randselect(int *a,int l, int r,int k) // Select the... From the sequence k Large number , Time complexity O(n), Is a very good algorithm
{
if(l==r)
return a[l]; // Recursive boundary , This step is necessary , Otherwise, it will be queried k Larger than the sequence length , Can cause errors
int pos=randPartition(a,l,r);
int th=pos-l+1; // At this time, No pos The sign is the... Of the sequence th Big
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) Quick sort
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
/**
int my_partition(int *a,int l,int r) // Select the first element as the primary element to sort , The element on the left of the primary element is no larger than it , The elements on the right are larger than it
{
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) // Call the main meta function for quick sorting
//{
// 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 Next function , Return to one 0 To rand_max Between pseudorandom numbers ;
To avoid getting the same sequence of random numbers every time the program runs , You can call srand function , With his parameters
The random number generator is initialized , It's usually srand((unsigned int) time(NULL));
time function :
time_t time(time_t timer):
When timer yes NULL when , Get the current time ( from 1970-01-01 00:00:00 The number of seconds to now );
*/
int randPartition(int *a,int l,int r) // Select the principal component randomly , Avoid a quick sort that degenerates into O(n^2)
{
srand((unsigned int) time(NULL)); // Initialize random sequence , Prevent every rand() Return the same value
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) // Call the random selection phase principal component function for quick sorting
{
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) // Select the... From the sequence k Large number , Time complexity O(n), Is a very good algorithm
{
if(l==r)
return a[l]; // Recursive boundary , This step is necessary , Otherwise, it will be queried k Larger than the sequence length , Can cause errors
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;
}Here is an example :
subject : Given a set of integers , The integers in the set are different , Now let's divide it into two sets ,
Make the union of these two sets the original set , Turn into an empty set , At the same time, the number of elements in the two subsets should be n1 and n2 The absolute value of
As small as possible , The sum of the respective elements should be as large as possible . Output the absolute value of the difference between the sum of the elements of two sets .
data:
13
1 6 33 18 4 0 10 5 12 7 2 9 3
analysis : When N When it's even , The set selects the first half and the second half of the sequence as its own elements ; When it's odd , Interpretation No. (N+1)/2 Is it a positive number or a negative number , If it's a positive number , In the next set , If it's negative , Put it in
In the previous set .
/**
subject : Given a set of integers , The integers in the set are different , Now let's divide it into two sets ,
Make the union of these two sets the original set , Turn into an empty set , At the same time, the number of elements in the two subsets should be n1 and n2 The absolute value of
As small as possible , The sum of the respective elements should be as large as possible . Output the absolute value of the difference between the sum of the elements of two sets .
analysis : When N When it's even , The set selects the first half and the second half of the sequence as its own elements ; When it's odd ,
Interpretation No. (N+1)/2 Is it a positive number or a negative number , If it's a positive number , In the next set , If it's negative , Put it in
In the previous set
data:
13
1 6 33 18 4 0 10 5 12 7 2 9 3
*/
/**
3) Quick sort
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
/**
int rand(void):stdlib.h Next function , Return to one 0 To rand_max Between pseudorandom numbers ;
To avoid getting the same sequence of random numbers every time the program runs , You can call srand function , With his parameters
The random number generator is initialized , It's usually srand((unsigned int) time(NULL));
time function :
time_t time(time_t timer):
When timer yes NULL when , Get the current time ( from 1970-01-01 00:00:00 The number of seconds to now );
*/
int randPartition(int *a,int l,int r) // Select the principal component randomly , Avoid a quick sort that degenerates into O(n^2)
{
srand((unsigned int) time(NULL)); // Initialize random sequence , Prevent every rand() Return the same value
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;
}
/** This function is not required
void quicksort(int *a,int l,int r) // Call the random selection phase principal component function for quick sorting
{
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) // Select the... From the sequence k Large number , Time complexity O(n), Is a very good algorithm
{
if(l==r)
return a[l]; // Recursive boundary , This step is necessary , Otherwise, it will be queried k Larger than the sequence length , Can cause errors
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) // Special attention needs to be paid here , The subscript is from 0 At the beginning
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;
}
边栏推荐
- Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
- Kubernetes: container resource requirements and constraints (constraints)
- Informatics Olympiad all in one 1361: production | Luogu P1037 [noip2002 popularization group] production
- Relationship between EMC, EMI and EMS
- PWN attack and defense world guess_ num
- arraylist基操和添加元素源码
- PWN攻防世界Level2
- 认证培训|StreamNative Certification 培训第2期
- EMC、EMI、EMS的关系
- 逆序对对数计算,顺序对对数计算——归并排序
猜你喜欢

Lanbao sensor technology rushes to the scientific innovation board: annual revenue of 350million yuan xuyongtong family has a strong color

1110: 最近共同祖先(函数专题)

If you dare to write MQ message queue middleware on your resume, you must win these questions!

對補wasm環境的一些測試

There's a mystery behind the little login

今日直播|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭

Relationship between EMC, EMI and EMS

Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)

SQL training 01

PWN攻防世界guess_num
随机推荐
sql训练01
LabVIEW jump to web page
Application of fsockopen function
【無標題】
PWN攻防世界guess_num
线程池是什么老鸡?
Calculate rectangular area
归并排序
均贫富
SQL training 01
mgalcu-a509
MySQL binlog log cleanup
allegro 设计中显示网络飞线或关闭网络飞线的方法
測試入門——集成測試
Use photoshop2022 to create a wonderful gradient effect for pictures
Ctfhub web SQL injection - integer injection
There's a mystery behind the little login
PHP database ODBC
Today's sleep quality record 82 points
mark