Results 1 to 5 of 5
This code is not working ..pls help me...
#!/bin/sh
#sort an array using insertion sort
echo "Enter the limit of the array"
read n
echo " enter the elements of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-09-2010 #1Just Joined!
- Join Date
- Feb 2010
- Posts
- 3
insertion sort shell script
This code is not working ..pls help me...
#!/bin/sh
#sort an array using insertion sort
echo "Enter the limit of the array"
read n
echo " enter the elements of the array"
for ((i=0; i<n; i++)) do
read A[$i]
done
#Display the unsorted array
echo "The entered array is"
for ((i=0; i<n; i++)) do
echo ${A[$i]}
done
#perform insertion sort on the array
for ((i=1; i<=n; i++)) do
v=${A[$i]}
j=` expr $i - 1`
while [ $j -ge 0 ] do
while [ ${A[$j]} -gt $v ] do
c=` expr $j + 1`
A[$c]=${A[$j]}
j=` expr $j - 1`
done
done
d=` expr $j + 1`
A[$d]=$v
done
#display the sorted array
echo "The sorted array is"
for ((i=0; i<n; i++)) do
echo ${A[$i]}
done
Its showing error::unexpected token done
- 02-12-2010 #2Just Joined!
- Join Date
- Feb 2010
- Posts
- 18
Hi,
to sort, there's the command "sort"
bye
giammy
- 02-14-2010 #3Just Joined!
- Join Date
- Feb 2010
- Posts
- 3
Thank u...but i wanted 2 know the mistake in my script...especially ..about multiple conditions within the while loop and regarding the array indexes within that.....can u pls help me ....?????
- 02-14-2010 #4Just Joined!
- Join Date
- Feb 2010
- Posts
- 18
sorry,
I never use bash in such I way as I get in a mess myself: what
I can suggest is to put an echo on every line (to simulate a
sort of debugger) showing what's going on (such as the values of variables)
bye
giammyLast edited by oz; 02-17-2010 at 10:00 PM. Reason: removed spam
- 02-14-2010 #5Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
I didn't run the whole script but you have a syntax error in all your for statements
for and do are built-in bash commands.Code:for ((i=0; i<n; i++)) do #should be written for ((i=0; i<n; i++)); do #or for ((i=0; i<n; i++)) do
0 + 1 = 1 != 2 <> 3 != 4 ...
Until the camel can pass though the eye of the needle.


Reply With Quote
