-
Notifications
You must be signed in to change notification settings - Fork 1
/
q9.c
232 lines (204 loc) · 3.79 KB
/
q9.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// sorting algorithms
#include <stdio.h>
#define MAX_SIZE 100
int arr[MAX_SIZE];
int size;
// Function to print the array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
// Bubble Sort function
void bubbleSort()
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Insertion Sort function
void insertionSort()
{
for (int i = 1; i < size; i++)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// Selection Sort function
void selectionSort()
{
for (int i = 0; i < size - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < size; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
if (minIndex != i)
{
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
// Merge function for Merge Sort
void merge(int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k++] = L[i++];
}
else
{
arr[k++] = R[j++];
}
}
while (i < n1)
{
arr[k++] = L[i++];
}
while (j < n2)
{
arr[k++] = R[j++];
}
}
// Merge Sort function
void mergeSort(int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(l, m);
mergeSort(m + 1, r);
merge(l, m, r);
}
}
// Partition function for Quick Sort
int partition(int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
// Quick Sort function
void quickSort(int low, int high)
{
if (low < high)
{
int pi = partition(low, high);
quickSort(low, pi - 1);
quickSort(pi + 1, high);
}
}
// Main function
int main()
{
int choice;
do
{
printf("\nMenu:\n");
printf("1. Bubble Sort\n");
printf("2. Insertion Sort\n");
printf("3. Selection Sort\n");
printf("4. Merge Sort\n");
printf("5. Quick Sort\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice != 6)
{
printf("Enter the number of elements: ");
scanf("%d", &size);
if (size > MAX_SIZE)
{
printf("Size exceeds maximum allowed size of %d\n", MAX_SIZE);
continue;
}
printf("Enter %d integers:\n", size);
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
}
switch (choice)
{
case 1:
bubbleSort();
printf("Array after Bubble Sort:\n");
printArray(arr, size);
break;
case 2:
insertionSort();
printf("Array after Insertion Sort:\n");
printArray(arr, size);
break;
case 3:
selectionSort();
printf("Array after Selection Sort:\n");
printArray(arr, size);
break;
case 4:
mergeSort(0, size - 1);
printf("Array after Merge Sort:\n");
printArray(arr, size);
break;
case 5:
quickSort(0, size - 1);
printf("Array after Quick Sort:\n");
printArray(arr, size);
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 6);
return 0;
}