-
-
Notifications
You must be signed in to change notification settings - Fork 632
Add Sleep Sort in C #4363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Sleep Sort in C #4363
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9e450c9
add sleep-sort.c
2Clutch fba8d45
refactor to use posix standards for cross-platform compatibility
2Clutch feaed1e
fix phthread linking
2Clutch d493661
fix output formatting
2Clutch 05dd95f
remove threading dependency
2Clutch 04a78a6
undo brain fart
2Clutch 94869eb
Merge branch 'main' into main
2Clutch 24095c1
undo brain fart
2Clutch 762789c
remove thread dependency
2Clutch defe755
bring back old solution
2Clutch 35592e3
Merge branch 'main' into main
2Clutch 550907b
bring back thread dependency
2Clutch f674f1f
Merge branch 'main' into main
2Clutch f294864
fix output consistency & ordering
2Clutch d287400
Merge branch 'main' into main
rzuckerm 082abc5
Merge branch 'main' into main
rzuckerm 29011eb
apply feedback
2Clutch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <pthread.h> | ||
| #include <unistd.h> | ||
|
|
||
| pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| int *global_sorted; | ||
| int global_index = 0; | ||
| int global_total; | ||
|
|
||
| typedef struct { | ||
| int number; | ||
| } ThreadPayload; | ||
|
|
||
| void* sortNumber(void* args) { | ||
| ThreadPayload* payload = (ThreadPayload*) args; | ||
| const int number = payload->number; | ||
|
|
||
| usleep(number * 1000); // Sleep for number milliseconds | ||
|
|
||
| pthread_mutex_lock(&print_mutex); | ||
| global_sorted[global_index++] = number; | ||
| pthread_mutex_unlock(&print_mutex); | ||
|
|
||
| free(payload); | ||
| return NULL; | ||
| } | ||
|
|
||
| int compare(const void* a, const void* b) { | ||
| return (*(int*)a - *(int*)b); | ||
| } | ||
rzuckerm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| void parseInput(const char *input, int **arr, int *n) { | ||
| char *token; | ||
| char *inputCopy = strdup(input); | ||
| token = strtok(inputCopy, ","); | ||
|
|
||
| while (token != NULL) { | ||
| (*arr)[(*n)++] = atoi(token); | ||
| token = strtok(NULL, ","); | ||
| } | ||
|
|
||
| free(inputCopy); | ||
| } | ||
|
|
||
| int main(int argc, char *argv[]) { | ||
| if (argc != 2) { | ||
| printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| const char *input = argv[1]; | ||
| if (strlen(input) == 0 || input[0] == ' ') { | ||
| printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| int *arr = malloc(100 * sizeof(int)); | ||
| int n = 0; | ||
|
|
||
| parseInput(input, &arr, &n); | ||
|
|
||
| if (n < 2) { | ||
| printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n"); | ||
| free(arr); | ||
| return 1; | ||
| } | ||
|
|
||
| global_sorted = malloc(n * sizeof(int)); | ||
| global_total = n; | ||
|
|
||
| pthread_t *threads = malloc(n * sizeof(pthread_t)); | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| ThreadPayload *payload = malloc(sizeof(ThreadPayload)); | ||
| payload->number = arr[i]; | ||
| pthread_create(&threads[i], NULL, sortNumber, (void *) payload); | ||
| } | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| pthread_join(threads[i], NULL); | ||
| } | ||
|
|
||
| qsort(global_sorted, n, sizeof(int), compare); | ||
rzuckerm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| printf("%d%s", global_sorted[i], (i < n - 1) ? ", " : ""); | ||
| } | ||
| printf("\n"); | ||
|
|
||
| free(arr); | ||
| free(threads); | ||
| free(global_sorted); | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.