forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.c
More file actions
249 lines (211 loc) · 7.24 KB
/
MergeSort.c
File metadata and controls
249 lines (211 loc) · 7.24 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Program: Merge Sort Algorithm
*
* Problem Statement:
* Implement merge sort to sort an array of integers in ascending order.
* Merge sort is a divide-and-conquer algorithm that divides the array into halves,
* recursively sorts them, and then merges the sorted halves back together.
*
* Input:
* - An unsorted array of integers
* - Size of the array
*
* Output:
* - The same array sorted in ascending order
*
* Example:
* Input: [64, 34, 25, 12, 22, 11, 90, 5]
* Output: [5, 11, 12, 22, 25, 34, 64, 90]
*
* Time Complexity: O(n log n) - for all cases (best, average, worst)
* Space Complexity: O(n) - requires additional space for temporary arrays
*
* Compile with: gcc MergeSort.c -o MergeSort
*/
#include <stdio.h>
#include <stdlib.h>
// Function to merge two sorted subarrays
void merge(int array[], int left, int middle, int right) {
int i, j, k;
// Calculate sizes of the two subarrays to be merged
int leftSize = middle - left + 1;
int rightSize = right - middle;
// Create temporary arrays for left and right subarrays
int *leftArray = (int*)malloc(leftSize * sizeof(int));
int *rightArray = (int*)malloc(rightSize * sizeof(int));
// Copy data to temporary arrays
for (i = 0; i < leftSize; i++) {
leftArray[i] = array[left + i];
}
for (j = 0; j < rightSize; j++) {
rightArray[j] = array[middle + 1 + j];
}
// Merge the temporary arrays back into array[left..right]
i = 0; // Initial index of left subarray
j = 0; // Initial index of right subarray
k = left; // Initial index of merged subarray
// Compare elements from both arrays and merge in sorted order
while (i < leftSize && j < rightSize) {
if (leftArray[i] <= rightArray[j]) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
k++;
}
// Copy remaining elements of leftArray[], if any
while (i < leftSize) {
array[k] = leftArray[i];
i++;
k++;
}
// Copy remaining elements of rightArray[], if any
while (j < rightSize) {
array[k] = rightArray[j];
j++;
k++;
}
// Free allocated memory
free(leftArray);
free(rightArray);
}
// Main merge sort function
void mergeSort(int array[], int left, int right) {
if (left < right) {
// Calculate middle point to divide array into two halves
int middle = left + (right - left) / 2;
// Recursively sort first and second halves
mergeSort(array, left, middle);
mergeSort(array, middle + 1, right);
// Merge the sorted halves
merge(array, left, middle, right);
}
}
// Function to display array elements
void displayArray(int array[], int size) {
printf("[");
for (int i = 0; i < size; i++) {
printf("%d", array[i]);
if (i < size - 1) {
printf(", ");
}
}
printf("]");
}
// Function to copy array (for demonstration purposes)
void copyArray(int source[], int destination[], int size) {
for (int i = 0; i < size; i++) {
destination[i] = source[i];
}
}
// Function to check if array is sorted
int isSorted(int array[], int size) {
for (int i = 0; i < size - 1; i++) {
if (array[i] > array[i + 1]) {
return 0; // Not sorted
}
}
return 1; // Sorted
}
int main() {
printf("=== Merge Sort Algorithm Demo ===\n\n");
// Example array for demonstration
int originalArray[] = {64, 34, 25, 12, 22, 11, 90, 5};
int size = sizeof(originalArray) / sizeof(originalArray[0]);
// Create a copy to sort (keep original for display)
int *arrayToSort = (int*)malloc(size * sizeof(int));
copyArray(originalArray, arrayToSort, size);
// Display original array
printf("Original array: ");
displayArray(originalArray, size);
printf("\nArray size: %d elements\n\n", size);
// Perform merge sort
printf("Sorting array using Merge Sort...\n");
mergeSort(arrayToSort, 0, size - 1);
// Display sorted array
printf("\nSorted array: ");
displayArray(arrayToSort, size);
printf("\n");
// Verify sorting
if (isSorted(arrayToSort, size)) {
printf("✓ Array successfully sorted!\n");
} else {
printf("✗ Sorting failed!\n");
}
// Interactive section for user input
printf("\n=== Try with your own array ===\n");
int userSize;
printf("Enter the number of elements (max 20): ");
scanf("%d", &userSize);
if (userSize > 0 && userSize <= 20) {
int *userArray = (int*)malloc(userSize * sizeof(int));
printf("Enter %d integers:\n", userSize);
for (int i = 0; i < userSize; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &userArray[i]);
}
printf("\nYour array: ");
displayArray(userArray, userSize);
printf("\n");
// Sort user's array
printf("Sorting your array...\n");
mergeSort(userArray, 0, userSize - 1);
printf("Sorted array: ");
displayArray(userArray, userSize);
printf("\n");
if (isSorted(userArray, userSize)) {
printf("✓ Your array was successfully sorted!\n");
}
free(userArray);
} else {
printf("Invalid size. Please enter a number between 1 and 20.\n");
}
// Additional test cases
printf("\n=== Additional Test Cases ===\n");
// Test case 1: Already sorted array
int sortedArray[] = {1, 2, 3, 4, 5};
int sortedSize = 5;
printf("\nTest 1 - Already sorted: ");
displayArray(sortedArray, sortedSize);
mergeSort(sortedArray, 0, sortedSize - 1);
printf("\nAfter sorting: ");
displayArray(sortedArray, sortedSize);
printf("\n");
// Test case 2: Reverse sorted array
int reverseArray[] = {5, 4, 3, 2, 1};
int reverseSize = 5;
printf("\nTest 2 - Reverse sorted: ");
displayArray(reverseArray, reverseSize);
mergeSort(reverseArray, 0, reverseSize - 1);
printf("\nAfter sorting: ");
displayArray(reverseArray, reverseSize);
printf("\n");
// Test case 3: Array with duplicates
int duplicateArray[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
int duplicateSize = 9;
printf("\nTest 3 - With duplicates: ");
displayArray(duplicateArray, duplicateSize);
mergeSort(duplicateArray, 0, duplicateSize - 1);
printf("\nAfter sorting: ");
displayArray(duplicateArray, duplicateSize);
printf("\n");
// Algorithm complexity information
printf("\n=== Algorithm Analysis ===\n");
printf("Time Complexity:\n");
printf(" - Best Case: O(n log n)\n");
printf(" - Average Case: O(n log n)\n");
printf(" - Worst Case: O(n log n)\n");
printf("Space Complexity: O(n)\n");
printf("\nAdvantages:\n");
printf(" - Stable sorting algorithm\n");
printf(" - Consistent O(n log n) performance\n");
printf(" - Good for large datasets\n");
printf(" - Parallelizable\n");
printf("\nDisadvantages:\n");
printf(" - Requires O(n) extra space\n");
printf(" - Not in-place sorting\n");
free(arrayToSort);
return 0;
}