From 1550810ddef5721e507bec72079696650c2f7390 Mon Sep 17 00:00:00 2001 From: Tulika Tayal Date: Sun, 9 Oct 2022 23:03:16 +0530 Subject: [PATCH] restructuring bead sort --- .../bead sort in c | 0 .../algorithm.txt | 52 ++++---- .../beadsort.cpp | 122 +++++++++--------- List Of Whats done.md | 1 + 4 files changed, 88 insertions(+), 87 deletions(-) rename Bead Sort/{BeadSort-Java => BeadSort-C}/bead sort in c (100%) rename Bead Sort/{BeadSort-Java/bead sort in cpp => BeadSort-Cpp}/algorithm.txt (98%) rename Bead Sort/{BeadSort-Java/bead sort in cpp => BeadSort-Cpp}/beadsort.cpp (94%) diff --git a/Bead Sort/BeadSort-Java/bead sort in c b/Bead Sort/BeadSort-C/bead sort in c similarity index 100% rename from Bead Sort/BeadSort-Java/bead sort in c rename to Bead Sort/BeadSort-C/bead sort in c diff --git a/Bead Sort/BeadSort-Java/bead sort in cpp/algorithm.txt b/Bead Sort/BeadSort-Cpp/algorithm.txt similarity index 98% rename from Bead Sort/BeadSort-Java/bead sort in cpp/algorithm.txt rename to Bead Sort/BeadSort-Cpp/algorithm.txt index 00ef655..0a1df22 100644 --- a/Bead Sort/BeadSort-Java/bead sort in cpp/algorithm.txt +++ b/Bead Sort/BeadSort-Cpp/algorithm.txt @@ -1,27 +1,27 @@ - - """Bead sort.""" - - 1. Initialize a ' transposed_list' to contain as many elements as - 2. the maximum value of the input -- in effect, taking the 'tallest' - 3. column of input beads and laying it out flat - transposed_list = [0] * max(input_list) - 4.for num in input_list: - 1. For each element (each 'column of beads') of the input list, - 2.'lay the beads flat' by incrementing as many elements of the - 3. transposed list as the column is tall. - 4. These will accumulate atop previous additions. - transposed_list[:num] = [n + 1 for n in transposed_list[:num]] - 5. We've now dropped the beads. To de-transpose, we count the - 6. 'bottommost row' of dropped beads, then mimic removing this - 7. row by subtracting 1 from each 'column' of the transposed list. - 8. When a column does not reach high enough for the current row, - 9.its value in transposed_list will be <= 0. - 10.for _ in input_list: - 1. Counting values > 0 is how we tell how many beads are in the - 2. current 'bottommost row'. Note that Python's bools can be - 3. evaluated as integers; True == 1 and False == 0. - return_list.append(sum(n > 0 for n in transposed_list)) - 4. Remove the 'bottommost row' by subtracting 1 from each element. - transposed_list = [n - 1 for n in transposed_list] - 11. The resulting list is sorted in descending order + + """Bead sort.""" + + 1. Initialize a ' transposed_list' to contain as many elements as + 2. the maximum value of the input -- in effect, taking the 'tallest' + 3. column of input beads and laying it out flat + transposed_list = [0] * max(input_list) + 4.for num in input_list: + 1. For each element (each 'column of beads') of the input list, + 2.'lay the beads flat' by incrementing as many elements of the + 3. transposed list as the column is tall. + 4. These will accumulate atop previous additions. + transposed_list[:num] = [n + 1 for n in transposed_list[:num]] + 5. We've now dropped the beads. To de-transpose, we count the + 6. 'bottommost row' of dropped beads, then mimic removing this + 7. row by subtracting 1 from each 'column' of the transposed list. + 8. When a column does not reach high enough for the current row, + 9.its value in transposed_list will be <= 0. + 10.for _ in input_list: + 1. Counting values > 0 is how we tell how many beads are in the + 2. current 'bottommost row'. Note that Python's bools can be + 3. evaluated as integers; True == 1 and False == 0. + return_list.append(sum(n > 0 for n in transposed_list)) + 4. Remove the 'bottommost row' by subtracting 1 from each element. + transposed_list = [n - 1 for n in transposed_list] + 11. The resulting list is sorted in descending order 12. return return_list \ No newline at end of file diff --git a/Bead Sort/BeadSort-Java/bead sort in cpp/beadsort.cpp b/Bead Sort/BeadSort-Cpp/beadsort.cpp similarity index 94% rename from Bead Sort/BeadSort-Java/bead sort in cpp/beadsort.cpp rename to Bead Sort/BeadSort-Cpp/beadsort.cpp index f71997a..ade0e84 100644 --- a/Bead Sort/BeadSort-Java/bead sort in cpp/beadsort.cpp +++ b/Bead Sort/BeadSort-Cpp/beadsort.cpp @@ -1,62 +1,62 @@ - -#include -using namespace std; - -#define BEAD(i, j) beads[i * max + j] - - -void beadSort(int *a, int len) -{ - - int max = a[0]; - for (int i = 1; i < len; i++) - if (a[i] > max) - max = a[i]; - - - unsigned char beads[max*len]; - memset(beads, 0, sizeof(beads)); - - - for (int i = 0; i < len; i++) - for (int j = 0; j < a[i]; j++) - BEAD(i, j) = 1; - - for (int j = 0; j < max; j++) - { - - int sum = 0; - for (int i=0; i < len; i++) - { - sum += BEAD(i, j); - BEAD(i, j) = 0; - } - - - for (int i = len - sum; i < len; i++) - BEAD(i, j) = 1; - } - - - for (int i = 0; i < len; i++) - { - int j; - for (j = 0; j < max && BEAD(i, j); j++); - - a[i] = j; - } -} - - -int main() -{ - int a[] = {5, 3, 1, 7, 4, 1, 1, 20}; - int len = sizeof(a)/sizeof(a[0]); - - beadSort(a, len); - - for (int i = 0; i < len; i++) - printf("%d ", a[i]); - - return 0; + +#include +using namespace std; + +#define BEAD(i, j) beads[i * max + j] + + +void beadSort(int *a, int len) +{ + + int max = a[0]; + for (int i = 1; i < len; i++) + if (a[i] > max) + max = a[i]; + + + unsigned char beads[max*len]; + memset(beads, 0, sizeof(beads)); + + + for (int i = 0; i < len; i++) + for (int j = 0; j < a[i]; j++) + BEAD(i, j) = 1; + + for (int j = 0; j < max; j++) + { + + int sum = 0; + for (int i=0; i < len; i++) + { + sum += BEAD(i, j); + BEAD(i, j) = 0; + } + + + for (int i = len - sum; i < len; i++) + BEAD(i, j) = 1; + } + + + for (int i = 0; i < len; i++) + { + int j; + for (j = 0; j < max && BEAD(i, j); j++); + + a[i] = j; + } +} + + +int main() +{ + int a[] = {5, 3, 1, 7, 4, 1, 1, 20}; + int len = sizeof(a)/sizeof(a[0]); + + beadSort(a, len); + + for (int i = 0; i < len; i++) + printf("%d ", a[i]); + + return 0; } \ No newline at end of file diff --git a/List Of Whats done.md b/List Of Whats done.md index 1c1c534..e396a33 100644 --- a/List Of Whats done.md +++ b/List Of Whats done.md @@ -2,6 +2,7 @@ * Bead Sort * Java + * C * Bitonic Sort * Java * C#