Thursday, July 29, 2010

Insertion Sort Shell Script


#!/bin/bash#!/bin/bash
# SCRIPT: insertionsort.sh
#
# LOGIC: Here, sorting takes place by inserting a particular element
# at the appropriate position, that’s why the name insertion sorting.
# In the First iteration, second element ARRAY[1] is compared with
# the first element ARRAY[0]. In the second iteration third element
# is compared with first and second element. In general, in every
# iteration an element is compared with all the elements before it.
# While comparing if it is found that the element can be inserted at
# a suitable position, then space is created for it by shifting the
# other elements one position up and inserts the desired element at
# the suitable position. This procedure is repeated for all the
# elements in the list.
#
#####################################################################
# Define Functions Here #
#####################################################################

printnumbers()
{
echo ${ARRAY[*]}
}

sortnumbers()
{
for((i=1;i<count;i++))
do
Temp=${ARRAY[i]}
j=$((i-1))
while [ $Temp -lt ${ARRAY[j]} ]
do
ARRAY[j+1]=${ARRAY[j]}
let j--
if [ $j == -1 ]
then
break
fi
done
ARRAY[j+1]=$Temp
done
}

#####################################################################
# Variable Initialization #
#####################################################################

echo "Enter numbers to be sorted"

read -a ARRAY

count=${#ARRAY[@]}

#####################################################################
# Main Script Starts Here #
#####################################################################

echo "---------------------------------------------------------------"

echo "Numbers Before Sort:"

printnumbers

sortnumbers

echo "Numbers After Sort: "

printnumbers

echo "---------------------------------------------------------------"



OUTPUT:

[root@www blog]# sh insertionsort.sh
Enter Numbers to be Sorted :
12 76 34 -34 67 9 -56 5 99 -3 17
---------------------------------------------------------------
Numbers Before Sort:
12 76 34 -34 67 9 -56 5 99 -3 17
Numbers After Sort:
-56 -34 -3 5 9 12 17 34 67 76 99
---------------------------------------------------------------

NOTE: If we complement the while condition in this program, it will
give out the sorted array in descending order.

7 comments:

  1. thanks a lot . the answers were useful & understandable.

    ReplyDelete
  2. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Android training in Chennai | Android course in Chennai

    ReplyDelete
  3. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/insertion-sorting-program-in-cpp-with-example-and-explanation


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  4. This website was… how do I say it? Relevant!!
    Finally I’ve found something which helped me.
    Thank you! Jaket Parka | Grosir Jaket Parka | Grosir Jaket Parka | Grosir Jaket Parka

    ReplyDelete
  5. Payday advances are a transient loan, when it is required the most...usually between paydays! Payday credits are an awesome method to get some money to hold you over to your next paycheck. The objective of payday advances is to help you with accounts to meet that unforseen prerequisite.
    Payday Loans

    ReplyDelete