使用冒泡排序方法用于Ruby的阵列[闭合](Using the Bubble sort method

2019-07-30 00:28发布

我试图实现冒泡排序方法为Ruby的一个简单的编码问题,但我遇到了一些麻烦。 我理解的想法是看的第一个元素的值,并把它比作第二个元素的值,然后相应地交换他们,但我似乎无法做到这一点在实际的问题。 会有人愿意提供这如何可能在Ruby中工作的一个简单的例子?

Answer 1:

正确执行冒泡排序的while循环

def bubble_sort(list)
  return list if list.size <= 1 # already sorted
  swapped = true
  while swapped do
    swapped = false
    0.upto(list.size-2) do |i|
      if list[i] > list[i+1]
        list[i], list[i+1] = list[i+1], list[i] # swap values
        swapped = true
      end
    end    
  end

  list
end


Answer 2:

arr = [4,2,5,1]
loop until arr.each_cons(2).with_index.none?{|(x,y),i| arr[i],arr[i+1] = y,x if x > y}
p arr #=> [1, 2, 4, 5]


Answer 3:

资源

def bubble_sort(list)
  return list if list.size <= 1 # already sorted

  loop do
    swapped = false
    0.upto(list.size-2) do |i|
      if list[i] > list[i+1]
        list[i], list[i+1] = list[i+1], list[i] # swap values
        swapped = true
      end
    end
    break unless swapped
  end

  list
end

虽然我一定会推荐一些具有更好的运行时间比冒泡:)



Answer 4:

这里是我顶答案的版本。 它调用数组大小​​只有一次,而不是每一个循环。 一旦他们转移到阵列结束它不比较的元素。

并从while循环退出一个循环越快。 大功告成,一旦你通过整个阵列消失了,只有做一个交换,因此无需与0互换另做。

def bubble_sort(list)
  iterations = list.size - 2

  return list unless iterations > 0 # already sorted

  swaps = 2

  while swaps > 1 do
    swaps = 0

    0.upto(iterations) do |i|
      if list[i] > list[i + 1]
        list[i], list[i + 1] = list[i + 1], list[i] # swap values
        swaps += 1
      end
    end

    iterations -= 1
  end

  list
end

运行这个测试需要减少25%的时间。

that_array = this_array = [22,66,4,44,5,7,392,22,8,77,33,118,99,6,1,62,29,14,139,2]
49.times {|variable| that_array = that_array + this_array}
bubble_sort that_array


Answer 5:

只是重新写@ VanDarg的代码使用while循环(注:代码没有测试......在你自己的危险跑)

def bubble_sort(list)
  return list if list.size <= 1 # already sorted

  swapped = true
  while swapped
    swapped = false # maybe this time, we won't find a swap
    0.upto(list.size-2) do |i|
      if list[i] > list[i+1]
        list[i], list[i+1] = list[i+1], list[i] # swap values
        swapped = true # found a swap... keep going
      end
    end
  end

  list
end

编辑:因为冒泡排序排序保持,同时还有掉期正在取得更新换值-只要它发现没有更多的互换,将停止排序。 请注意,这并不关注@ Doug的代码,但它与@ cLuv的修正符合



Answer 6:

def bubble_sort array 
  array.each do
    swap_count = 0
    array.each_with_index do |a, index|
      break if index == (array.length - 1)
      if a > array[index+1]
        array[index],array[index+1] = array[index +1], array[index]
        swap_count += 1
      end
    end
    break if swap_count == 0 # this means it's ordered
  end
  array
end


Answer 7:

该直截了当:

def bubble_sort(n)
  return n if n.length <= 1

  0.upto(n.length - 1) do |t|
    0.upto(n.length - 2 - t) do |i|
      if n[i] > n[i + 1]
        n[i], n[i + 1] = n[i + 1], n[i]
      end
    end
  end

  n
end


Answer 8:

如果你不想使用这个有趣的交换线(IMO):

    arr[i], arr[j] = arr[j], arr[i]

这里是我的看法:

def bubble_sort(arr)
  temp = 0

  arr.each do |i|
    i = 0
    j = 1
    while (j < arr.length)
      if arr[i] > arr[j]
        temp = arr[i] 
        arr[i] = arr[j]
        arr[j] = temp 
        p arr
      end
    i+=1
    j+=1
    end
  end  
 arr
end


Answer 9:

老套

def bubble_sort(random_numbers)
for i in 0..random_numbers.size 
  for j in i+1..random_numbers.size-1    
    random_numbers[i], random_numbers[j] = random_numbers[j], random_numbers[i] if(random_numbers[i] > random_numbers[j])
  end
end
random_numbers
end


Answer 10:

class Array
a = [6, 5, 4, 3, 2, 1]
n = a.length

for j in 0..n-1
    for i in 0..n - 2 - j
           if a[i]>a[i+1]
                tmp = a[i]
                a[i] = a[i+1]
                a[i+1] = tmp
          end
     end
end

puts a.inspect
end


Answer 11:

下面是使用运营商XOR我的见解:

def bubble(arr)
     n = arr.size - 1
     k = 1
     loop do
          swapped = false
          0.upto(n-k) do |i|
              if arr[i] > arr[i+1]
                 xor = arr[i]^arr[i+1]
                 arr[i] = xor^arr[i]
                 arr[i+1] = xor^arr[i+1]
                 swapped = true  
              end 
          end 
          break unless swapped
          k +=1 
     end
   return arr
end  


Answer 12:

另外,稍有不同的命名。

def bubble_sort(list)
  return list if list.size <= 1
  not_sorted = true

  while not_sorted
    not_sorted = false

    0.upto(list.size - 2) do |i|
      if list[i] > list[i + 1]
        list[i], list[i + 1] = list[i + 1], list[i]
        not_sorted = true
      end
    end
  end
  list
end


Answer 13:

 def bubbleSort(list)
  sorted = false
  until sorted
    sorted = true
    for i in 0..(list.length - 2)
      if list[i] > list[i + 1]
        sorted = false
        list[i], list[i + 1] = list[i + 1], list[i]
      end
    end
  end
  return list
end


Answer 14:

这里是我的代码。 我喜欢用(arr.length-1)。 对于循环,您还可以使用这样的重复,如直到,而对于,高达,循环做,等有趣的尝试不同的事情,看看它是如何发挥作用。

def bubble_sort(arr)  #10/17/13 took me 8mins to write it

return arr if arr.length <= 1

sorted = true

    while sorted
     sorted = false
     (arr.length-1).times do |i|
        if arr[i] > arr[i+1]
          arr[i], arr[i+1] = arr[i+1], arr[i]
          sorted = true
        end
       end
     end
    arr
    end


文章来源: Using the Bubble sort method for an array in Ruby [closed]