Problem:
Bubble Sort Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode: BubbleSort(A)
1 for i = 0 to A.length-1
2     for j = A.length-1 downto i+1
3         if A[j] < A[j-1]
4             swap A[j] and A[j-1] Note that, indices for array elements are based on 0-origin. Your program should also print the number of swap operations defined in line 4 of the pseudocode. Input The first line of the input includes an integer N , the number of elements in the sequence. In the second line, N elements of the sequence are given separated by spaces characters. Output The output consists of 2 lines. In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character. In the second line, please print the number of swap operations. Constraints 1 ≤ N ≤ 100 Sample Input 1 5
5 3 2 4 1 Sample Output 1 1 2 3 4 5
8 Sample Input 2 6
5 2 4 6 1 3 Sample Output 2 1 2 3 4 5 6
9

Solution (Python):
def bubblesort(N, A):
    """Do BubbleSort"""
    m = 0
    flag = True
    while(flag):
        flag = False
        for i in range(N-1, 0, -1):
            if (A[i-1] > A[i]):
                tmp = A[i-1]
                A[i-1] = A[i]
                m += 1
                A[i] = tmp
                flag = True
    b = list(map(str, A))
    print(" ".join(b))
    print(m)

A = []
N = int(input())
s = input()
A = list(map(int, s.split()))
bubblesort(N, A)

Solution (Python):
n = int(input())
m = list(map(int, input().split()))

count = 0
flag = 1
while flag:
    flag = 0
    for j in range(n-1, 0, -1):
        if m[j] < m[j-1]:
            m[j], m[j-1] = m[j-1], m[j]
            count += 1
            flag = 1

print(" ".join(str(x) for x in m))
print(count)

Solution (Python):

def BS(N,A):
    count=0
    flag=True
    while flag==True:
        flag=False
        for i in range(1,N):
            if A[N-i]<A[N-i-1]:
                swap=A[N-i]
                A[N-i]=A[N-i-1]
                A[N-i-1]=swap
                flag=True
                count+=1
    for i in range(len(A)):
        A[i]=str(A[i])
    print(" ".join(A))
    print(count)

if __name__=="__main__":
    N=int(input())
    a=input()
    A=a.split(" ")
    for i in range(len(A)):
        A[i]=int(A[i])
    BS(N,A)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

counter = 0
flag = 1  
while flag:
    flag = 0
    for j in range(N-1, 0, -1):
        if A[j] < A[j-1]:
            v = A[j]
            A[j] = A[j-1]
            A[j-1] = v
            flag = 1
            counter += 1

print(' '.join([str(A[i]) for i in range(0, N)]))
print(counter)

Solution (Python):
N = int(raw_input())
nums = map(int, raw_input().split(" "))

count = 0
for i in range(0, len(nums)):
    for j in range(len(nums)-1, i, -1):
        if nums[j-1] > nums[j]:
            temp = nums[j-1]
            nums[j-1] = nums[j]
            nums[j] = temp
            count += 1

nums = map(str, nums)
print(" ".join(nums))
print(count)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
f = 1 
c = 0

while f == 1:
    f = 0
    for j in range(n-1,0,-1):
        if A[j] < A[j-1]:
            inc = A[j]
            A[j] = A[j-1] 
            A[j-1] = inc
            f = 1
            c += 1
print(" ".join(map(str,A)))
print(c)

Solution (Python):




class Solution:
    """
    @param prices: Given an integer array
    @return: Maximum profit
    """

    @staticmethod
    def bubble_sort():
        
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

        flag = 1
        count = 0
        while flag:
            flag = 0
            for j in range(array_length - 1, 0, -1):
                if unsorted_array[j] < unsorted_array[j - 1]:
                    unsorted_array[j], unsorted_array[j - 1] = unsorted_array[j - 1], unsorted_array[j]
                    flag = 1
                    count += 1
        print(" ".join(map(str, unsorted_array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Solution (Python):




class Solution:

    @staticmethod
    def bubble_sort():
        
        array_length = int(input())
        array = [int(x) for x in input().split()]

        flag, count, cursor = 1, 0, 0
        while flag:
            flag = 0
            for j in range(array_length - 1, cursor, -1):
                if array[j] < array[j - 1]:
                    array[j], array[j - 1] = array[j - 1], array[j]
                    flag = 1
                    count += 1
        cursor += 1
        print(" ".join(map(str, array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Solution (Python):



N = int(raw_input())

A = map(int,raw_input().split())
counter = 0 
for i in range(len(A)):
    for j in range(len(A) - 1, 0, -1):
        if A[j] < A[j-1]:
            A[j],A[j-1] = A[j-1], A[j]
            counter += 1

print ' '.join(map(str,A))
print counter

Solution (Python):
n = int(input())
L = list(map(int, input().split()))
p = 0
for _ in L:
    for j in range(n-1, 0, -1):
        if L[j] < L[j-1]:
            L[j], L[j-1] = L[j-1], L[j]
            p += 1
print(*L)
print(p)

Solution (Python):
N = input()
A = map(int, raw_input().split())
ans = 0
flag = True
while flag:
    flag = False
    for j in xrange(N-1, 0, -1):
        if A[j] < A[j-1]:
            ans += 1
            A[j], A[j-1] = A[j-1], A[j]
            flag = True
print ' '.join(map(str, A))
print ans

Solution (Python):
n = input()
lst = map(int, raw_input().split())
flag = True
cnt=0
while flag:
    flag = False
    for j in range(n-1,0,-1):
        if lst[j] < lst[j-1]:
            tmp=lst[j-1]
            lst[j-1] = lst[j]
            lst[j] = tmp
            flag = True
            cnt+=1

print " ".join(map(str, (lst)))
print cnt

Solution (Python):





def bubble_sort(target_list):
    list_length = len(target_list)
    
    flag = True
    change_count = 0
    top_index = 1
    while flag:
        flag = False
        for i in range(top_index, list_length)[::-1]:
            if target_list[i] < target_list[i - 1]:
                tmp = target_list[i]
                target_list[i] = target_list[i - 1]
                target_list[i - 1] = tmp
                change_count += 1
                flag = True
        top_index += 1
        
    return change_count

def main():
    n_list = int(input())
    target_list = [int(n) for n in input().split()]
    cc = bubble_sort(target_list)
    print(*target_list)
    print(cc)
if __name__ == "__main__":
    main()

Solution (Python):

def print_array(A, N):
    for i in xrange(N - 1):
        print str(A[i]),

    print A[N - 1]


def bubbleSort(A, N):
    swap_num = 0
    flag = 1
    while flag == 1:
        flag = 0
        for j in xrange(N - 1, 0, -1):
            if A[j] < A[j - 1]:
                swap_num += 1

                temp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = temp
                flag = 1

    return swap_num


def main():
    N = int(raw_input())
    A = []

    for num in raw_input().split():
        A.append(int(num))

    temp = bubbleSort(A, N)
    print_array(A, N)

    print temp


if __name__ == "__main__":
    main()

Solution (Python):
def bubble_sort(numbers, n):
    """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)

    Returns:
        sorted list
    """
    flag = True
    counter = 0
    while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if numbers[index] < numbers[index - 1]:
                numbers[index], numbers[index - 1] = numbers[index - 1], numbers[index]
                flag = True
                counter += 1
    return numbers, counter


n = int(raw_input())
numbers = [int(x) for x in raw_input().split()]
numbers, swapped_numbers = bubble_sort(numbers, n)

print(" ".join([str(x) for x in numbers]))
print(swapped_numbers)

Solution (Python):
N=input()
A=map(int,raw_input().split())
num=0
flag=1
while flag:
	flag=0
	for j in reversed(range(N-1)):
		if A[j+1]<A[j]:
			A[j+1],A[j]=A[j],A[j+1]
			flag=1
			num+=1

for m in range(N-1):
	print str(A[m]),
print A[N-1]
print num

Solution (Python):
def bubble_sort(data, n):
    """ ??°???data??????????????????????????????????????§???????????????????????????n?????°????????????
    ?????????????????????????????°??¨????????????????????????????????????
    """
    swap_count = 0
    flag = True
    i = 0
    while flag is True:
        flag = False
        for j in range(n-1, i, -1):
            if data[j] < data[j-1]:
                temp = data[j]
                data[j] = data[j-1]
                data[j-1] = temp
                swap_count += 1
                flag = True
        i == 1
    return swap_count, data


if __name__ == '__main__':
    
    num = int(input())
    data = [int(x) for x in input().split(' ')]
    

    
    count, data = bubble_sort(data, num)

    
    print('{0}\n{1}'.format(' '.join(map(str, data)), count))

Solution (Python):



''' ?????????????????? '''




def bubble_sort(A, N):
    swap_num = 0  

    for i in range(N - 1):
        for j in range(N - 1, i, -1):
            if A[j] < A[j - 1]:
                
                temp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = temp
                swap_num += 1

    return (A, swap_num)


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    
    
    array_sorted, swap_num = bubble_sort(A, N)
    print(' '.join(map(str, array_sorted)))
    print(swap_num)

Solution (Python):



def bubble_sort(n, a):
    cnt = 0
    flag = True
    while flag:
        flag = False
        for i in reversed(range(n)):
            if i is 0: break
            if a[i] < a[i - 1]:
                tmp = a[i]
                a[i] = a[i - 1]
                a[i - 1] = tmp
                flag = True
                cnt += 1
    return a, cnt


if __name__ == '__main__':
    n = int(input())
    a = [int(n) for n in input().split()]
    ans = bubble_sort(n, a)
    print(' '.join(map(str, ans[0])))
    print(ans[1])

Solution (Python):
def buble_sort(seq):
    l = len(seq)
    cnt = 0
    for i in range(l):
        for j in range(i+1, l)[::-1]:
            if seq[j] < seq[j-1]:
                seq[j], seq[j-1] = seq[j-1], seq[j]
                cnt += 1
    return seq, cnt


n = int(input())
a = list(map(int, input().split()))
sorted_a, num_swap = buble_sort(a)
print(' '.join(map(str, sorted_a)))
print(num_swap)

Solution (Python):
N = int(input())
A = list(map(int, input().split(" ")))

num_of_swp = 0
for i in range(N):
  for j in range(N - 1, i, -1):
    if A[j] < A[j - 1]:
      A[j - 1], A[j] = A[j], A[j - 1]
      num_of_swp += 1

print(" ".join(map(str, A)))
print(num_of_swp)

Solution (Python):


def bubble_sort(num_list):
    swap_count = 0
    for i in range(len(num_list)):
        for j in range(len(num_list)-1, i , -1):
            if num_list[j] < num_list[j-1]:
                num_list[j], num_list[j-1] = num_list[j-1], num_list[j]
                swap_count = swap_count + 1
    return num_list, swap_count

def show_list(list):
    i = 0;
    while(i < len(list)-1):
        print(list[i], end=" ")
        i = i + 1
    print(list[i])

num = int(input())
num_list = [int(x) for i, x in enumerate(input().split()) if i < num]
num_list, swap_count = bubble_sort(num_list)
show_list(num_list)
print(swap_count)

Solution (Python):

num_length = int(input())
num_array = [int(x) for x in input().split()]

def bubbleSort(A, N):
    flag = 1
    exchange_counter = 0
    while flag:
        flag = 0
        for i in range(1, N):
            if A[i] < A[i-1]:
                A[i], A[i-1] = A[i-1], A[i]
                flag = 1
                exchange_counter += 1
    print(" ".join(map(str, A)))
    print(exchange_counter)

bubbleSort(num_array, num_length)

Solution (Python):
def bubble_sort(array):
	N = len(array)
	count = 0
	
	for i in range(N - 1):
		flag = 0
		
		for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		if flag == 0:
			break
	
	return array, count
	
n = int(input())
array = [int(i) for i in input().split()]
result = bubble_sort(array)
print(*result[0])
print(result[1])

Solution (Python):
_, vs, c = input(), list(map(int, input().split())), 0
for i in range(len(vs)):
    for j in range(len(vs)-1, i, -1):
        if vs[j] < vs[j-1]:
            vs[j], vs[j-1] = vs[j-1], vs[j]
            c += 1
print(' '.join(map(str, vs)))
print(c)

Solution (Python):
def bubble_sort(r, n):
    flag = True  
    count = 0
    while flag:
        flag = False
        for i in range(n-1, 0, -1):
            if r[i-1] > r[i]:
                r[i-1],r[i] = r[i],r[i-1]
                count += 1
                flag = True

    print(*r)
    print(count)

N = int(input())
A = list(map(int, input().split()))
bubble_sort(A,N)

Solution (Python):

def BubbleSort(A, N):
    cnt = 0 
    flag = 1 

    while flag:
        flag = 0

        for j in range(N - 1, 0, -1):
            if A[j] < A[j - 1]:
                A[j] , A[j - 1] = A[j - 1], A[j]
                cnt += 1
                flag = 1

    
    return cnt


N = int(input())
A = [int(a) for a in input().split()]

cnt = BubbleSort(A, N)

print(*A)
print(cnt)

Solution (Python):
def bubbleSort(A, N):
    swap_count = 0
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                flag = True
                swap_count += 1
    return swap_count

n = int(input())
A = list(map(int, input().split()))
count = bubbleSort(A,n)
print(*A)
print(count)

Solution (Python):

def bubbleSort(A,N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in range(N-1):
            if A[j] > A[j+1]:
                tmp = A[j]
                A[j] = A[j+1]
                A[j+1] = tmp
                flag = 1
                count += 1
    return A,count
N = int(input())
A = input()
A = A.split()
for i in range(len(A)):
    A[i] = int(A[i])
    
A,count = bubbleSort(A,N)
for i in range(len(A)-1):
    print(A[i],end = ' ')
print(A[len(A)-1])
print(count)

Solution (Python):
def bubbleSort(A, N):
    swap_count = 0
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                flag = True
                swap_count += 1
    return swap_count

n = int(input())
A = list(map(int, input().split()))
count = bubbleSort(A,n)
print(*A)
print(count)

Solution (Python):
def bubble_sort(target):
    global cnt
    flag = True
    while flag:
        flag = False
        for i in range(len(target)-1, 0, -1):
            if target[i] < target[i-1]:
                target[i-1], target[i] = target[i], target[i-1]
                flag = True
                cnt += 1
    return target

N = int(input())
A = [int(s) for s in input().split()]
cnt = 0
print(' '.join(str(s) for s in bubble_sort(A)))
print(cnt)

Solution (Python):



def bubble_sort(seq, l):
    cnt = 0

    for max_size in range(l, 0, -1):
        for left in range(1, max_size):
            if seq[left-1] > seq[left]:
                seq[left-1], seq[left] = seq[left], seq[left-1]
                cnt += 1

    print(' '.join([str(i) for i in seq]))
    print(cnt)


def to_int(v):
    return int(v)


def to_seq(v):
    return [int(c) for c in v.split()]


if __name__ == '__main__':
    l = to_int(input())
    seq = to_seq(input())

    bubble_sort(seq, l)

Solution (Python):
def bubble_sort(A):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, 0, -1):
            if A[i - 1] > A[i]:
                A[i - 1], A[i] = A[i], A[i - 1]
                flag = 1
                count += 1
    return count


def show(A):
    for i in range(len(A) - 1):
        print(A[i], end=" ")
    print(A[len(A) - 1])


n = int(input())
line = input()
A = list(map(int, line.split()))
count = bubble_sort(A)
show(A)
print(count)

Solution (Python):
def main():
    N = int(input())
    A = list(map(int,input().split()))
    flag = True
    count = 0
    while flag:
        flag = False
        for i in range(N-1,0,-1):
            if A[i] < A[i-1]:
                count += 1
                A[i], A[i-1] = A[i-1], A[i]
                flag = True
    print(" ".join(map(str,A)))
    print(count)


if __name__ == "__main__":
    import os
    import sys
    if len(sys.argv) > 1:
        if sys.argv[1] == "-d":
            fd = os.open("input.txt", os.O_RDONLY)
            os.dup2(fd, sys.stdin.fileno())
            main()
    else:
        main()

Solution (Python):
cnt = 0
n = int(input())
lst = list(map(int, input().split()))
for i in range(n):
    for j in range(n-1,i,-1):
        if lst[j] < lst[j-1]:
            lst[j-1:j+1] = lst[j], lst[j-1]
            cnt += 1
print(*lst)
print(cnt)

Solution (Python):
N = int(raw_input())
A = map(int, raw_input().split())

def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in range(N-1, 0, -1):
            if A[i] < A[i-1]:
                temp = A[i]
                A[i] = A[i-1]
                A[i-1] = temp
                flag = 1
                count += 1
    return count

count = bubbleSort(A, N)

print " ".join(map(str, A))
print count

Solution (Python):
n = int(input())
num_list = input().split()
 
def bubbleSort(num_list, n):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in range(n-1, 0, -1):
            if int(num_list[j]) < int(num_list[j-1]):
                num_list[j], num_list[j-1] = num_list[j-1], num_list[j]
                flag = 1
                count += 1
 
    result = ' '.join(num_list)
    print(result)
    print(count)
 
bubbleSort(num_list, n)

Solution (Python):
n = int(input())

list = [int(i) for i in input().split()]

def bubble(list):
    flag = 1
    count = 0
    while flag == 1:
        flag = 0
        for i in range(len(list)-1):
            if list[len(list)-1-i] < list[len(list)-1-i-1]:
                x = list[len(list)-i-1]
                list[len(list)-1-i] = list[len(list)-1-i-1]
                list[len(list)-1-i-1] = x
                flag = 1
                count += 1
    return [list,count]

answer = bubble(list)
for i in range(len(answer[0])):
    print(str(answer[0][i]) + ' ',end='') if i != len(answer[0])-1 else print(answer[0][i])
print(answer[1])

Solution (Python):
n = int(input())

list = [int(i) for i in input().split()]

def bubble(list):
    flag = 1
    count = 0
    while flag == 1:
        flag = 0
        for i in range(len(list)-1):
            if list[len(list)-1-i] < list[len(list)-1-i-1]:
                x = list[len(list)-i-1]
                list[len(list)-1-i] = list[len(list)-1-i-1]
                list[len(list)-1-i-1] = x
                flag = 1
                count += 1
    return [list,count]

answer = bubble(list)
for i in range(len(answer[0])):
    print(str(answer[0][i]) + ' ',end='') if i != len(answer[0])-1 else print(answer[0][i])
print(answer[1])

Solution (Python):
def bubbleSort():
    N = int(input())
    lst = list(map(int, input().split()))
    
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
    print(*lst)
    print(count)

bubbleSort()

Solution (Python):
def bubbleSort(A, N):
    swap = 0
    flag = True
    while flag:
        flag = False
        for j in range(N-1,0,-1):
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1],A[j]
                swap += 1
                flag = True
    return swap
N = int(input())
A = list(map(int,input().split()))
swap=bubbleSort(A,N)
print(" ".join(map(str,A)))
print (swap)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=0
for _ in a:
    for i in range(n-1,0,-1):
        if a[i]<a[i-1]:
            a[i],a[i-1]=a[i-1],a[i]
            b+=1
print(*a)
print(b)

Solution (Python):
def bubblesort(a):
    count = 0
    swap = True
    while swap:
        swap = False
        for j in range(1,len(a))[::-1]:
            if a[j] < a[j-1]:
                ret = a[j]
                a[j] = a[j-1]
                a[j-1] = ret
                count += 1
                swap = True           
    print(" ".join(map(str,a)))
    print(count)
    
n = int(input())
a = list(map(int,input().split()))
bubblesort(a)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

flag = 1
counter = 0
while flag:
	flag = 0
	for i in reversed(range(1,N)):
		if A[i] < A[i-1]:
			A[i], A[i-1] = A[i-1], A[i]
			counter += 1
			flag = 1
print(" ".join(map(str, A)))
print(counter)

Solution (Python):
N = int(input())

A = [int(n) for n in input().split()]

flag = 1
num_c  = 0
while(flag):
    flag = 0
    for j in range(1, N):
        if A[j] < A[j-1]:
            v = A[j]
            A[j] = A[j-1]
            A[j-1] = v
            num_c += 1
            flag = 1

print((" ").join((str(a) for a in A)))
print(num_c)

Solution (Python):
def bubbleSort(n,A):
    flag = True
    cnt = 0
    while flag:
        flag = False
        for j in range(n-1, 0, -1):
            if A[j-1] > A[j]:
                A[j-1],A[j] = A[j],A[j-1]
                cnt += 1
                flag = True
    print(*A)
    print(cnt)

if __name__ == '__main__':
    N = int(input())
    A = list(map(int,input().split()))
    bubbleSort(N, A)

Solution (Python):
def bubbleSort(A,N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in range(1,N):
            if A[N-i] < A[N-i-1]:
                A[N-i] , A[N-i-1] = A[N-i-1] , A[N-i]
                flag = 1
                count += 1
    return (count)

n = int(input())
data = [int(i) for i in input().split()]
a = bubbleSort(data,n)
print(" ".join(map(str,data)))
print(a)

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
frag = 1
i = 0
count = 0
while frag:
    frag = 0
    for j in range(N-1,i,-1):
        if a[j] < a[j-1]:
            a[j], a[j-1] = a[j-1], a[j]
            count += 1
        frag = 1
        
    i += 1

print(" ".join(map(str, a)))
print(count)

Solution (Python):
data_num = int(input())
nums = list(map(int, input().split(" ")))
cnt = 0
for i in range(data_num):
    for j in range(data_num-1, i,-1):
        if nums[j]<nums[j-1]:
            nums[j], nums[j-1] = nums[j-1], nums[j]
            cnt = cnt + 1

print(" ".join(map(str,nums)) + "\n" + str(cnt))

Solution (Python):
n = int(input())
sequence = [int(x) for x in input().split(' ')]

def pretty_sequence(sequence):
    n = len(sequence)
    for i in range(n-1):
        print(format(sequence[i]) + ' ', end='')
    print(sequence[n - 1])

def bubleSort(n, sequence):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if sequence[i] < sequence[i-1]:
                tmp_val = sequence[i]
                sequence[i] = sequence[i-1]
                sequence[i-1] = tmp_val
                count += 1
                flag = 1
    return sequence, count

sequence, count = bubleSort(n, sequence)
pretty_sequence(sequence)
print(count)

Solution (Python):
n = int(input())
A = [int(i) for i in input().split(' ')]



def trace(A):
    for index, v in enumerate(A):
        print(v, end='')
        if index != len(A) - 1:
            print(' ', end='')
    print()

def bubble_sort(A, n):
    i = 0
    counter = 0
    while i <= n - 1:
        j = n - 1
        while j >= i + 1:
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                counter += 1
            j -= 1
        i += 1
    trace(A)
    print(counter)

bubble_sort(A, n)

Solution (Python):
n = int(input())
R = [int(x) for x in input().split()]
time = 0
flag = True
while flag:
     flag = False
     for i in reversed(range(n-1)):
        if R[i+1] < R[i]:
            R[i], R[i+1] = R[i+1], R[i]
            flag = True
            time += 1 
print(*R)
print(time)

Solution (Python):


S = int(input())
N = list(map(int,input().split()))
count=0
def bubblesort(n,s):
    global count
    flag = 1
    while flag:
        flag = 0
        for i in range(s-1,0,-1):
            key = n[i]
            if n[i]<n[i-1]:
                n[i]=n[i-1]
                n[i-1]=key
                count+=1
                flag=1
    print(" ".join(list(map(str,n))))
    print(count)
        
bubblesort(N,S)

Solution (Python):
def bubbleSort():
    l = int(input())
    A = list(map(int, input().split(" ")))
    count = 0
    for i in range(l-1):
        for j in range(l-1-i):
            if (A[j]>A[j+1]):
                temp = A[j]
                A[j] = A[j+1]
                A[j+1] = temp
                count += 1
    print(" ".join(map(str, A)))
    print(count)

bubbleSort()

Solution (Python):
N = int(input())
A = [int(x) for x in input().split(" ")]
flag = True
swap_count = 0
i = 0
while flag:
    flag = False
    for j in range(N-1, i, -1):
        if A[j] < A[j-1]:
            A[j], A[j-1] = A[j-1], A[j]
            flag = True
            swap_count += 1
    i += 1

print(*A)
print(swap_count)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split(" ")]
cnt = 0
flag = True
while flag:
    flag = False
    for i in range(n-1, 0, -1):
        if a[i-1] > a[i]:
            a[i-1], a[i] = a[i], a[i-1]
            flag = True
            cnt += 1
print(" ".join([str(i) for i in a]))
print(cnt)

Solution (Python):
N   = int(input())
A   = [int(temp) for temp in input().split()]
cou = 0

for mak in range(1,N) :
    for j in range(mak,N)[ : : -1] :
        if A[j] < A[j - 1] :
            tem      = int(A[j])
            A[j]     = int(A[j - 1])
            A[j - 1] = int(tem)
            cou = cou + 1
A   = [str(temp) for temp in A]
print(' '.join(A))
print(cou)

Solution (Python):


import sys
import os
import math


N = int(input())
A = list(map(int, input().split()))


def bubble_sort(A):
    swap_num = 0
    while True:
        swapped = False
        for i in range(N - 1):
            if A[i] > A[i + 1]:
                A[i], A[i + 1] = A[i + 1], A[i]
                swap_num += 1
                swapped = True
        if not swapped:
            return A, swap_num

A, num = bubble_sort(A)
print(*A)
print(num)

Solution (Python):


def bubbleSort(A, N):
    swap_count = 0

    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                swap_count += 1
                flag = True
    ans = " ".join(map(str, A))
    print(ans)
    print(swap_count)

def main():
    size = int(input())
    line = list(map(int, input().split()))
    bubbleSort(line, size)

if __name__ == '__main__':
    main()

Solution (Python):
N = input()
A = map(int,raw_input().split())

flag = 1
x = 0
while flag:
    flag = 0
    for i in range(1,N):
        j = N - i
        if A[j] < A[j-1]:
            v = A[j]
            A[j] = A[j-1]
            A[j-1] = v
            x += 1
            flag = 1

for i in range(N-1):
    print A[i],
print A[-1]

print x

Solution (Python):

def print_list(ele_list):
    print(" ".join(map(str, ele_list)))

def bubble_sort(ele_list):
    len_ele_list = len(ele_list)
    num_swap = 0
    for i in range(0, len_ele_list):
       for j in range(len_ele_list-1, i, -1):
           if ele_list[j] < ele_list[j-1]:
               key = ele_list[j]
               ele_list[j] = ele_list[j-1]
               ele_list[j-1] = key
               num_swap += 1
    print_list(ele_list)
    print(num_swap)

N = int(input())
ele_list = list(map(int, input().split()))

bubble_sort(ele_list)

Solution (Python):
def bubble_sort(A):
    count = 0
    for i in reversed(range(len(A))):
        for j in range(i):
            if A[j] > A[j+1]:
                temp = A[j]
                A[j] = A[j+1]
                A[j+1] = temp
                count += 1
    return count

N = int(input())
A = list(map(int,input().split()))

count = bubble_sort(A)
print(" ".join(map(str,A)))
print(count)

Solution (Python):


N = int(input())
a = list(map(int, input().split()))

flag = 1
swapcount = 0
while flag == 1:
    flag = 0
    for i in range(0, N-1):
        if a[i] > a[i + 1]:
            tmp = a[i];
            a[i] = a[i + 1]
            a[i + 1] = tmp
            flag = 1
            swapcount += 1

print(' '.join(map(str, a)))
print(swapcount)

Solution (Python):
def bubble_sort(array):
    count = 0
    n = len(array)
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

    print(" ".join(map(str,array)))
    print(count)

times = int(input())
array = list(map(int,input().split()))

bubble_sort(array)

Solution (Python):


def bubblesort(ls):
    count = 0
    
    for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j] < ls[j-1]:
                ls[j], ls[j-1] = ls[j-1], ls[j]
                count += 1
    return count

def main():
    trash = input()
    ls = list(map(int, input().split(' ')))

    i = bubblesort(ls)

    print(' '.join((map(str,ls))))
    print(i)
    
if __name__ == '__main__':
    main()

Solution (Python):


def bubblesort(ls):
    count = 0
    
    for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j] < ls[j-1]:
                ls[j], ls[j-1] = ls[j-1], ls[j]
                count += 1
    return count

def main():
    trash = input()
    ls = list(map(int, input().split(' ')))

    i = bubblesort(ls)

    print(' '.join((map(str,ls))))
    print(i)
    
if __name__ == '__main__':
    main()

Solution (Python):
def buble_sort(a,n):
    flag = True
    count = 0
    while flag:
        flag = False
        for j in range(n-1,0,-1):
            if a[j] < a[j-1]:
                a[j],a[j-1] = a[j-1],a[j]
                count += 1
                flag = True
    return (a,count)

n = int(input())
a = list(map(int, input().split()))
a,c = buble_sort(a,n)
print(' '.join(map(str, a)))
print(c)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))


def bubble_sort(A, N):
    cnt = 0
    flag = True
    while(flag):
        flag = False
        for i in range(N-1, 0, -1):
            if(A[i-1] > A[i]):
                A[i-1], A[i] = A[i], A[i-1]
                cnt += 1
                flag = True

    print(" ".join(list(map(str, A))))
    print(cnt)

bubble_sort(A, N)

Solution (Python):
_n = int(raw_input())
_a = [int(x) for x in raw_input().split()]

_flg = 1
cnt = 0

while _flg:
    _flg = 0
    for i in reversed(range(1, _n)):
        if _a[i] < _a[i-1]:
            _a[i], _a[i-1] = _a[i-1], _a[i]
            cnt += 1
            _flg = 1

print(" ".join([str(x) for x in _a]))
print(cnt)

Solution (Python):
def bubble_sort(A, n):
	flag = 1
	i = 0
	count = 0

	while flag:
		flag = 0
		for j in range(n-1, i, -1):
			if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		i += 1
	
	print(' '.join(map(str, A)))
	print(count)


if __name__ == '__main__':
	n = int(input())
	A = list(map(int, input().split()))

	bubble_sort(A, n)