Find the answer to your Linux question:
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.
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    3

    Unhappy 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

  2. #2
    Just Joined!
    Join Date
    Feb 2010
    Posts
    18
    Hi,

    to sort, there's the command "sort"

    bye
    giammy

  3. #3
    Just Joined!
    Join Date
    Feb 2010
    Posts
    3

    Unhappy

    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 ....?????

  4. $spacer_open
    $spacer_close
  5. #4
    Just 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
    giammy
    Last edited by oz; 02-17-2010 at 11:00 PM. Reason: removed spam

  6. #5
    Linux 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

    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
    for and do are built-in bash commands.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •