Python

def bubbleSort(l):
    l = l[:]
    size = len(l)

    for i in range(size - 1):
        for j in range(size - 1 - i):
            if l[j] > l[j + 1]:
                l[j], l[j + 1] = l[j + 1], l[j]

    return l

Ruby

def bubble_sort array
  array = Array.new array
  0.upto(array.size - 2).each do |i|
    0.upto(array.size - 2 - i).each do |j|
      array[j], array[j + 1] = array[j + 1], array[j] if array[j] > array[j + 1]
    end
  end

  return array
end

C

void bubbleSort(int array[], int size) {
    int i, j, t = 0;
    for(i = 0; i < size - 1; i++) {
        for(j = 0; j < size - 1 - i; j++) {
            if(array[j] > array[j + 1]) {
                t = array[j];
                array[j] = array[j + 1];
                array[j + 1] = t;
            }
        }
    }
}
Comments
Write a Comment