How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Kotlin programming language
How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Kotlin 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 Kotlin programming language
Bead Sort is a non-comparative sorting algorithm that uses binary beads to sort an array of integers. It is a simple yet effective algorithm, particularly for sorting small arrays.
Implementation Details:
-
** Initialization**:
n
: Size of the input arraya
.max
: Maximum element in the array.beads
: A byte array representing the beads. Each bead is initially set to0
.
-
Bead Marking:
- For each element
a[i]
in the input array, marka[i]
beads on each post, starting from the bottom. This represents the number of beads for each element.
- For each element
-
Bead Counting:
- For each post (column)
j
:- Count the number of beads on that post (
sum
). - Reset the beads on that post to
0
. - Mark the bottom
sum
beads on that post as1
.
- Count the number of beads on that post (
- For each post (column)
-
Bead Interpretation:
- For each element
a[i]
:- Find the first post
j
where the bead is1
. - Assign
a[i]
the value ofj
.
- Find the first post
- For each element
-
Example:
- Input array:
[5, 3, 1, 7, 4, 1, 1, 20]
- Bead marking:
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
- Bead counting and marking:
0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- Bead interpretation:
a[0] = 4
(4 beads on the 4th post)a[1] = 2
(2 beads on the 2nd post)a[2] = 0
(0 beads on any post)a[3] = 6
(6 beads on the 6th post)a[4] = 3
(3 beads on the 3rd post)a[5] = 0
(0 beads on any post)a[6] = 0
(0 beads on any post)a[7] = 7
(7 beads on the 7th post)
- Input array:
-
Sorted Output:
[0, 0, 1, 2, 3, 4, 6, 7]
Properties of Bead Sort:
- Time Complexity: O(n + max), where
n
is the size of the array andmax
is the maximum element. - Space Complexity: O(n * max), for the bead array.
- Stability: Not stable, as elements with the same value may be reordered.
- Limitations: Suitable for small arrays and limited by the maximum value in the array.
Applications:
Bead Sort finds applications in situations where the input data is small and has a limited range of values, such as:
- Sorting small arrays
- Bucketing elements into ranges
Source code in the kotlin programming language
// version 1.1.2
fun beadSort(a: IntArray) {
val n = a.size
if (n < 2) return
var max = a.max()!!
val beads = ByteArray(max * n)
/* mark the beads */
for (i in 0 until n)
for (j in 0 until a[i])
beads[i * max + j] = 1
for (j in 0 until max) {
/* count how many beads are on each post */
var sum = 0
for (i in 0 until n) {
sum += beads[i * max + j]
beads[i * max + j] = 0
}
/* mark bottom sum beads */
for (i in n - sum until n) beads[i * max + j] = 1
}
for (i in 0 until n) {
var j = 0
while (j < max && beads[i * max + j] == 1.toByte()) j++
a[i] = j
}
}
fun main(args: Array<String>) {
val a = intArrayOf(5, 3, 1, 7, 4, 1, 1, 20)
println("Before sorting : ${a.contentToString()}")
beadSort(a)
println("After sorting : ${a.contentToString()}")
}
You may also check:How to resolve the algorithm Function definition step by step in the HolyC programming language
You may also check:How to resolve the algorithm Morse code step by step in the C++ programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the zkl programming language
You may also check:How to resolve the algorithm Leonardo numbers step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the X86 Assembly programming language