在红宝石排序程序 [英] Sorting program in ruby
问题描述
基本上我想在Ruby中创建一个基本排序程序,而无需使用的的.sort 的。
这里是我的code迄今:
高清optsort
@@一个= 0
@@ AA = 1
@@ B = unsorted.size
@@ smallestnum
@@ ssmallestnum而B!= 1
如果不排序[A]< =无序[B],然后
@@ smallestnum =无序[B]
否则@@ smallestnu =无序[一]
结束
@@ A = @@ A +
@@ AA = @@一个+ 1
B = - 1
结束
请帮我code。
另:我得到这些错误消息当我运行它:
(EVAL):465(EVAL):465:编译错误(SyntaxError错误)
(EVAL):465:语法错误,意想不到的$结束,预计kEND
在code是应该的数字从低到高排序。
如果不排序[0]<< ==无序[1],然后numsmall =无序[一]
^
(EVAL):51:语法错误,意想不到的kTHEN,期待kEND
这一点 ^
点,第一个问题在这里。 << ==
不是红宝石合法的运营商,因此语法错误。也许你的意思是小于等于,这是< =
如果不排序[A]< =无序[B]
此外,缩进将帮助您了解流量较好,请尝试重写是这样的:
如果不排序[A]< =无序[B]
numsmall ==无序[一]
其他
numsmall =未排序并[b]
结束
Basically I'm trying to create a BASIC sorting program in ruby without using .sort . Here's my code so far:
def optsort
@@a = 0
@@aa= 1
@@b = unsorted.size
@@smallestnum
@@ssmallestnum
while b !=1
if unsorted[a] <= unsorted[b] then
@@smallestnum = unsorted[b]
else @@smallestnu = unsorted[a]
end
@@a = @@a +
@@aa = @@a + 1
b = b - 1
end
Please help me with the code. ALSO: I get those error messages when I run it:
(eval):465: (eval):465: compile error (SyntaxError)
(eval):465: syntax error, unexpected $end, expecting kEND
The code is supposed to sort the numbers from least to greatest.
if unsorted[0] <<== unsorted[1] then numsmall = unsorted[a]
^
(eval):51: syntax error, unexpected kTHEN, expecting kEND
That little ^
points to first problem here. <<==
is not a legal operator in ruby, hence the syntax error. Perhaps you mean "less than or equal to" which is <=
?
if unsorted[a] <= unsorted[b]
Also, indentation will help you understand the flow better, try rewriting that like this:
if unsorted[a] <= unsorted[b]
numsmall == unsorted[a]
else
numsmall = unsorted[b]
end
这篇关于在红宝石排序程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!