【问题标题】:Array Alphabetical Sorting Without .sort没有 .sort 的数组按字母顺序排序
【发布时间】:2015-03-17 06:20:59
【问题描述】:

我试图在不使用 .sort 方法的情况下将多个字符串排序到一个按字母顺序排列的数组中。我试过 while 循环和 if 语句,但它不起作用。

在将 user_input 与数组进行比较时,我在第 13 行不断收到 nil 错误。我不知道如何解决这个问题,我什至不明白为什么会这样。

alpha_sorter = [' ']
user_input = ' '
sort_counter = 0

puts 'Type in as many words as you want.'
puts 'One word per line.'
puts 'When you are done, leave the line blank and press enter.'

while (user_input != '')
  user_input = gets.chomp.downcase
  if (user_input <= alpha_sorter[sort_counter])
    alpha_sorter.insert(sort_counter, user_input)
    sort_counter = 0
  else
    sort_counter += 1
  end
end

puts alpha_sorter

我的程序使用排序:

alpha_sorter = []
user_input = ' '


puts 'Type in as many words as you want.'
puts 'One word per line.'
puts 'When you are done, leave the line blank and press enter.'

while (user_input != '')
  user_input = gets.chomp
  alpha_sorter.push user_input
end

puts alpha_sorter.sort

【问题讨论】:

  • 你为什么不想使用排序?
  • 这是一个挑战,在不使用排序的情况下做到这一点。
  • user_input &gt; alpha_sorter[sort_counter]你去下一个循环迭代,增加sort_counter,这显然是错误的。
  • 您的解决方案的问题在于您实际上并没有找到放置新输入的正确位置。每次通过获取输入的循环时,您只检查一个位置——无论它是在它之前还是之后。无论您决定什么,您都不会进一步检查。但是如果项目不在这个位置之后,而是在更远的三个位置之后呢?你的“弄清楚项目应该去哪里”部分不应该是一个单一的 if 语句,它也应该是一个循环......继续寻找它去哪里直到你发现它实际去哪里)。这就是人们建议其他排序算法的原因。

标签: ruby arrays sorting


【解决方案1】:

对不起,如果我让你更困惑了,我只是想帮助另一个新手。lol

这是我的解决方案,有很多注释要解释:

puts
puts "Enter some words and I will returned them to you in alphabetical order!"
puts
input = gets.chomp
input = input.split


def swap(array)  
  i =0
  while i < array.length #here is one iteration over the array

    i2 = i + 1
    while i2 < array.length #here is a second. this allows me to compare the second index (and the rest as i2 increments) to the current one
      if array[i] < array[i2]  #if the second is larger than the first
        array[i]  , array[i2] = array[i2], array[i] #switch them. this is called parallel assignment
      end # if
      i2 += 1  #increment the second loop before the first. it will keep incrementing until the end before the first one does. then the whole thing starts again until the first loop has reached the full length of the array
    end # inner loop
    i += 1
 end # outer loop
  puts
  puts '...And just like magic ...here is your string in alphabetical order!! :'
  puts
  return array.reverse.join(' ')
end # def

puts swap(input)

我在网上找到的另一个漂亮的解决方案(我很生气,我没想到)是:

def shuffle_sort(array)
sorted = []
  while !array.empty? #do this until the array is empty
    sorted << array.delete(array.min) #push the word with the min value into sorted then delete it. min is a enum method
  end                             
  return sorted.join(' ')
end

puts shuffle_sort(alpha)

现在有多美??! #min 方法返回具有最小值的对象(在本例中为单词)!! :)

【讨论】:

  • 当您谈到我的这部分代码时:(user_input &lt;= alpha_sorter[sort_counter]) 我正在将字符串与数组的特定部分进行比较。基本上,array[n] 其中 n 是一个数字。是否不可能这样做,因为我正在将字符串与数组内的字符串进行比较?此外,(user_input &lt;= alpha_sorter[sort_counter]),此代码将用户输入的内容与数组内部的内容进行比较。所以,'cat' &lt;= 'dog' 将是 true。因为它是真的,'cat' 会被插入。
  • 最后,alpha_sorter.insert(sort_counter, user_input),这里我将user_input推到数组中sort_counter的位置。 insert 方法中的第一个参数是后面的参数将被放置在数组中的位置。参考这里:link
  • 好吧,您不能将字符串与数组进行比较,因为它们是完全不同的对象。但是,您可以将 array1 的索引与 array2 的索引进行比较: user_input[n] alpha_sorter << user_input[sort_counter]
  • 我还想指出,在你的 while 循环中:while (user_input != '') 这不是最好的方法,因为 user_input = gets.chomp 和 gets.chomp 总是返回一个字符串。即使用户输入了一个数字,比如 45 - 返回的是字符串“45”,因此该条件将始终返回 true,并且不会阻止用户输入数字。
  • 哇,我从来不知道。即使array1的索引是字符串,也不能和字符串比较?哇。但是,是的,我在这方面真的很陌生。我两天前才开始学习 Ruby。
【解决方案2】:

Ruby 中的快速排序非常简单:

def sort(arr)
  return [] unless arr
  pivot = arr[0]
  grouped = arr.group_by { |e| e <=> pivot }
  sort(grouped[-1]) + grouped[0] + sort(grouped[1])
end

这是一种幼稚的方法,但应该可行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    相关资源
    最近更新 更多