Sorting an Array in Ruby without using Sort method

后端 未结 7 1995
感动是毒
感动是毒 2020-12-20 02:20

I\'m trying to use the bubble sort method to sort an array of only three numbers. The code I\'m using is below.

def my_sort(list)
  return list if list.         


        
7条回答
  •  情话喂你
    2020-12-20 02:32

    def sort(arr)
        arr_len = arr.length - 1
        swap = true
        while swap
            swap = false
            arr_len.times do |i|
             if arr[i] > arr[i+1]
               arr[i],arr[i + 1] = arr[i + 1],arr[i]
               swap = true
             end
            end
         end
         p arr
    
    end
    

提交回复
热议问题