[ create a new paste ] login | about

Link: http://codepad.org/8BsoiWQo    [ raw code | output | fork ]

Ruby, pasted on Jan 24:
def bubblesort(array)
      i = 0 
      swapped = true
      length = array.length
      while i < length-1 and swapped do # also check for end of array
          swapped = false
          n = i +1  # second loop variable 'n' should be initialized here
          while n < length
              if array[i] > array[n]
                  dummy = array[i]
                  array[i]= array[n]
                  array[n] = dummy  
                  swapped = true
              end
             n+=1 # 'n' should be incremented here...
          end
          i += 1 # and 'i' here
      end
      return array
end

ret = bubblesort([10,3,4,6,1,4,2])
x = 0
while x < ret.length
  print ret[x], ' '
  x+=1
end


Output:
1
1 2 3 4 4 6 10 


Create a new paste based on this one


Comments: