-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort_Serial.cpp
More file actions
80 lines (64 loc) · 1.92 KB
/
Sort_Serial.cpp
File metadata and controls
80 lines (64 loc) · 1.92 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <time.h>
#include <math.h>
#include <algorithm>
#include <sys/time.h> // for clock_gettime()
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void RandomInit(int* data, int N)
{
/* initialize random seed: */
srand (time(NULL));
for (int i=0; i<N; i++)
{
data[i] = (int)(rand()%100)+1;
}
}
void printArray(int* A, int N){
int i;
printf("{");
for (i=0; i<N; i++){
printf("%d ", A[i]);
}
printf("}\n");
}
bool isArraySorted(int* A, int N){
int i;
for (i=0; i<N-1; i++){
if(A[i] > A[i+1])
return false;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
if(argc < 5)
{
printf("Argument are not corect\n");
printf("Four arguemnts are required: printArray checkRequired arraysize numThreads\n");
return 0;
}
//initialize variables from arguments
int printArrayRequired = atoi(argv[1]);
int checkRequired = atoi(argv[2]);
int N = atoi(argv[3]);
int numThreads = atoi(argv[4]);
int* master_array;
//clock_t start, end;
double cpu_time_used;
struct timeval start, end;
gettimeofday(&start, NULL);
printf("Allocate Array\n");
master_array = (int*)malloc(N*sizeof(int));
RandomInit(master_array, N);
// Sorting_Partial Per Process
printf("Sorting ...\n");
std::sort(master_array, master_array + N);
gettimeofday(&end, NULL);
cpu_time_used = ((end.tv_sec - start.tv_sec) * 1000000u + end.tv_usec - start.tv_usec) / 1.e6;
printf("Elapsed Time= %f seconds \n", cpu_time_used);
}