Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby数组五_排序算法_Ruby_Algorithm - Fatal编程技术网

Ruby数组五_排序算法

Ruby数组五_排序算法,ruby,algorithm,Ruby,Algorithm,我试图解决一个名为five_sort的问题,它接受一个整数数组作为参数,并将所有的五放在数组的末尾,而将所有其他数字都不排序。例如,[1,2,5,3,2,5,5,7]将被排序为[1,2,3,2,7,5,5,5]。问题规则规定只能使用while循环,并且除了[]和[]=之外,不能对数组调用其他方法。这是我目前的代码: def five_sort(array) sorted = false while sorted == false idx = 0 while

我试图解决一个名为five_sort的问题,它接受一个整数数组作为参数,并将所有的五放在数组的末尾,而将所有其他数字都不排序。例如,
[1,2,5,3,2,5,5,7]
将被排序为
[1,2,3,2,7,5,5,5]
。问题规则规定只能使用while循环,并且除了
[]
[]=
之外,不能对数组调用其他方法。这是我目前的代码:

def five_sort(array)
    sorted = false
    while sorted == false
    idx = 0

      while idx < array.length
        if array[idx] == 5
            array[idx], array[idx + 1] = array[idx + 1], array[idx]
        end
        idx += 1
      end
    sorted = true
    end
array
end
def五次排序(数组)
排序=假
排序时==false
idx=0
而idx
当运行它时,它只是在一个连续的循环中,但我不知道如何修复它。我知道如果我只运行第二个while循环,而不运行
while sorted
循环,那么数组将只运行一次,五个数组将只切换一次位置,循环将结束。但我不知道如何运行第二个while循环,并在所有五个循环结束后停止它


有人能帮我解决这个问题吗?

您的阵列在每个循环中都变得越来越长:

array = [1,2]
array[1], array[2] = array[2], array[1]
puts array.length
产出3

如果
idx=array.length-1

if (array[idx] == 5)
  array[idx], array[idx+1] = array[idx+1], array[idx] if idx != array.length - 1
end

虽然有几个人发布了替代方法,这些方法都很好,但我想发布一些基于您自己的代码的东西,以向您保证您已经非常接近解决方案

我添加了一些注释来解释我所做的更改:

def five_sort(array)
  sorted = false
  while sorted == false
    idx = 0
    # use did_swap to keep track of if we've needed to swap any numbers
    did_swap = false

    # check if next element is nil as alternative to using Array#length
    while array[idx + 1] != nil
      # it's only really a swap if the other entry is not also a 5
      if array[idx] == 5 and array[idx + 1] != 5
        array[idx], array[idx + 1] = array[idx + 1], array[idx]
        did_swap = true
      end
      idx += 1
    end

    # if we've been through the array without needing to make any swaps
    # then the list is sorted
    if !did_swap
      sorted = true
    end
  end
  array
end
注:

  • 根据规范的要求,在
    arr
    上调用的唯一方法是
    []
    []=
    ,并且不创建其他数组
  • 如果
    i
    索引数组的最后一个元素,
    arr[i+1]
    等于
    nil
  • arr[i..i]=[]
    arr
    中删除元素
    arr[i]
  • arr[-1,2]=[arr[-1,5]
    arr
    中追加一个
    5
    • 只是一个简单的O(n)时间和O(1)空间解决方案,使用写索引和读索引

        w = r = 0
        while array[w]
          r += 1 while array[r] == 5
          array[w] = array[r] || 5
          w += 1
          r += 1
        end
      

      不确定您是想要一个解决方案,还是仅仅是一个提示来为您指明正确的方向?作为提示,当您通过列表并且没有更多的交换要进行时,列表将正确排序。因此,在内部
      while
      循环中,尝试跟踪您是否进行了任何交换,并使用该循环决定是否将
      sorted
      设置为true。我不知道。算法看起来基本正确吗?这是我需要的一个小改动,还是我的算法根本错误?如果只是一个小变动,我可以用什么来跟踪掉期?就像一个
      swap=0
      计数器然后递增?它非常接近,只需要一些小的更改。在第一次循环第二个循环后,您总是将
      排序
      设置为true。所以循环只运行一次。您已经正确地计算出希望第二个循环重复,直到列表的末尾有所有的5。提示是,当您完全通过第二个循环而不需要任何交换时,列表将被排序。请注意,如果
      array[idx]
      是5,而
      array[idx+1]
      也是5,那么实际上并不是在交换任何东西。另外,当idx位于阵列的末尾时,请注意不要访问阵列[idx+1]。这有帮助吗?这些规则不禁止你使用
      数组.length
      ?Good spot@StefanPochmann-我还没有学过这个。
      length
      数组的一种方法,既不是
      []
      也不是
      []=
      。我喜欢这个解决方案,我会这么做的,但是我忘了提到这个问题也禁止创建新的数组。我修改了我的答案,以避免使用额外的数组。(只有在看到您的建议
      &&arr[I+1]
      后才阅读此问题)。谢谢!我太习惯于使用
      length
      方法,以至于我忽略了思考如何在数组末尾停止。因此,无法在阵列上使用任何方法严重限制了我解决问题的能力。这是一个简单的问题,没有对解决方案的限制。但我知道问题的关键是让我思考逻辑,而不仅仅是操纵对象。当然,RW-INDEX!非常优雅,谢谢。当我看到这样的代码时,我总是想知道我一生中到底在用什么来代替大脑不错,斯蒂芬!
        w = r = 0
        while array[w]
          r += 1 while array[r] == 5
          array[w] = array[r] || 5
          w += 1
          r += 1
        end