#!/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.
Thursday, July 29, 2010
Posted by venu k
7 comments | 9:34 AM
Subscribe to:
Post Comments (Atom)
thanks a lot . the answers were useful & understandable.
ReplyDeleteExcellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeleteAndroid training in Chennai | Android course in Chennai
Very informative article.Thank you author for posting this kind of article .
ReplyDeletehttp://www.wikitechy.com/view-article/insertion-sorting-program-in-cpp-with-example-and-explanation
Both are really good,
Cheers,
Venkat
This website was… how do I say it? Relevant!!
ReplyDeleteFinally I’ve found something which helped me.
Thank you! Jaket Parka | Grosir Jaket Parka | Grosir Jaket Parka | Grosir Jaket Parka
Good and interesting article . thanks for sharing your post.
ReplyDeleteAndroid Training in chennai | Android course in chennai
Thank you for sharing the post! gmail account login
ReplyDeletePayday 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.
ReplyDeletePayday Loans