From 7ac5d40bff1b9c6df8ce0b438ba2526bba3527f9 Mon Sep 17 00:00:00 2001 From: Arijit Saha Date: Mon, 18 Oct 2021 12:15:28 +0530 Subject: [PATCH 1/2] Add files via upload --- C/SleepSort.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 C/SleepSort.c diff --git a/C/SleepSort.c b/C/SleepSort.c new file mode 100644 index 0000000..2da4a18 --- /dev/null +++ b/C/SleepSort.c @@ -0,0 +1,30 @@ +// C implementation of Sleep Sort + +#include +#include +#include + +void routine(void *a) +{ + int n = *(int *) a; + Sleep(n); + printf("%d ", n); // After the sleep, print the number +} + +void sleepSort(int arr[], int n) +{ + HANDLE threads[n]; + for (int i = 0; i < n; i++) + threads[i] = (HANDLE)_beginthread(&routine, 0, &arr[i]); + + WaitForMultipleObjects(n, threads, TRUE, INFINITE); + return; +} + +int main() +{ + int arr[] = {555, 32, 7, 888,37,2,52}; + int n = sizeof(arr) / sizeof(arr[0]); + sleepSort (arr, n); + return(0); +} From e7b99301eec2fa774cdc3dfd09b3355631e58e52 Mon Sep 17 00:00:00 2001 From: Arijit Saha Date: Mon, 18 Oct 2021 12:28:17 +0530 Subject: [PATCH 2/2] Add files via upload --- C/QuickSort.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 C/QuickSort.c diff --git a/C/QuickSort.c b/C/QuickSort.c new file mode 100644 index 0000000..5aca5cd --- /dev/null +++ b/C/QuickSort.c @@ -0,0 +1,46 @@ +// Quick sort in C + +#include + +void swap(int *a, int *b) { + int t = *a; + *a = *b; + *b = t; +} + +int partition(int array[], int low, int high) { + int pivot = array[high]; + int i = (low - 1); + for (int j = low; j < high; j++) { + if (array[j] <= pivot) { + i++; + swap(&array[i], &array[j]); + } + } + swap(&array[i + 1], &array[high]); + return (i + 1); +} + +void quickSort(int array[], int low, int high) { + if (low < high) { + int pi = partition(array, low, high); + quickSort(array, low, pi - 1); + quickSort(array, pi + 1, high); + } +} + +void printArray(int array[], int size) { + for (int i = 0; i < size; ++i) { + printf("%d ", array[i]); + } + printf("\n"); +} + +int main() { + int data[] = {8, 7, 2, 1, 0, 9, 6}; + int n = sizeof(data) / sizeof(data[0]); + quickSort(data, 0, n - 1); + + printf("Sorted array in ascending order: \n"); + printArray(data, n); +} \ No newline at end of file