-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivideConquer_QuickSort.c
More file actions
215 lines (183 loc) · 4.26 KB
/
DivideConquer_QuickSort.c
File metadata and controls
215 lines (183 loc) · 4.26 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Divide & Conquer Method : Quick Sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int comparisons = 0;
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++)
{
comparisons++;
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);
}
}
void quickSortDescending(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSortDescending(arr, low, pi - 1);
quickSortDescending(arr, pi + 1, high);
}
for (int i = 0; i < (high - low + 1) / 2; i++)
{
swap(&arr[low + i], &arr[high - i]);
}
}
const char *checkScenario(int arr[], int low, int high, int pi)
{
int leftSize = pi - low;
int rightSize = high - pi;
if (leftSize == 0 || rightSize == 0)
{
return "Worst-case";
}
else if (leftSize == rightSize)
{
return "Best-case";
}
else
{
return "Intermediate-case";
}
}
int readFile(const char *filename, int arr[])
{
FILE *file = fopen(filename, "r");
if (!file)
{
printf("Error opening file %s\n", filename);
return -1;
}
int i = 0;
while (fscanf(file, "%d", &arr[i]) != EOF)
{
i++;
}
fclose(file);
return i;
}
void writeFile(const char *filename, int arr[], int n)
{
FILE *file = fopen(filename, "w");
if (!file)
{
printf("Error opening file %s\n", filename);
return;
}
for (int i = 0; i < n; i++)
{
fprintf(file, "%d ", arr[i]);
}
fclose(file);
}
void displayFile(const char *filename)
{
FILE *file = fopen(filename, "r");
if (!file)
{
printf("Error opening file %s\n", filename);
return;
}
int value;
while (fscanf(file, "%d", &value) != EOF)
{
printf("%d ", value);
}
fclose(file);
printf("\n");
}
void generateRandomArray(int arr[], int n, int maxElementCount)
{
srand(time(NULL));
for (int i = 0; i < n; i++)
{
arr[i] = rand() % maxElementCount;
}
}
int main()
{
int arr[500];
int n;
int choice;
do
{
printf("MAIN MENU (QUICK SORT)\n");
printf("1. Ascending Data\n");
printf("2. Descending Data\n");
printf("3. Random Data\n");
printf("4. Exit\n");
printf("Enter option: ");
scanf("%d", &choice);
const char *inputFilename;
const char *outputFilename;
switch (choice)
{
case 1:
inputFilename = "input.txt";
outputFilename = "SaveHere.txt";
n = readFile(inputFilename, arr);
if (n == -1)
{
continue;
}
break;
case 2:
inputFilename = "input.txt";
outputFilename = "SaveHere.txt";
n = readFile(inputFilename, arr);
if (n == -1)
{
continue;
}
quickSortDescending(arr, 0, n - 1);
break;
case 3:
printf("Enter the number of elements : ");
scanf("%d", &n);
outputFilename = "SaveHere.txt";
generateRandomArray(arr, n, 1000);
quickSort(arr, 0, n - 1);
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice! Try again.\n");
continue;
}
printf("\nBefore Sorting:\n");
displayFile(inputFilename);
writeFile(outputFilename, arr, n);
printf("\nAfter Sorting:\n");
displayFile(outputFilename);
printf("\nNumber of Comparisons: %d\n", comparisons);
const char *scenario = checkScenario(arr, 0, n - 1, (n - 1) / 2);
printf("\nScenario: %s\n", scenario);
comparisons = 0;
} while (choice != 4);
return 0;
}