-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path6-3selective.cpp
57 lines (49 loc) · 1.08 KB
/
6-3selective.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
#include <iostream>
#include <time.h>
using namespace std;
int times;
void select_sort(int a[],int n)
{
int i,j,min;
for(int i=0;i<n-1;i++)
{
min=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
times++;
}
if(min!=i)
swap(a[i],a[min]);
}
}
int main()
{
int n,num[900000];
int tot_times;
cin>>n;
freopen("/Users/davidparker/desktop/sort.out","w",stdout);
for(int k=1;k<=10;k++)
{
cout<<"##################################"<<endl;
times=0;
for(int i=1;i<=n;i++)
num[i]=rand()%1000000+10;
for(int i=1;i<=n;i++)
cout<<num[i]<<" ";
cout<<endl;
cout<<"____________________"<<endl;
clock_t start=clock();
select_sort(num,n);
cout<<"+_+"<<(clock()-double(start))/CLOCKS_PER_SEC<<"秒"<<endl;
for(int i=1;i<=n;i++)
cout<<num[i]<<" ";
cout<<endl;
tot_times+=times;
cout<<"共进行 "<<times<<" 次比较"<<endl;
}
cout<<endl<<endl<<"平均进行 "<<tot_times/10<<" 次比较"<<endl;
fclose(stdin);
return 0;
}