How to resolve the algorithm Sorting algorithms/Bead sort step by step in the XPL0 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Bead sort step by step in the XPL0 programming language
Table of Contents
Problem Statement
Sort an array of positive integers using the Bead Sort Algorithm. A bead sort is also known as a gravity sort.
Algorithm has 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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Bead sort step by step in the XPL0 programming language
Source code in the xpl0 programming language
include c:\cxpl\codes;
proc BeadSort(Array, Length); \Sort Array into increasing order
int Array, Length; \Array contents range 0..31; number of items
int Row, I, J, T, C;
[Row:= Reserve(Length*4); \each Row has room for 32 beads
for I:= 0 to Length-1 do \each Row gets Array(I) number of beads
Row(I):= ~-1<
for J:= 1 to Length-1 do
for I:= Length-1 downto J do
[T:= Row(I-1) & ~Row(I); \up to 31 beads fall in a single pass
Row(I-1):= Row(I-1) | T; \(|=xor, !=or)
Row(I):= Row(I) | T;
];
for I:= 0 to Length-1 do \count beads in each Row
[C:= 0; T:= Row(I);
while T do
[if T&1 then C:= C+1; T:= T>>1];
Array(I):= C; \count provides sorted order
];
];
int A, I;
[A:= [3, 1, 4, 1, 25, 9, 2, 6, 5, 0];
BeadSort(A, 10);
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )];
]
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Ada programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Call a foreign-language function step by step in the Maxima programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Modula-3 programming language