-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_sort.c
75 lines (73 loc) · 1.98 KB
/
Word_sort.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
#include <string.h>
// for using strcmp and strcpy
#define MAX 10
// Every string has this as its default length.
#include <stdlib.h>
#include <stdio.h>
void Merge_arr(char* arr[],int beg,int mid,int end)
// Merging the Array Function
// Merges two subarrays.
{
int nL = mid - beg + 1;
int nR = end - mid;
char **L = malloc(sizeof(char *) * nL);
char **R = malloc(sizeof(char *) * nR);
int i; /* copying data to temp arrays */
for (i = 0; i < nL; i++)
{
L[i] = malloc(sizeof(arr[beg + i]));
strcpy(L[i], arr[beg + i]);
}
for (i = 0; i < nR; i++)
{
R[i] = malloc(sizeof(arr[mid + i + 1]));
strcpy(R[i], arr[mid + i + 1]);
}
int j = 0, k; /* starting index of the first sub-array */
i = 0; /* starting index of the second sub-array */
k = beg; /* starting index of the merged sub-array */
while (i < nL && j < nR)
{
if (strcmp(L[i], R[j]) < 0)
strcpy(arr[k++], L[i++]);
else
strcpy(arr[k++], R[j++]);
}
while (i < nL)
strcpy(arr[k++], L[i++]);
while (j < nR)
strcpy(arr[k++], R[j++]);
}
void MergeSort(char *arr[], int beg, int end)
// Main MergeSort function
{
if (beg < end)
{
int mid = (beg + end) / 2;
MergeSort(arr, beg, mid);
MergeSort(arr, mid + 1, end);
Merge_arr(arr, beg, mid, end);
}
}
int main()
{
printf("\nEnter the number of words: ");
int n;
// size of the array for storing the words
scanf("%d", &n);
char **arr_str = malloc(sizeof(char *) * n);
// Creating required string array
int i;
for (i = 0; i < n; i++)
{
arr_str[i] = malloc(sizeof(char) * MAX);
printf("\nEnter the word %d : ", (i + 1));
scanf("%s", arr_str[i]);
}
MergeSort(arr_str, 0, n - 1);
printf("\nWords in Sorted order is:\n ");
// displaying the sorted elements
for (i = 0; i < n; i++)
printf("%s ->", arr_str[i]);
return 0;
}