From: vanjac12@... (Van Jacques) Date: 2003-12-06T04:57:06+09:00 Subject: Re: sorting Thanks for the help. I looked at the web sites, etc., and now have plenty of info on sorting. I am also happy that my program turned out fairly well :-) BTW, Are all emails from ruby-talk posted here? Is it necessary to subscribe if one reads this newgroup instead? ============== Here is my first sort program in ruby: ************** x = Array.new i = 0 puts 'Enter some integers, end with CR.' x[0] = gets.chomp while x[i] != '' x[i+1] = gets.chomp i = i + 1 end n = x.length # Find min, put in x[0], etc. Do 2, then 3 integers 0.upto(n - 2) do |i| (i + 1).upto(n - 1) do |j| if ( x[i] <=> x[j] ) == 1 x[i],x[j]=x[j],x[i] end end end puts n.to_s puts x.each {|i| print i.to_s + " "} puts Peter wrote in message news:... > > 0.upto(n-2) |i| > > ii = i+1 > > ii.upto(n-1) |j| > > if ( x[i] <=> x[j] ) == 1 > > swap(x[i],x[j]) > > end > > end > > end > > Actually you should insert a do before |i| and |j|: > > 0.upto(n-2) do |i| > ii = i+1 > ii.upto(n-1) do |j| > if ( x[i] <=> x[j] ) == 1 > x[i],x[j]=x[j],x[i] > end > end > end > > And this version works on a few random samples (I didn't change the > algorithm, so your thinking was correct :-) > > def sort(x) > n = x.length > 0.upto(n-2) do |i| > ii = i+1 > ii.upto(n-1) do |j| > if ( x[i] <=> x[j] ) == 1 > x[i],x[j]=x[j],x[i] > end > end > end > end > > Peter