|
1 | 1 | #include <stdio.h> |
2 | 2 | #include <stdlib.h> |
3 | 3 | #include <string.h> |
4 | | -#include <limits.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <pthread.h> |
5 | 6 |
|
6 | | -void print_usage() { |
7 | | - printf("Usage: Please provide a list of integers in the format: \"1, 2, 3, 4, 5\"\n"); |
| 7 | +void* sleepSort(void* arg) { |
| 8 | + int num = *(int*)arg; |
| 9 | + usleep(num * 1000000); // Sleep for num seconds |
| 10 | + return (void*)(intptr_t)num; |
8 | 11 | } |
9 | 12 |
|
10 | | -int max_subarray_sum(int* arr, int n) { |
11 | | - int max_so_far = INT_MIN; |
12 | | - int max_ending_here = 0; |
| 13 | +void parseInput(const char *input, int **arr, int *n) { |
| 14 | + char *token; |
| 15 | + char *inputCopy = strdup(input); |
| 16 | + token = strtok(inputCopy, ","); |
13 | 17 |
|
14 | | - for (int i = 0; i < n; i++) { |
15 | | - max_ending_here += arr[i]; |
16 | | - |
17 | | - if (max_so_far < max_ending_here) { |
18 | | - max_so_far = max_ending_here; |
19 | | - } |
20 | | - |
21 | | - if (max_ending_here < 0) { |
22 | | - max_ending_here = 0; |
23 | | - } |
| 18 | + while (token != NULL) { |
| 19 | + (*arr)[(*n)++] = atoi(token); |
| 20 | + token = strtok(NULL, ","); |
24 | 21 | } |
25 | 22 |
|
26 | | - return max_so_far; |
| 23 | + free(inputCopy); |
27 | 24 | } |
28 | 25 |
|
29 | | -int main(int argc, char* argv[]) { |
30 | | - if (argc < 2) { |
31 | | - print_usage(); |
| 26 | +int main(int argc, char *argv[]) { |
| 27 | + if (argc != 2) { |
| 28 | + printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n"); |
32 | 29 | return 1; |
33 | 30 | } |
34 | 31 |
|
35 | | - // Check if input is empty |
36 | | - if (strlen(argv[1]) == 0) { |
37 | | - print_usage(); |
| 32 | + const char *input = argv[1]; |
| 33 | + if (strlen(input) == 0 || input[0] == ' ') { |
| 34 | + printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n"); |
38 | 35 | return 1; |
39 | 36 | } |
40 | 37 |
|
41 | | - // Parse input string |
42 | | - char* token; |
43 | | - int arr[100]; // Assuming a maximum of 100 integers |
44 | | - int count = 0; |
| 38 | + int *arr = malloc(100 * sizeof(int)); // Allocate memory for up to 100 integers |
| 39 | + int n = 0; |
45 | 40 |
|
46 | | - token = strtok(argv[1], ","); |
47 | | - while (token != NULL) { |
48 | | - arr[count++] = atoi(token); |
49 | | - token = strtok(NULL, ","); |
50 | | - } |
| 41 | + parseInput(input, &arr, &n); |
51 | 42 |
|
52 | | - // If less than two integers were provided |
53 | | - if (count == 1) { |
54 | | - printf("%d\n", arr[0]); |
55 | | - return 0; |
56 | | - } else if (count < 2) { |
57 | | - print_usage(); |
| 43 | + if (n < 2) { |
| 44 | + printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n"); |
| 45 | + free(arr); |
58 | 46 | return 1; |
59 | 47 | } |
60 | 48 |
|
61 | | - // Calculate maximum subarray sum |
62 | | - int result = max_subarray_sum(arr, count); |
| 49 | + pthread_t *threads = malloc(n * sizeof(pthread_t)); |
| 50 | + int *results = malloc(n * sizeof(int)); |
| 51 | + |
| 52 | + for (int i = 0; i < n; i++) { |
| 53 | + pthread_create(&threads[i], NULL, sleepSort, &arr[i]); |
| 54 | + } |
| 55 | + |
| 56 | + for (int i = 0; i < n; i++) { |
| 57 | + void* result; |
| 58 | + pthread_join(threads[i], &result); |
| 59 | + results[i] = (int)(intptr_t)result; |
| 60 | + } |
| 61 | + |
| 62 | + // Print with commas |
| 63 | + for (int i = 0; i < n; i++) { |
| 64 | + printf("%d%s", results[i], (i < n - 1) ? ", " : ""); |
| 65 | + } |
| 66 | + printf("\n"); |
63 | 67 |
|
64 | | - printf("%d\n", result); |
| 68 | + free(arr); |
| 69 | + free(threads); |
| 70 | + free(results); |
65 | 71 |
|
66 | 72 | return 0; |
67 | 73 | } |
0 commit comments