-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick Sort Analysis.c
158 lines (131 loc) · 2.79 KB
/
Quick Sort Analysis.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Coding Assignment 4 */
// Samuel Ambani 1001694453
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
void PrintArray (int ArrayToPrint [], int SizeAp)
{
int i ;
for ( i = 0; i < SizeAp; i++)
printf("%d\n",ArrayToPrint[i]);
}
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
#if QSM
int middle = (high+low)/2;
swap(&arr[middle], &arr[high]);
#elif QSRND
int random = (rand() % (high-low+1) + low);
swap(&arr[random], &arr[high]);
#endif
int pivot = arr[high];
int i = (low - 1);
int j;
for ( j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap (&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int ReadFileIntoarray(int argc, char *argv[], int **AP)
{
char Filename[100];
int Count= 0;
int num;
int sum;
FILE *fp;
int i = 0;
if(argv[1]!= NULL)
{
strcpy(Filename,argv[1]);
fp = fopen(Filename,"r");
while (fgets(Filename,sizeof(Filename),fp))
{
Count++;
}
fseek(fp,0,0);
*AP = malloc(Count*sizeof(int));
while (fgets(Filename,sizeof(Filename),fp))
{
(*AP)[i] = atoi(Filename);
i++;
}
return i;
}
else
{
if (argc != 3)
{
printf("Number of Runs now specified on command line... defaulting to 10\n");
}
printf("File must be provided on command line...exiting \n");
exit(0);
}
fclose(fp);
}
int main(int argc , char* argv[])
{
int *AP = NULL;
int arr [] = {};
int elements= 0;
//elements= ReadFileIntoarray(argc,argv,&AP);
clock_t start, end, mstart,mend;
int loop;
long int sum = 0;
long int average = 0;
int runcount = 1;
int j ;
if (argc < 3)
{
printf("Number of Runs now specified on command line... deafaulting to 10\n");
loop = 10;
}
else
{
loop = atoi(argv[2]);
}
for(j = 0; j < loop; j++)
{
elements= ReadFileIntoarray(argc,argv,&AP);
#ifdef PRINTARRAY
printf("Given array is \n");
PrintArray(AP, elements);
#endif
start = clock();
quickSort(AP, 0, elements - 1);
end = clock();
printf("Run %d complete: %ld tics\n", runcount, end-start);
sum += end-start;
#ifdef PRINTARRAY
printf("\nQuick Sort Array Sorted is \n");
PrintArray(AP, elements);
#endif
runcount++;
free(AP);
}
average = sum / loop;
printf("Average run time for %d is %ld\n", loop, average);
printf("\nproceed %d records\n",elements);
return 0;
}