-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
80 lines (57 loc) · 1.41 KB
/
main.c
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 <time.h>
#include <stdbool.h>
void printArr(int* arr, int len);
void quicksort(int* arr, int len);
void parallelQuicksort(int* arr, int len);
void copyArr(int* arr, int n, int* newArr);
bool isSorted(int* arr, int n);
void main()
{
int n = 10000;
int arr[n];
int* unorderedArr = (int *)malloc(n * sizeof(int));
int buffer = -1;
int i;
// Reads n entries from stdin
for(i = 0; i < n; i++)
{
scanf("%d", &buffer);
arr[i] = buffer;
}
copyArr(arr, n, unorderedArr);
// Run sorting here
clock_t startTime;
clock_t endTime;
startTime = clock();
quicksort(arr, n);
endTime = clock();
printf("Sequential:\t%ld\n", (endTime - startTime));
startTime = clock();
parallelQuicksort(unorderedArr, n);
endTime = clock();
printf("Parallel:\t%ld\n", (endTime - startTime));
free(unorderedArr);
}
// Determines if an array is sorted
bool isSorted(int* arr, int n)
{
int i;
for(i = 1; i < n; i++)
if (arr[i] < arr[i - 1]) return false;
return true;
}
// Copies an array into another
void copyArr(int* arr, int n, int* newArr)
{
int i;
for(i = 0; i < n; i++) newArr[i] = arr[i];
}
// Prints and integer array 'arr' of length 'len'
void printArr(int* arr, int len)
{
int i = 0;
for(i = 0; i < len; i++) printf("%d\t", arr[i]);
printf("\n");
}