-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
85 lines (77 loc) · 1.93 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
//System Libraries Here
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
using namespace chrono;
typedef long long ll;
//Function Prototypes Here
void fillAry(int [],int,int,int);
ll selSort(int [],int);
//Program Execution Begins Here
int main(int argc, char** argv)
{
//Set the random number seed
srand(static_cast<unsigned int>(time(0)));
//Declare all Variables Here
const int SIZE=8000;
int *array = new int[SIZE];
int lowRng=0,highRng=SIZE;
int loopCnt = 1000;
ll operation = 0LL;
//start time
time_point<system_clock> start, end;
duration<float> t = duration<float>::zero();
//Loop through the array 1000 times
for(int i = 0; i < loopCnt; ++i)
{
fillAry(array,SIZE,highRng,lowRng);
start = system_clock::now();
selSort(array,SIZE);
operation += selSort(array,SIZE);
t += system_clock::now() - start;
}
//Output operations
cout<<"Array Size: "<<SIZE<<endl;
cout<<"Loop Count: "<<loopCnt<<endl;
cout<<"Operations: "<<operation / loopCnt<<endl;
cout<<"Average Time: "<<t.count() / loopCnt<<" Seconds"<<endl;
//Exit
delete [] array;
return 0;
}
ll selSort(int a[],int n)
{
//Loop and declare variables
int indx,min;
ll ops = 4;
for(int pos=0;pos<n-1;pos++)
{
//Find the smallest in the list, swap after finding
min=a[pos];indx=pos;
ops += 7;
for(int i=pos+1;i<n;i++)
{
ops += 3;
if(a[i]<min){
min=a[i];
indx=i;
ops += 3;
}
ops += 4;
}
//Perform the swap
a[indx]=a[pos];
a[pos]=min;
ops += 9;
}
return ops;
}
void fillAry(int a[],int n,int hr,int lr)
{
for(int indx=0;indx<n;indx++)
{
a[indx]=rand()%(hr-lr+1)+lr;
}
}