Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Bead sort
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Implementation == This implementation is written in [[Python (programming language)|Python]]; it is assumed that the {{code|input_list}} will be a sequence of integers. The function returns a new list rather than mutating the one passed in, but it can be trivially modified to operate in place efficiently. <syntaxhighlight lang="python3"> def beadsort(input_list): """Bead sort.""" return_list = [] # Initialize a 'transposed list' to contain as many elements as # the maximum value of the input -- in effect, taking the 'tallest' # column of input beads and laying it out flat transposed_list = [0] * max(input_list) for num in input_list: # For each element (each 'column of beads') of the input list, # 'lay the beads flat' by incrementing as many elements of the # transposed list as the column is tall. # These will accumulate atop previous additions. transposed_list[:num] = [n + 1 for n in transposed_list[:num]] # We've now dropped the beads. To de-transpose, we count the # 'bottommost row' of dropped beads, then mimic removing this # row by subtracting 1 from each 'column' of the transposed list. # When a column does not reach high enough for the current row, # its value in transposed_list will be <= 0. for i in range(len(input_list)): # Counting values > i is how we tell how many beads are in the # current 'bottommost row'. Note that Python's bools can be # evaluated as integers; True == 1 and False == 0. return_list.append(sum(n > i for n in transposed_list)) # The resulting list is sorted in descending order return return_list </syntaxhighlight> We can also implement the algorithm using [[Java (programming language)|Java]].<ref>{{cite web |url=https://www.geeksforgeeks.org/bead-sort-natural-sorting-algorithm/|title=Bead Sort - A Natural Sorting Algorithm |last=Nigam |first=Palash |date=10 June 2017 | publisher=GeeksForGeeks}}</ref> <syntaxhighlight lang=java> public static void beadSort(int[] a) { // Find the maximum element int max = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] > max) { max = a[i]; } } // allocating memory int[][] beads = new int[a.length][max]; // mark the beads for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i]; j++) { beads[i][j] = 1; } } // move down the beads for (int j = 0; j < max; j++) { int sum = 0; for (int i = 0; i < a.length; i++) { sum += beads[i][j]; beads[i][j] = 0; } for (int i = a.length - 1; i >= a.length - sum; i--) { a[i] = j + 1; } } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)