forked from tulika-99/sorting-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
87 deletions.
There are no files selected for viewing
File renamed without changes.
52 changes: 26 additions & 26 deletions
52
...Sort-Java/bead sort in cpp/algorithm.txt → Bead Sort/BeadSort-Cpp/algorithm.txt
This file contains 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 |
---|---|---|
@@ -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 |
122 changes: 61 additions & 61 deletions
122
...dSort-Java/bead sort in cpp/beadsort.cpp → Bead Sort/BeadSort-Cpp/beadsort.cpp
This file contains 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 |
---|---|---|
@@ -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; | ||
} |
This file contains 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
* Bead Sort | ||
* Java | ||
* C | ||
* Bitonic Sort | ||
* Java | ||
* C# | ||
|