尝试用 ruby 编写排序程序 - 堆栈级别太深(系统堆栈错误)
我正在读 Chris Pine 的书“Learn to Progam”(这是关于 Ruby 的)。现在我正在尝试编写一个对单词进行排序的程序。不幸的是,我陷入了:第 16 行中的堆栈级别太深(系统堆栈错误),如果我在 Google 上正确搜索,则意味着存在无限循环,但我不知道为什么。
这是代码:
words = []
wordss = []
word = 'word'
i = 0
k = 0
def sortw array
i = 0
if (array.length == 1) || (array.length == 0)
else sort array, [], [], i
end
return array
end
def sort unsorted, unsort, sorted, i
k = 0 # The error should be here, according to command prompt
while i < unsorted.length
while (unsorted[i] < unsorted[k])
if k < unsorted.length
k = k + 1
elsif k == unsorted.length
sorted.push unsorted[i]
else unsort.push unsorted[i]
end
end
i = i + 1
sort unsorted, unsort, sorted, i
end
if unsort.length != 1
i = 0
sort unsort, [], sorted, i
else sorted.push unsort[0]
end
return sorted
end
puts 'type one word per line...'
puts 'typing enter on an empty line sorts the inputted words'
while word != ''
word = gets.chomp
words = words.push word
end
wordss = (sortw words)
puts 'your words'
puts words
puts 'sorted here'
puts wordss
I'm reading Chris Pine's book "Learn to Progam" (it's about Ruby). Right now I'm trying to write a program that sorts words. Unfortunately I'm stuck with: stack level too deep (system stack error) in line 16
, which, if i Googled correctly means that there is an infinite loop, but I don't know why.
Here's the code:
words = []
wordss = []
word = 'word'
i = 0
k = 0
def sortw array
i = 0
if (array.length == 1) || (array.length == 0)
else sort array, [], [], i
end
return array
end
def sort unsorted, unsort, sorted, i
k = 0 # The error should be here, according to command prompt
while i < unsorted.length
while (unsorted[i] < unsorted[k])
if k < unsorted.length
k = k + 1
elsif k == unsorted.length
sorted.push unsorted[i]
else unsort.push unsorted[i]
end
end
i = i + 1
sort unsorted, unsort, sorted, i
end
if unsort.length != 1
i = 0
sort unsort, [], sorted, i
else sorted.push unsort[0]
end
return sorted
end
puts 'type one word per line...'
puts 'typing enter on an empty line sorts the inputted words'
while word != ''
word = gets.chomp
words = words.push word
end
wordss = (sortw words)
puts 'your words'
puts words
puts 'sorted here'
puts wordss
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到错误是因为递归由于排序算法的问题而没有停止。在
sort
方法中,k
始终小于unsorted.length
。这会导致其他数组、sorted
和unsort
永远不会填充。例如,尝试输入以下内容:
另外,我认为您不想包含空行,因此我会将代码从:
words = Words.push word
更改为words = Words .push word if word != ''
这将创建
未排序
数组:下面编号的是递归
排序的迭代方法。
因此,以下代码会导致无限循环,因为没有任何内容被推送到
unsort
数组中:You are getting the error because recursion does not stop due to a problem with the sorting algorithm. In the
sort
method,k
is always less thanunsorted.length
. This causes the other arrays,sorted
andunsort
to never populate.For example try these for input:
Additionally, I think you want to not include the blank line so I would change the code from:
words = words.push word
towords = words.push word if word != ''
This creates the
unsorted
array:Numbered below are the iterations of the recursive
sort
method.i
is now equal to theunsorted
array length, the two while loops never get entered anymore.Therefore, the following code results in an infinite loop since nothing was pushed to the
unsort
array: