fbpx
Wikipedia

Bead sort

Bead sort, also called gravity sort, is a natural sorting algorithm, developed by Joshua J. Arulanandham, Cristian S. Calude and Michael J. Dinneen in 2002, and published in The Bulletin of the European Association for Theoretical Computer Science.[1] Both digital and analog hardware implementations of bead sort can achieve a sorting time of O(n); however, the implementation of this algorithm tends to be significantly slower in software and can only be used to sort lists of positive integers. Also, it would seem that even in the best case, the algorithm requires O(n2) space.

Algorithm overview edit

 
Step 1: Suspended beads on vertical poles.
 
Step 2: The beads have been allowed to fall.

The bead sort operation can be compared to the manner in which beads slide on parallel poles, such as on an abacus. However, each pole may have a distinct number of beads. Initially, it may be helpful to imagine the beads suspended on vertical poles. In Step 1, such an arrangement is displayed using n=5 rows of beads on m=4 vertical poles. The numbers to the right of each row indicate the number that the row in question represents; rows 1 and 2 are representing the positive integer 3 (because they each contain three beads) while the top row represents the positive integer 2 (as it only contains two beads).[notes 1]

If we then allow the beads to fall, the rows now represent the same integers in sorted order. Row 1 contains the largest number in the set, while row n contains the smallest. If the above-mentioned convention of rows containing a series of beads on poles 1..k and leaving poles k+1..m empty has been followed, it will continue to be the case here.

The action of allowing the beads to "fall" in our physical example has allowed the larger values from the higher rows to propagate to the lower rows. If the value represented by row a is smaller than the value contained in row a+1, some of the beads from row a+1 will fall into row a; this is certain to happen, as row a does not contain beads in those positions to stop the beads from row a+1 from falling.

The mechanism underlying bead sort is similar to that behind counting sort; the number of beads on each pole corresponds to the number of elements with value equal or greater than the index of that pole.

Complexity edit

Bead sort can be implemented with four general levels of complexity, among others:

  • O(1): The beads are all moved simultaneously in the same time unit, as would be the case with the simple physical example above. This is an abstract complexity, and cannot be implemented in practice.
  • O( ): In a realistic physical model that uses gravity, the time it takes to let the beads fall is proportional to the square root of the maximum height, which is proportional to n.
  • O(n): The beads are moved one row at a time. This is the case used in the analog and digital hardware solutions.
  • O(S), where S is the sum of the integers in the input set: Each bead is moved individually. This is the case when bead sort is implemented without a mechanism to assist in finding empty spaces below the beads, such as in software implementations.

Like the Pigeonhole sort, bead sort is unusual in that in worst case it can perform faster than O(n log n), the fastest performance possible for a comparison sort in worst case. This is possible because the key for a bead sort is always a positive integer and bead sort exploits its structure.

Implementation edit

This implementation is written in Python; it is assumed that the 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.

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 

We can also implement the algorithm using Java.[2]

 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;  }  }  } 

Notes edit

  1. ^ By convention, a row representing the positive integer k should have beads on poles 1..k and poles k+1..m should be empty. This is not a strict requirement, but will most likely simplify implementation.

References edit

  1. ^ Arulanandham, Joshua J.; Calude, Cristian S.; Dinneen, Michael J. (January 2002). "Bead-Sort: A Natural Sorting Algorithm" (PDF). Department of Computer Science, University of Auckland. Retrieved 2021-05-14.
  2. ^ Nigam, Palash. "Bead Sort - A Natural Sorting Algorithm". GeeksForGeeks.

External links edit

  • (PDF). Archived from the original (PDF) on 2017-08-09. Retrieved 2005-01-01. (114 KiB)
  • Bead Sort in MGS, a visualization of a bead sort implemented in the MGS programming language
  • Bead Sort on MathWorld
  • Bead Sort interactive visualization

bead, sort, this, article, multiple, issues, please, help, improve, discuss, these, issues, talk, page, learn, when, remove, these, template, messages, this, article, needs, additional, citations, verification, please, help, improve, this, article, adding, cit. This article has multiple issues Please help improve it or discuss these issues on the talk page Learn how and when to remove these template messages This article needs additional citations for verification Please help improve this article by adding citations to reliable sources Unsourced material may be challenged and removed Find sources Bead sort news newspapers books scholar JSTOR October 2017 Learn how and when to remove this template message The topic of this article may not meet Wikipedia s general notability guideline Please help to demonstrate the notability of the topic by citing reliable secondary sources that are independent of the topic and provide significant coverage of it beyond a mere trivial mention If notability cannot be shown the article is likely to be merged redirected or deleted Find sources Bead sort news newspapers books scholar JSTOR October 2017 Learn how and when to remove this template message Learn how and when to remove this template message Bead sort also called gravity sort is a natural sorting algorithm developed by Joshua J Arulanandham Cristian S Calude and Michael J Dinneen in 2002 and published in The Bulletin of the European Association for Theoretical Computer Science 1 Both digital and analog hardware implementations of bead sort can achieve a sorting time of O n however the implementation of this algorithm tends to be significantly slower in software and can only be used to sort lists of positive integers Also it would seem that even in the best case the algorithm requires O n2 space Contents 1 Algorithm overview 2 Complexity 3 Implementation 4 Notes 5 References 6 External linksAlgorithm overview edit nbsp Step 1 Suspended beads on vertical poles nbsp Step 2 The beads have been allowed to fall The bead sort operation can be compared to the manner in which beads slide on parallel poles such as on an abacus However each pole may have a distinct number of beads Initially it may be helpful to imagine the beads suspended on vertical poles In Step 1 such an arrangement is displayed using n 5 rows of beads on m 4 vertical poles The numbers to the right of each row indicate the number that the row in question represents rows 1 and 2 are representing the positive integer 3 because they each contain three beads while the top row represents the positive integer 2 as it only contains two beads notes 1 If we then allow the beads to fall the rows now represent the same integers in sorted order Row 1 contains the largest number in the set while row n contains the smallest If the above mentioned convention of rows containing a series of beads on poles 1 k and leaving poles k 1 m empty has been followed it will continue to be the case here The action of allowing the beads to fall in our physical example has allowed the larger values from the higher rows to propagate to the lower rows If the value represented by row a is smaller than the value contained in row a 1 some of the beads from row a 1 will fall into row a this is certain to happen as row a does not contain beads in those positions to stop the beads from row a 1 from falling The mechanism underlying bead sort is similar to that behind counting sort the number of beads on each pole corresponds to the number of elements with value equal or greater than the index of that pole Complexity editBead sort can be implemented with four general levels of complexity among others O 1 The beads are all moved simultaneously in the same time unit as would be the case with the simple physical example above This is an abstract complexity and cannot be implemented in practice O n displaystyle sqrt n nbsp In a realistic physical model that uses gravity the time it takes to let the beads fall is proportional to the square root of the maximum height which is proportional to n O n The beads are moved one row at a time This is the case used in the analog and digital hardware solutions O S where S is the sum of the integers in the input set Each bead is moved individually This is the case when bead sort is implemented without a mechanism to assist in finding empty spaces below the beads such as in software implementations Like the Pigeonhole sort bead sort is unusual in that in worst case it can perform faster than O n log n the fastest performance possible for a comparison sort in worst case This is possible because the key for a bead sort is always a positive integer and bead sort exploits its structure Implementation editThis implementation is written in Python it is assumed that the 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 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 lt 0 for i in range len input list Counting values gt 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 gt i for n in transposed list The resulting list is sorted in descending order return return list We can also implement the algorithm using Java 2 public static void beadSort int a Find the maximum element int max a 0 for int i 1 i lt a length i if a i gt max max a i allocating memory int beads new int a length max mark the beads for int i 0 i lt a length i for int j 0 j lt a i j beads i j 1 move down the beads for int j 0 j lt max j int sum 0 for int i 0 i lt a length i sum beads i j beads i j 0 for int i a length 1 i gt a length sum i a i j 1 Notes edit By convention a row representing the positive integer k should have beads on poles 1 k and poles k 1 m should be empty This is not a strict requirement but will most likely simplify implementation References edit Arulanandham Joshua J Calude Cristian S Dinneen Michael J January 2002 Bead Sort A Natural Sorting Algorithm PDF Department of Computer Science University of Auckland Retrieved 2021 05 14 Nigam Palash Bead Sort A Natural Sorting Algorithm GeeksForGeeks External links edit Bead Sort A Natural Sorting Algorithm PDF Archived from the original PDF on 2017 08 09 Retrieved 2005 01 01 114 KiB Bead Sort in MGS a visualization of a bead sort implemented in the MGS programming language Bead Sort on MathWorld Bead Sort interactive visualization Retrieved from https en wikipedia org w index php title Bead sort amp oldid 1162370135, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.