-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
97 lines (89 loc) · 1.98 KB
/
main.cpp
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
#include <iostream>
#include<fstream>
#include<time.h>
#include<stdlib.h>
#define MAXNUM 1000
using namespace std;
int select(int a[],int len,int k);
int bubble_sort(int a[],int len,int k);
int main()
{
srand((unsigned int)time (NULL));
ofstream op("E:\\DAA\\test2.txt");
for(int i=0;i<MAXNUM;i++){
op<<rand()%500<<"\n";
}
op.close();
int len;
int k;
cout<<"**********求数组第K小元素问题**********"<<endl<<endl;
cout<<"请输入数组的长度:";
cin>>len;
cout<<"请输入K的值:";
cin>>k;
int a[len];
ifstream fp("E:\\DAA\\test2.txt");
for(int i=0;i<len;i++){
fp>>a[i];
}
fp.close();
int result;
result=select(a,len,k);
cout<<"该数组第"<<k<<"小元素为:"<<result<<endl;
if(len<=30){
cout<<"数组元素为:"<<endl;
for(int i=0;i<len;i++){
cout<<a[i]<<" ";
}
}
}
int select(int a[],int len,int k){
int sub_len=len/5;
int result;
if(len<5){
result=bubble_sort(a,len,k);
return result;
}
int b[5];
int median[sub_len];
for(int i=0;i<sub_len;i++){
for(int j=0;j<5;j++){
b[j]=a[i*5+j];
}
median[i]=bubble_sort(b,5,3);
}
int x;
int s1[len],s2[len];
int s1_num=0,s2_num=0;
x=select(median,sub_len,sub_len/2+1); //这里加1为了避免sub_len=1时sub_len/2=0,出bug
for(int i=0;i<len;i++){
if(a[i]<=x){
s1[s1_num]=a[i];
s1_num++;
}
else{
s2[s2_num]=a[i];
s2_num++;
}
}
if(k==s1_num+1)
result=x;
else if(k<=s1_num)
result=select(s1,s1_num,k);
else
result=select(s2,s2_num,k-s1_num);
return result;
}
int bubble_sort(int a[],int len,int k){
int t;
for(int i=0;i<len-1;i++){
for(int j=0;j<len-i-1;j++){
if(a[j]>a[j+1]){
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return a[k-1];
}