Skip to content

Commit

Permalink
restructuring bead sort
Browse files Browse the repository at this point in the history
  • Loading branch information
tulika-99 committed Oct 9, 2022
1 parent c2e5d6a commit 1550810
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 87 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@

#include <bits/stdc++.h>
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 <bits/stdc++.h>
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;
}
1 change: 1 addition & 0 deletions List Of Whats done.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Bead Sort
* Java
* C
* Bitonic Sort
* Java
* C#
Expand Down

0 comments on commit 1550810

Please sign in to comment.