From d349660a61005f0d50ec84c759ba2aeaa3abae3f Mon Sep 17 00:00:00 2001 From: anushka344 <73011298+anushka344@users.noreply.github.com> Date: Mon, 18 Oct 2021 11:16:43 +0530 Subject: [PATCH 1/2] Add files via upload --- bucket_sort.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 bucket_sort.c diff --git a/bucket_sort.c b/bucket_sort.c new file mode 100644 index 0000000..b0031aa --- /dev/null +++ b/bucket_sort.c @@ -0,0 +1,154 @@ +// Bucket sort in C + +#include +#include + +#define NARRAY 7 // Array size +#define NBUCKET 6 // Number of buckets +#define INTERVAL 10 // Each bucket capacity + +struct Node { + int data; + struct Node *next; +}; + +void BucketSort(int arr[]); +struct Node *InsertionSort(struct Node *list); +void print(int arr[]); +void printBuckets(struct Node *list); +int getBucketIndex(int value); + +// Sorting function +void BucketSort(int arr[]) { + int i, j; + struct Node **buckets; + + // Create buckets and allocate memory size + buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET); + + // Initialize empty buckets + for (i = 0; i < NBUCKET; ++i) { + buckets[i] = NULL; + } + + // Fill the buckets with respective elements + for (i = 0; i < NARRAY; ++i) { + struct Node *current; + int pos = getBucketIndex(arr[i]); + current = (struct Node *)malloc(sizeof(struct Node)); + current->data = arr[i]; + current->next = buckets[pos]; + buckets[pos] = current; + } + + // Print the buckets along with their elements + for (i = 0; i < NBUCKET; i++) { + printf("Bucket[%d]: ", i); + printBuckets(buckets[i]); + printf("\n"); + } + + // Sort the elements of each bucket + for (i = 0; i < NBUCKET; ++i) { + buckets[i] = InsertionSort(buckets[i]); + } + + printf("-------------\n"); + printf("Bucktets after sorting\n"); + for (i = 0; i < NBUCKET; i++) { + printf("Bucket[%d]: ", i); + printBuckets(buckets[i]); + printf("\n"); + } + + // Put sorted elements on arr + for (j = 0, i = 0; i < NBUCKET; ++i) { + struct Node *node; + node = buckets[i]; + while (node) { + arr[j++] = node->data; + node = node->next; + } + } + + return; +} + +// Function to sort the elements of each bucket +struct Node *InsertionSort(struct Node *list) { + struct Node *k, *nodeList; + if (list == 0 || list->next == 0) { + return list; + } + + nodeList = list; + k = list->next; + nodeList->next = 0; + while (k != 0) { + struct Node *ptr; + if (nodeList->data > k->data) { + struct Node *tmp; + tmp = k; + k = k->next; + tmp->next = nodeList; + nodeList = tmp; + continue; + } + + for (ptr = nodeList; ptr->next != 0; ptr = ptr->next) { + if (ptr->next->data > k->data) + break; + } + + if (ptr->next != 0) { + struct Node *tmp; + tmp = k; + k = k->next; + tmp->next = ptr->next; + ptr->next = tmp; + continue; + } else { + ptr->next = k; + k = k->next; + ptr->next->next = 0; + continue; + } + } + return nodeList; +} + +int getBucketIndex(int value) { + return value / INTERVAL; +} + +void print(int ar[]) { + int i; + for (i = 0; i < NARRAY; ++i) { + printf("%d ", ar[i]); + } + printf("\n"); +} + +// Print buckets +void printBuckets(struct Node *list) { + struct Node *cur = list; + while (cur) { + printf("%d ", cur->data); + cur = cur->next; + } +} + +// Driver code +int main(void) { + int array[NARRAY] = {42, 32, 33, 52, 37, 47, 51}; + + printf("Initial array: "); + print(array); + printf("-------------\n"); + + BucketSort(array); + printf("-------------\n"); + printf("Sorted array: "); + print(array); + return 0; +} \ No newline at end of file From c120845f137c91f9962961f8a70e3f7f7bc9734f Mon Sep 17 00:00:00 2001 From: anushka344 <73011298+anushka344@users.noreply.github.com> Date: Mon, 18 Oct 2021 11:21:42 +0530 Subject: [PATCH 2/2] Add files via upload --- stooge_sort.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 stooge_sort.cpp diff --git a/stooge_sort.cpp b/stooge_sort.cpp new file mode 100644 index 0000000..3e76ea6 --- /dev/null +++ b/stooge_sort.cpp @@ -0,0 +1,49 @@ +// C++ code to implement stooge sort +#include +using namespace std; + +// Function to implement stooge sort +void stoogesort(int arr[],int l, int h) +{ + if (l >= h) + return; + + // If first element is smaller than last, + // swap them + if (arr[l] > arr[h]) + swap(arr[l], arr[h]); + + // If there are more than 2 elements in + // the array + if(h-l+1>2) + { + int t = (h-l+1)/3; + + // Recursively sort first 2/3 elements + stoogesort(arr, l, h-t); + + // Recursively sort last 2/3 elements + stoogesort(arr, l+t, h); + + // Recursively sort first 2/3 elements + // again to confirm + stoogesort(arr, l, h-t); + } +} + + +int main() +{ + int arr[] = {2, 4, 5, 3, 1}; + int n = sizeof(arr)/sizeof(arr[0]); + + // Calling Stooge Sort function to sort + // the array + stoogesort(arr, 0, n-1); + + // Display the sorted array + for (int i=0; i