9/22/25, 12:48 PM
DIY1.cpp
演算法作業\DIY1.cpp
1
2
#include<iostream>
#include<random>
3
4
5
#include<vector>
#include<fstream>
#include<cmath>
6
7
8
//使用較為不準的ctime不影響實驗結果,因主要的不準量會是random pivot造成的
#include<ctime>
9
10
11
using namespace std;
const int SIZE = 1000000;
12
13
14
vector<int> input(SIZE*3);
15
16
17
void Read() {
fstream input1("1000000.case1.in");
fstream input2("1000000.case2.in");
18
19
20
fstream input3("1000000.case3.in");
int i = 0, iMax = 0, temp;
string line;
21
22
23
getline(input1, line);
getline(input1, line);
24
25
26
for( iMax = SIZE; i < iMax; i++ ) {
input1 >> temp >> input[i];
}
27
28
29
getline(input2, line);
getline(input2, line);
for( iMax = SIZE * 2; i < iMax; i++ ) {
30
31
32
33
input2 >> temp >> input[i];
}
getline(input3, line);
getline(input3, line);
34
35
36
for( iMax = SIZE * 3; i < iMax; i++ ) {
input3 >> temp >> input[i];
}
37
38
39
input1.close();
input2.close();
input3.close();
40
41
42
}
int f( int n ) { // how many random numbers to pick
43
44
45
//return 1;
if( n < 1000 ) return 1;
if( n < 10000 ) return 3;
46
47
48
}
return 5;
49
50
51
int tphp[5];
int temp, len;
void swap( int a, int b ) {
localhost:62401/293643cc-4787-4728-9fc9-df3d84d12ca2/
1/3
9/22/25, 12:48 PM
52
53
DIY1.cpp
temp = tphp[a];
tphp[a] = tphp[b];
54
55
56
tphp[b] = temp;
}
int thre = 50; //小於這個值就取一個,大於取三個
57
58
59
int GetR( int low, int high ) {
len = high - low + 1;
//return rand() % len + low;
60
61
62
if( len < thre ) return rand() % ( len ) + low;
63
64
65
if( input[tphp[0]] < input[tphp[1]] ) {
if( input[tphp[1]] < input[tphp[2]] ) return tphp[1];
if( input[tphp[0]] < input[tphp[2]] ) return tphp[2];
for( int i = 0; i < 3; i++ ) tphp[i] = rand() % ( len ) + low;
66
67
68
return tphp[0];
}
if( input[tphp[0]] < input[tphp[2]] ) return tphp[0];
69
70
71
if( input[tphp[1]] < input[tphp[2]] ) return tphp[2];
return tphp[1];
}
72
73
74
void InsertionSort(vector<int>& data, int low, int high ) {
for( int i = low; i <= high; i++ ) {
75
76
77
int j = i - 1, key = data[i];
while( j >= 0 && data[j] > key ) {
data[j+1] = data[j];
78
79
80
j--;
}
data[j+1] = key;
81
82
83
84
85
86
87
}
}
int RandomizedPartition(vector<int>& data, int low, int high){
int r = GetR( low, high );
int i = low, pivot, temp;
88
89
90
temp = data[r];
data[r] = data[high];
data[high] = temp;
91
92
93
pivot = data[high];
94
95
96
for( int j = low; j < high; j++ ) {
if( data[j] <= pivot ) {
temp = data[j];
97
98
99
data[j] = data[i];
data[i] = temp;
i++;
100
101
102
}
}
temp = data[i];
103
104
105
data[i] = data[high];
data[high] = temp;
return i;
localhost:62401/293643cc-4787-4728-9fc9-df3d84d12ca2/
2/3
9/22/25, 12:48 PM
DIY1.cpp
106
107
}
108
109
110
int thre1 = 30; //小於這個大小用insertion
void QuickSortSubVector(vector<int>& data, int low, int high) {
if( high - low < thre1 ) InsertionSort( data, low, high );
111
112
113
else if( low < high ) {
int mid = RandomizedPartition( data, low, high );
QuickSortSubVector( data, mid+1, high );
114
115
116
QuickSortSubVector( data, low, mid-1 );
}
117
118
119
bool check( int num ) {
int k = 0;
}
120
121
122
for( int i = 0; i < num; i++ ) {
//cout<< input[i]<< endl;
if( k > input[i] ) return 0;
123
124
125
k = input[i];
}
return 1;
126
127
128
}
int main() {
129
130
131
Read();
132
133
134
QuickSortSubVector(input, SIZE*2, SIZE*3);
clock_t end = clock();
135
136
137
double cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
cout<< "time: "<< cpu_time_used<< endl;
138
139
140
141
//cout<< check(SIZE*3);
return 0;
clock_t start = clock();
}
142
143
localhost:62401/293643cc-4787-4728-9fc9-df3d84d12ca2/
3/3