93 lines
		
	
	
	
		
			2.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
	
		
			2.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include <stdio.h>
 | 
						|
#include <stdlib.h>
 | 
						|
#include <string.h>
 | 
						|
#include <unistd.h>
 | 
						|
#include <sys/types.h>
 | 
						|
#include <sys/wait.h>
 | 
						|
 | 
						|
int compare_strings(const void *a, const void *b) {
 | 
						|
    return strcmp(*(const char **)a, *(const char **)b);
 | 
						|
}
 | 
						|
 | 
						|
void bubble_sort_strings(char **arr, int n) {
 | 
						|
    for (int i = 0; i < n - 1; ++i)
 | 
						|
        for (int j = 0; j < n - i - 1; ++j)
 | 
						|
            if (strcmp(arr[j], arr[j + 1]) > 0) {
 | 
						|
                char *temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;
 | 
						|
            }
 | 
						|
}
 | 
						|
 | 
						|
int partition(char **arr, int low, int high) {
 | 
						|
    char *pivot = arr[high];
 | 
						|
    int i = (low - 1);
 | 
						|
    for (int j = low; j <= high - 1; ++j)
 | 
						|
        if (strcmp(arr[j], pivot) < 0) {
 | 
						|
            i++;
 | 
						|
            char *temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
 | 
						|
        }
 | 
						|
    char *temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;
 | 
						|
    return (i + 1);
 | 
						|
}
 | 
						|
 | 
						|
void quick_sort_strings(char **arr, int low, int high) {
 | 
						|
    if (low < high) {
 | 
						|
        int pi = partition(arr, low, high);
 | 
						|
        quick_sort_strings(arr, low, pi - 1);
 | 
						|
        quick_sort_strings(arr, pi + 1, high);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
void print_strings(char **strings, int n, const char *process, const char *sort) {
 | 
						|
    printf("\n%s (%s):\n", process, sort);
 | 
						|
    for (int i = 0; i < n; ++i) printf("%s\n", strings[i]);
 | 
						|
}
 | 
						|
 | 
						|
char **read_strings(int n) {
 | 
						|
    char **strings = malloc(n * sizeof(char *));
 | 
						|
    for (int i = 0; i < n; ++i) {
 | 
						|
        size_t size = 100;
 | 
						|
        strings[i] = malloc(size);
 | 
						|
        printf("String %d: ", i + 1);
 | 
						|
        getline(&strings[i], &size, stdin);
 | 
						|
        strings[i][strcspn(strings[i], "\n")] = 0; // Remove newline
 | 
						|
    }
 | 
						|
    return strings;
 | 
						|
}
 | 
						|
 | 
						|
int main() {
 | 
						|
    int n;
 | 
						|
    printf("N strings: ");
 | 
						|
    scanf("%d", &n);
 | 
						|
    while (getchar() != '\n');
 | 
						|
    char **input = read_strings(n);
 | 
						|
 | 
						|
    pid_t pid_bubble = fork();
 | 
						|
    if (pid_bubble == 0) {
 | 
						|
        char **sorted_bubble = malloc(n * sizeof(char *));
 | 
						|
        for (int i = 0; i < n; ++i) sorted_bubble[i] = strdup(input[i]);
 | 
						|
        bubble_sort_strings(sorted_bubble, n);
 | 
						|
        print_strings(sorted_bubble, n, "Child BubbleSort", "Bubble Sort");
 | 
						|
        for (int i = 0; i < n; ++i) free(sorted_bubble[i]);
 | 
						|
        free(sorted_bubble);
 | 
						|
        exit(0);
 | 
						|
    }
 | 
						|
 | 
						|
    pid_t pid_quick = fork();
 | 
						|
    if (pid_quick == 0) {
 | 
						|
        char **sorted_quick = malloc(n * sizeof(char *));
 | 
						|
        for (int i = 0; i < n; ++i) sorted_quick[i] = strdup(input[i]);
 | 
						|
        quick_sort_strings(sorted_quick, 0, n - 1);
 | 
						|
        print_strings(sorted_quick, n, "Child QuickSort", "Quick Sort");
 | 
						|
        for (int i = 0; i < n; ++i) free(sorted_quick[i]);
 | 
						|
        free(sorted_quick);
 | 
						|
        exit(0);
 | 
						|
    }
 | 
						|
 | 
						|
    wait(NULL);
 | 
						|
    printf("\nParent (Unsorted):\n");
 | 
						|
    for (int i = 0; i < n; ++i) printf("%s\n", input[i]);
 | 
						|
 | 
						|
    for (int i = 0; i < n; ++i) free(input[i]);
 | 
						|
    free(input);
 | 
						|
    return 0;
 | 
						|
}
 |