Skip to content

Commit 746cbd0

Browse files
authored
Add Sleep Sort in C (#4363)
1 parent 4fc3cdd commit 746cbd0

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

archive/c/c/sleep-sort.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <pthread.h>
5+
#include <unistd.h>
6+
7+
pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
8+
int *global_sorted;
9+
int global_index = 0;
10+
int global_total;
11+
12+
typedef struct {
13+
int number;
14+
} ThreadPayload;
15+
16+
void* sortNumber(void* args) {
17+
ThreadPayload* payload = (ThreadPayload*) args;
18+
const int number = payload->number;
19+
20+
sleep(number); // Sleep for number seconds
21+
22+
pthread_mutex_lock(&print_mutex);
23+
global_sorted[global_index++] = number;
24+
pthread_mutex_unlock(&print_mutex);
25+
26+
free(payload);
27+
return NULL;
28+
}
29+
30+
void parseInput(const char *input, int **arr, int *n) {
31+
char *token;
32+
char *inputCopy = strdup(input);
33+
token = strtok(inputCopy, ",");
34+
35+
while (token != NULL) {
36+
(*arr)[(*n)++] = atoi(token);
37+
token = strtok(NULL, ",");
38+
}
39+
40+
free(inputCopy);
41+
}
42+
43+
int main(int argc, char *argv[]) {
44+
if (argc != 2) {
45+
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
46+
return 1;
47+
}
48+
49+
const char *input = argv[1];
50+
if (strlen(input) == 0 || input[0] == ' ') {
51+
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
52+
return 1;
53+
}
54+
55+
int *arr = malloc(100 * sizeof(int));
56+
int n = 0;
57+
58+
parseInput(input, &arr, &n);
59+
60+
if (n < 2) {
61+
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
62+
free(arr);
63+
return 1;
64+
}
65+
66+
global_sorted = malloc(n * sizeof(int));
67+
global_total = n;
68+
69+
pthread_t *threads = malloc(n * sizeof(pthread_t));
70+
71+
for (int i = 0; i < n; i++) {
72+
ThreadPayload *payload = malloc(sizeof(ThreadPayload));
73+
payload->number = arr[i];
74+
pthread_create(&threads[i], NULL, sortNumber, (void *) payload);
75+
}
76+
77+
for (int i = 0; i < n; i++) {
78+
pthread_join(threads[i], NULL);
79+
}
80+
81+
for (int i = 0; i < n; i++) {
82+
printf("%d%s", global_sorted[i], (i < n - 1) ? ", " : "");
83+
}
84+
printf("\n");
85+
86+
free(arr);
87+
free(threads);
88+
free(global_sorted);
89+
90+
return 0;
91+
}

archive/c/c/testinfo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ folder:
55
container:
66
image: "gcc"
77
tag: "8.3"
8-
build: "gcc -o {{ source.name }} {{ source.name }}{{ source.extension }} -lm"
8+
build: "gcc -o {{ source.name }} {{ source.name }}{{ source.extension }} -pthread -lm"
99
cmd: "./{{ source.name }}"

0 commit comments

Comments
 (0)