insertion sort???


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers insertion sort???
# 1  
Old 04-15-2008
insertion sort???

Hi, I was wondering if someone here could help me figure out what's wrong with this simple insertion sort shell script. This is the output I get when I try to run it:

"23 43 22 15 63 43 23 11 10 2
./insertion.sh: line 23: [: -gt: unary operator expected
2 63 63 63 63 63 63 63 63 63 63"

The test file I use looks like this:
Code:
23
43
22
15
63
43
23
11
10
2

And here's the script:

Code:
#!/bin/bash

#read in the data from a file

echo "Enter file to be sorted: "
read file

a=1
while read myline; do
    array[$a]=$myline
    a=`expr $a + 1`
done < $file

#display the unsorted data
echo ${array[*]}

#sort the data
b=1
while [ $b -lt $a ]; do
    index=${array[$b]}
    c=$b
    z=`expr $c - 1`
    while [ $c -gt 0 ] && [ ${array[$z]} -gt $index ]; do
        array[$c]=${array[$z]}
        c=`expr $c - 1`
    done
    array[$c]=$index
    b=`expr $b + 1`
done

#display the sorted data
echo ${array[*]}


Last edited by Yogesh Sawant; 04-15-2008 at 02:31 AM.. Reason: added code tags
# 2  
Old 04-15-2008
You gotta do debugging yourself.

${array[$z]} could be zero.
#do this
echo ${array[$z]}
# 3  
Old 04-16-2008
Problems look to be with your inner while loop. c -gt 0 won't work - your array starts with a[1]. That's the 'unary operator' error.
Also... you need to paste the $z value in the inner loop or it won't change as $c does.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Insertion Sort Error in C++

TD P { margin-bottom: 0in; }P { margin-bottom: 0.08in; }TT.cjk { font-family: "WenQuanYi Zen Hei Sharp",monospace; }A:link { } I am trying to run this program for Insertion Sort, But don't know that why I am getting following Error #include<iostream> int main(){ int i,j,key,n;... (4 Replies)
Discussion started by: liveproject101
4 Replies

2. UNIX for Dummies Questions & Answers

Simple, I think, VIM Insertion

Hey everyone, I recently had to learn how to use UNIX and VIM for an internship, and have run in to a little challenge today. I have lists of thousands of file names that represent CAS (Chemical Abstracts Service) numbers, but they are not formatted correctly. CAS numbers run in the form of... (2 Replies)
Discussion started by: Stuart Ness
2 Replies

3. Ubuntu

Random insertion of letters to password

Hi,, Here i have attached a text file where iam facing problem in my code. Please read the file and help me out of this issue.. Thanks in advance (4 Replies)
Discussion started by: vanid
4 Replies

4. Shell Programming and Scripting

Insertion into csv

I want to use bash to insert the variable $a into the cell A1 of a csv file mycsv.csv. How do I insert a variable into a specific cell in a csv file? (1 Reply)
Discussion started by: locoroco
1 Replies

5. Programming

C: Binary Insertion Sort Optimization: Scoreboard

Hello, As part of a scheme to determine the top 10 5x5 Boggle board configurations beyond a reasonable doubt, I set out to optimize every aspect of the problem, and this is part one of a series of web pages that I am going to publish shortly... Optimal Binary Insertion Sort - Scoreboard Is... (0 Replies)
Discussion started by: HeavyJ
0 Replies

6. Shell Programming and Scripting

Insertion in a file

Hi All, I want to insert a line just befor the lst line in a file. please can anyone advise. Cheers, Shazin (3 Replies)
Discussion started by: Shazin
3 Replies

7. Ubuntu

how can insertion of one module deletesother modules

Masters, I have inserted a linux module which controlls the bandwidth of LAN . Just when I insmod'ed kernel panicked. When restarted I am stunned to find out that many of my other modules are missing. How can insertion of one module removes other modules . I use 2.6.9 kernel on a redhat 3.4.3... (1 Reply)
Discussion started by: iamjayanth
1 Replies

8. Shell Programming and Scripting

Insertion of Header record

A header record is to be inserted in the begining of a flat file without using extra file or new file. It should be inserted into same file. Advace thanks for all help... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

9. Shell Programming and Scripting

automatic header insertion

hi Is there any way to automatically insert a predefined header into a file? It would include the file name, author name, date and a description, the description entered on the first line of vi. Thanks for any help Oliver (4 Replies)
Discussion started by: olimiles
4 Replies

10. UNIX for Advanced & Expert Users

Insertion of Leap Second

Hi All, We are running the HP-UX 11.11 and Linux AS 3.0. so, shall we need to make any changes for leap second i.e. insert the leap second on 1st Jan 2006 or does the system have some setup which would take care of this automatically. Please advise. Regards, Inder (2 Replies)
Discussion started by: isingh786
2 Replies
Login or Register to Ask a Question