How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Bash programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Bash programming language

Table of Contents

Problem Statement

An O(n2) sorting algorithm which moves elements one at a time into the correct position. The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary. To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part. Although insertion sort is an O(n2) algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases:

The algorithm is as follows (from wikipedia): Writing the algorithm for integers will suffice.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Bash programming language

Source code in the bash programming language

#!/bin/bash

# Sorting integers with insertion sort

function  insertion_sort ()
{
    # input: unsorted integer array
    # output:  sorted integer array (ascending)
    
    # local variables
    local -a arr         # array
    local -i i           # integers
    local -i j
    local -i key
    local -i prev
    local -i leftval
    local -i N          # size of array

    arr=( $@ )    # copy args into array
   
    N=${#arr[*]}  # arr extent
    readonly N    # make const

    # insertion sort 
    for (( i=1; i<$N; i++ ))  # c-style for loop
    do
	key=$((arr[$i]))      # current value
	prev=$((arr[$i-1]))   # previous value
	j=$i                  # current index
	
	while [ $j -gt 0 ]  && [ $key -lt $prev ]  # inner loop  
	do
	    arr[$j]=$((arr[$j-1])) # shift
	    
	    j=$(($j-1))            # decrement

	    prev=$((arr[$j-1]))    # last value
	    
	done
	
	arr[$j]=$(($key))          # insert key in proper order

    done

    echo ${arr[*]}                   # output sorted array
}

################
function main ()
{
    # main script
    declare -a sorted
   
    # use a default if no cmdline list 
    if [ $# -eq 0 ]; then
	arr=(10 8 20 100 -3 12 4 -5 32 0 1)
    else
	arr=($@) #cmdline list of ints
    fi

    echo
    echo "original"
    echo -e "\t ${arr[*]} \n"

    sorted=($(insertion_sort ${arr[*]}))  # call function

    echo    
    echo "sorted:"
    echo -e "\t ${sorted[*]} \n"
 }   


#script starts here
# source or run
if [[ "$0" == "bash" ]]; then # script is sourced 
    unset main
else
    main "$@"                 # call with cmdline args 
fi

#END


  

You may also check:How to resolve the algorithm Loops/For 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 Morse code step by step in the bash programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Bash programming language
You may also check:How to resolve the algorithm Fractran step by step in the bash programming language