KSH - Selection Sort Error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH - Selection Sort Error
# 1  
Old 12-05-2008
Power KSH - Selection Sort Error

I have 'translated' selection sort from java to KSH, here is my code

Code:
	#1. get the inputs and put into arr[]
	print "Enter the integers (separated by a space):"
	read integers
	set -A arr $integers
	#2. start sorting process, using selection sort
	min=0
	tmp=0
	i=0
	while test $i -lt ${#arr[*]}
	do
		min=$i
		(( j = $i + 1 ))
		while test $j -lt ${#arr[*]}
		do
			if test ${arr[$j]} -lt ${arr[$min]}
			then
				min=$j
			fi
			(( j += 1 ))
		done
		tmp=${arr[$i]}
		arr[$i]=${arr[$min]}
		arr[$min]=$tmp
		(( i += 1 ))
	done
	#3. print sorted arr[]
	print "The sorted result is:"
	print ${arr[*]}

The problem is when I input:
23 7 -9 45 62 102 -42 3 -7 -78

the output is like:
The sorted result is:
-42 -9 -7 3 7 23 45 62 102

arr[0] is just gone... it works in java code though.Smilie
Some help would be nice...Smilie
# 2  
Old 12-06-2008
You did not specify what version of ksh you are using so I will assume ksh93. Try the following ....
Code:
#!/usr/bin/ksh93

print "Enter the integers (separated by a space):"
read -A arr

integer min=0, tmp=0, i=0

while (( i < ${#arr[*]} ))
do
    min=i
    (( j = i + 1 ))
    while (( j < ${#arr[*]} ))
    do
        (( ${arr[$j]} < ${arr[$min]} )) && min=j
        (( j++ ))
    done
    tmp=${arr[$i]}
    arr[$i]=${arr[$min]}
    arr[$min]=$tmp
    (( i++ ))
done

print "The sorted result is:"
print -- "${arr[*]}"

Output for you sample string of numbers is
Code:
The sorted result is:
-78 -42 -9 -7 3 7 23 45 62 102

# 3  
Old 12-06-2008
what /bin/ksh | grep Version
Version M-11/16/88i

I suppose it's ksh88
University's Solaris server.

I have modified your code and it works now! Thanks!
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. Shell Programming and Scripting

[Solved] Help with Sort command (ksh)

hello everyone, I have file with de-limited values in random order shown below, where C1V0 represents column1 value1, C2V0 represents column2 value 2, and so on. Column0 is a constant value which i didn't represent in the sample input format. Column1 is a numeric column. Column2 is a... (0 Replies)
Discussion started by: angie1234
0 Replies

3. Shell Programming and Scripting

Receiving error: ./ang.ksh[35]: 0403-057 Syntax error at line 116 : `done' is not expected.

Hi All I am quite new to Unix. Following is a shell script that i have written and getting the subject mentioned error. #!/bin/ksh #------------------------------------------------------------------------- # File: ang_stdnld.ksh # # Desc: UNIX shell script to extract Store information.... (3 Replies)
Discussion started by: amitsinha
3 Replies

4. Homework & Coursework Questions

Making a sort error

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I need to show when stderror would be written to save1 and when it would be written to save2 2. Relevant... (0 Replies)
Discussion started by: snag49ers
0 Replies

5. Shell Programming and Scripting

ksh ./sort.sh: not found

I've a simple .sh file: #!/bin/ksh sort -t: -k 1.13,1.13 -k 1.16,1.29 sort.txt > output.txt When i try to run this sort.sh file ./sort.sh it gives me the error: ksh: ./sort.sh: not found I've tried the following: 1) echo $SHELL /bin/ksh 2) echo $PATH... (4 Replies)
Discussion started by: kajal_ind
4 Replies

6. Shell Programming and Scripting

AWK or KSH : Sort, Group and extract from 3 files

Hi, I've the following two CSV files: File1.csv File2.csv Class,Student# Student#,Marks 1001,6001 6002,50 1001,6002 6001,60 1002,7000 ... (3 Replies)
Discussion started by: Matrix2682
3 Replies

7. Shell Programming and Scripting

Numeric sort error

Hello all I have data like below where the column with values (PRI, SEC ) is the char field and the rest are Numeric Fields. 200707,9580,58,7,2,1,PRI,1,1,137,205594,0,5,10,-45.51,-45.51 200707,9580,58,7,2,1,SEC,1,1,137,205594,0,5,10,-45.51,45.51... (1 Reply)
Discussion started by: vasuarjula
1 Replies

8. Shell Programming and Scripting

Variable Selection Operation Error

Ok i have taken your advised indented my code and i have managed to fix my problem but unfortuantely now another small one has arisen. The problem is that executing my commands requires two presses of the ENTER key as opposed to the originally being pressed once as one would expect, for example... (1 Reply)
Discussion started by: warlock129
1 Replies

9. Shell Programming and Scripting

sort unexpected error?

I have the following script: mysort.sh: #!/bin/ksh for i in `ls` sort -bfu $i > sort_$i wait mv sort_$i $i wait done exit 0 I get the following error: mysort.sh: syntax error at line 3 : `sort' unexpected Does anybody know what I am missing here. ... (2 Replies)
Discussion started by: radhika
2 Replies

10. Shell Programming and Scripting

sort can't stat error.

I am using the command below to sort unique a file and I keep getting this error that sort can't stat. Error: sort: can't stat /xxxx/xxxxx/2005/xxxxxx/out_20050602155231/test.txt: No such file or directory Does anybody have an idea when this error could occur. Thanks, Radhika. if ... (5 Replies)
Discussion started by: radhika
5 Replies
Login or Register to Ask a Question