diff --git a/OS/C/Week5/q2 b/OS/C/Week5/q2 new file mode 100755 index 0000000..2ed273f Binary files /dev/null and b/OS/C/Week5/q2 differ diff --git a/OS/C/Week5/q2_opt b/OS/C/Week5/q2_opt new file mode 100755 index 0000000..3464647 Binary files /dev/null and b/OS/C/Week5/q2_opt differ diff --git a/OS/C/Week5/q2_opt.c b/OS/C/Week5/q2_opt.c index 317a02b..92f9430 100644 --- a/OS/C/Week5/q2_opt.c +++ b/OS/C/Week5/q2_opt.c @@ -8,7 +8,7 @@ #include #include -void main () { +int main () { // array declaration char *strings[5]; diff --git a/OS/C/Week5/q2opt b/OS/C/Week5/q2opt index f2352aa..5d2db88 100755 Binary files a/OS/C/Week5/q2opt and b/OS/C/Week5/q2opt differ diff --git a/OS/C/Week5/q2opt.c b/OS/C/Week5/q2opt.c index 4ad4a11..88104f1 100644 --- a/OS/C/Week5/q2opt.c +++ b/OS/C/Week5/q2opt.c @@ -12,7 +12,18 @@ int compare_strings(const void *a, const void *b) { int main(int argc, char *argv[]) { pid_t pid = fork(); if (pid == 0) { - qsort(&argv[1], argc - 1, sizeof(char *), compare_strings); + int i, j; + int count = argc - 1; + char **strings = &argv[1]; + for (i = 0; i < count - 1; i++) { + for (j = 0; j < count - i - 1; j++) { + if (strcmp(strings[j], strings[j + 1]) > 0) { + char *temp = strings[j]; + strings[j] = strings[j + 1]; + strings[j + 1] = temp; + } + } + } printf("Child Sorted:\n"); for (int i = 1; i < argc; ++i) printf("%s\n", argv[i]);