How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the bash programming language
How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the bash programming language
Table of Contents
Problem Statement
A bubble sort is generally considered to be the simplest sorting algorithm.
A bubble sort is also known as a sinking sort.
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
Because of its abysmal O(n2) performance, it is not used often for large (or even medium-sized) datasets.
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.
A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
This can be expressed in pseudo-code as follows (assuming 1-based indexing):
Sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the bash programming language
Source code in the bash programming language
$ function bubble_sort() {
local a=("$@")
local n
local i
local j
local t
ft=(false true)
n=${#a[@]} # array length
i=n
while ${ft[$(( 0 < i ))]}
do
j=0
while ${ft[$(( j+1 < i ))]}
do
if ${ft[$(( a[j+1] < a[j] ))]}
then
t=${a[j+1]}
a[j+1]=${a[j]}
a[j]=$t
fi
t=$(( ++j ))
done
t=$(( --i ))
done
echo ${a[@]}
}
> > > > > > > > > > > > > > > > > > > > > > > > > $ # this line output from bash
$ bubble_sort 3 2 8
2 3 8
$ # create an array variable
$ a=(2 45 83 89 1 82 69 88 112 99 0 82 58 65 782 74 -31 104 4 2)
$ bubble_sort ${a[@]}
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
$ b=($( bubble_sort ${a[@]} ) )
$ echo ${#b[@]}
20
$ echo ${b[@]}
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
$
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the Bash programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the bash programming language
You may also check:How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the bash programming language
You may also check:How to resolve the algorithm Array length step by step in the Bash programming language
You may also check:How to resolve the algorithm 21 game step by step in the bash programming language