尝试用 ruby​​ 编写排序程序 - 堆栈级别太深(系统堆栈错误)

发布于 2024-10-20 17:33:48 字数 1143 浏览 7 评论 0原文

我正在读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-10-27 17:33:48

您收到错误是因为递归由于排序算法的问题而没有停止。在 sort 方法中,k 始终小于 unsorted.length。这会导致其他数组、sortedunsort 永远不会填充。

例如,尝试输入以下内容:

  • dog
  • zebra
  • cat

另外,我认为您不想包含空行,因此我会将代码从:

words = Words.push word 更改为 words = Words .push word if word != ''

这将创建未排序数组:

  • [0] dog
  • [1] zebra
  • [2] cat

下面编号的是递归排序的迭代方法。

#initial variable state:
i = 0
k = 1
  1. 狗=狗
    • 跳过第二个 while 循环
    • i = 1
  2. zebra >狗
    • 跳过第二个 while 循环
    • i = 2
  3. cat <狗
    • 进入第二个while循环
      • k = 1,现在 cat <斑马,所以继续循环
      • k = 2,现在 cat = cat,因此退出
    • i = 3
  4. 由于 i 现在等于未排序的数组长度,所以两个 while 循环永远不会不再进入。

因此,以下代码会导致无限循环,因为没有任何内容被推送到 unsort 数组中:

if unsort.length != 1
  i = 0
  sort unsort, [], sorted, i #Problem is that `unsort` and `sorted` are empty
elsif
...
end

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 than unsorted.length. This causes the other arrays, sorted and unsort to never populate.

For example try these for input:

  • dog
  • zebra
  • cat

Additionally, I think you want to not include the blank line so I would change the code from:

words = words.push word to words = words.push word if word != ''

This creates the unsorted array:

  • [0] dog
  • [1] zebra
  • [2] cat

Numbered below are the iterations of the recursive sort method.

#initial variable state:
i = 0
k = 1
  1. dog = dog
    • skip second while loop
    • i = 1
  2. zebra > dog
    • skip second while loop
    • i = 2
  3. cat < dog
    • enter second while loop
      • k = 1, now cat < zebra, so keep looping
      • k = 2, now cat = cat, so exit while
    • i = 3
  4. Since i is now equal to the unsorted 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:

if unsort.length != 1
  i = 0
  sort unsort, [], sorted, i #Problem is that `unsort` and `sorted` are empty
elsif
...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文