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):

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

def BubbleSort(A):
    count = 0
    for i in range(len(A)):
        for j in reversed(range(i+1,len(A))):
            if A[j]<A[j-1]:
                
                a = A[j]
                b = A[j-1]
                A[j-1] = a
                A[j] = b
                count+=1
    return [A,count]

result = BubbleSort(li)    

print(" ".join(list(map(str,result[0]))))
print(result[1])

Solution (Python):
numberOfArray=int(input())
arrayList=list(map(int,input().split()))

def bubbleSort():
    frag=1
    count=0
    while frag:
        frag=0
        for i in range(1,numberOfArray):
            if arrayList[i-1]>arrayList[i]:
                arrayList[i-1],arrayList[i]=arrayList[i],arrayList[i-1]
                frag=1
                count+=1
    arrayForReturn=str(arrayList[0])
    for i2 in range(1,numberOfArray):
        arrayForReturn+=" "+str(arrayList[i2])
    print(arrayForReturn)
    print(count)


bubbleSort()

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

cnt = 0
for i in range(n) :
    for j in range(n - 1, i, -1) :
        if s[j - 1] > s[j] :
            s[j - 1], s[j] = s[j], s[j - 1]
            cnt += 1     
print(" ".join(map(str, s)))
print(cnt)

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

count = 0
flag = 1
while flag:
    flag = 0
    for i in range(len(ls)-1, 0, -1):
        if ls[i] < ls[i-1]:
            ls[i], ls[i-1] = ls[i-1], ls[i]
            count += 1
            flag = 1
print(' '.join(map(str, ls)))
print(count)

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


n = int(input())
a = list(map(int, input().split()))
bubbleSort(n, a)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c=0
f=True
while f:
    f=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]
            c+=1
            f=True
print(*a)
print(c)

Solution (Python):

n = int(input())
line = [int(i) for i in input().rstrip().split()]


count = 0
for i in range(n - 1):
    a = n - 1
    while a > i:
        if line[a - 1] > line[a]:
            line[a - 1], line[a] = line[a], line[a - 1]
            
            count += 1
        a -= 1

for i in range(n):
    print(line[i], end = "")
    if i < n - 1:
        print(" ", end = "")
print()

print(count)

Solution (Python):
def print_list(A):
    print(*A, sep=" ")

def swap(a, b):
    return b, a

def bubble_sort(A, n):
    flg = 1 
    i = 0
    replace_num = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 
        i += 1
    print_list(A)
    print(replace_num)

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

bubble_sort(A, n)

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

num = 0

is_swapped = True
while is_swapped:
    is_swapped = False
    for i in range(n-1, 0, -1):
        if a[i] < a[i-1]:
            tmp = a[i]
            a[i] = a[i-1]
            a[i-1] = tmp
            is_swapped = True
            num += 1

print(' '.join([str(i) for i in a]))
print(num)

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

num = 0

is_swapped = True
i = 0
while is_swapped:
    is_swapped = False
    for j in range(n-1, i, -1):
        if a[j] < a[j-1]:
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp
            is_swapped = True
            num += 1
    i += 1

print(' '.join([str(i) for i in a]))
print(num)

Solution (Python):
def bubble_sort(a, n):
    sw_num = 0
    is_swapped = True
    i = 0
    while is_swapped:
        is_swapped = False
        for j in range(n-1, i, -1):
            if a[j] < a[j-1]:
                tmp = a[j]
                a[j] = a[j-1]
                a[j-1] = tmp
                is_swapped = True
                sw_num += 1
        i += 1

    return sw_num


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

sw_num = bubble_sort(a, n)

print(' '.join([str(i) for i in a]))
print(sw_num)

Solution (Python):
def bubbleSort(a,n):
  flag = 1
  count = 0
  while flag:
    flag = 0
    for j in range(n-1,0,-1):
      if a[j] < a[j-1]:
        count += 1
        w = a[j]
        a[j] = a[j-1]
        a[j-1] = w
        flag = 1
  print(' '.join(map(str, a)))
  print(count)

n = int(input())
a = list(map(int, input().split()))
bubbleSort(a,n)

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

Solution (Python):





def formatting(nums):
    for i in range(len(nums)):
        if i != len(nums) - 1:
            print(nums[i], end=' ')
        else:
            print(nums[i])


def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0

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




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

bubbleSort(inputList, N)

Solution (Python):





def formatting(nums):
    for i in range(len(nums)):
        if i != len(nums) - 1:
            print(nums[i], end=' ')
        else:
            print(nums[i])


def bubbleSort(A, N):
    count = 0
    i = 0
    flag = 1
    while flag:
        flag = 0

        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
                count += 1
                flag = 1
        i += 1
    formatting(A)
    print(count)




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

bubbleSort(inputList, N)

Solution (Python):
bit=[0 for i in xrange(200)]
n=int(raw_input().strip())
class dig:
    def __init__(self,val,id):
        self.val=val
        self.id=id
    def __gt__(self,other):
        return self.val<other.val


def sum(i):
    s=0
    i=int(i)
    while i>0:
        s+=bit[i]
        i-=i&(-i)
    return s
def add(i,x):
    i=int(i)
    while i<=n:
        bit[i]+=x
        i+=i&-i


def sort2(lst):
    lens=len(lst)
    sm=0
    for i in xrange(lens):
        for j in xrange(i+1,lens):
            if lst[i]>lst[j]:
                sm+=1
    return sm
lst=map(int,raw_input().strip().split())
vals=[]
lens=len(lst)
for i in xrange(lens):
    vals.append(dig(lst[i],i+1))
vals.sort()
ans=0
for i in vals:
    
    ans+=sum(i.id)
    add(i.id,1)
ans=sort2(lst)
lst.sort()
print " ".join(str(pp) for pp in lst)
print ans

Solution (Python):
def bubble_sort(A, N):
    cnt = 0
    flag = True
    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]
                flag = True
                cnt += 1
    return cnt

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

Solution (Python):
def bubble_sort(num_list):
    list_length = len(num_list)-1
    cnt = 0

    for i in range(list_length):
        for j in range(list_length,i,-1):
            if num_list[j] < num_list[j-1]:
                num_list[j],num_list[j-1] = num_list[j-1],num_list[j]
                cnt += 1
    return num_list,cnt

n = int(input())
num_list = list(map(int,input().split()))
ans_list,cnt = bubble_sort(num_list)
str_ans_list = [str(i) for i in ans_list]
print(' '.join(str_ans_list))
print(cnt)

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



def bubbleSort(arr):
    arr_len = len(arr)
    flg = True
    cnt = 0
    while flg:
        flg = False
        for i in range(arr_len -1, 0, -1):
            if arr[i] < arr[i-1]:
                arr[i], arr[i-1] = arr[i-1], arr[i]
                flg = True
                cnt = cnt + 1
                
    print(' '.join([str(x) for x in arr]))
    print(cnt)

bubbleSort(A)

Solution (Python):
def main():
    n = int(input())
    array = [int(x) for x in input().split()]

    cnt = 0
    while True:
        flag = False
        for i in range(1, n)[::-1]:
            if array[i-1] > array[i]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
                cnt += 1
        if not flag:
            break
    print(" ".join(map(str, array)))
    print(cnt)


main()

Solution (Python):
def showList(A, N):
    for i in range(N-1):
        print(A[i],end=' ')
    print(A[N-1])

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

N = int(input())
A = [int(x) for x in input().split(' ')]
bubbleSort(A, N)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
nums = [n - i for i in range(1, n)]
flag = 1
exchange = 0

while flag == 1:
    flag = 0
    for j in nums:
        if A[j] < A[j - 1]:
            A[j], A[j - 1] = A[j - 1], A[j]
            exchange += 1
            flag = 1
        else:
            pass
        
print(*A)
print(exchange)

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

def bubbleSort(A):
    n = len(A)
    flag = True
    i = 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]
                i += 1
                flag = True
    print(*A)
    print(i)
bubbleSort(A)

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

swap_count = 0

for i in range(0,len(table)-1): 
    for k in range(len(table)-1,i,-1):
        if table[k-1] > table[k]:
            table[k-1],table[k] = table[k],table[k-1]
            swap_count += 1

print(' '.join(map(str,table)))
print("%d"%(swap_count))

Solution (Python):
N = input()
N = int(N)
array = list(map(int,  input().split()))
cnt = 0
for i in range(N):
    
    for j in range(N-1, i, -1):
        if array[j] < array[j-1]:
            array[j], array[j-1] = array[j-1], array[j]
            cnt += 1
print(' '.join(map(str, array)))
print("%d" % (cnt))

Solution (Python):
N = input()
N = int(N)
array = list(map(int,  input().split()))
cnt = 0
for i in range(N):

    for j in range(N-1, i, -1):
        if array[j] < array[j-1]:
            array[j], array[j-1] = array[j-1], array[j]
            cnt += 1
print(' '.join(map(str, array)))
print("%d" % (cnt))

Solution (Python):
def show(nums):
    for i in range(len(nums)):
        if i != len(nums) - 1:
            print(nums[i],end=' ')
        else:
            print(nums[i])

def bubbleSort(A,N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in range(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
                count += 1
    show(A)
    print(count)

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


bubbleSort(A,N)

Solution (Python):
n = int(input())
s = list(map(int, input().strip().split(' ')))
e = int()
e = 0
flag = True
while flag:
        flag = False
        for i in range(n - 1, 0, -1):
            if s[i] < s[i - 1]:
                s[i - 1], s[i] = s[i], s[i - 1]
                e = e + 1
                flag = True

for i in range(0 , len(s)):
    if i != len(s) - 1:
        print(s[i], end=" ")
    else:
        print(s[i])

print(e)

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

cnt = 0
flag = 1
while flag > 0:
    flag = 0
    for j in range(n-1,0,-1):
        if a[j] < a[j-1]:
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp
            flag = 1
            cnt += 1
print(' '.join(map(str,a)))
print(cnt)

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

flag = True
swap_count = 0
for i in range(0, n):
    if not flag:
        break
    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

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

Solution (Python):
from typing import List


def bublle_sort(A: List[int]) -> List[int]:
    cnt = 0
    flg = True
    while flg:
        flg = False
        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 = True
    return A, cnt


N = int(input())
A = [int(i) for i in input().split()]
A, cnt = bublle_sort(A)
print(' '.join(map(str, A)))
print(cnt)

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

def bubble_sort(A, N):
    count = 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]
                count += 1
                flag = 1
    return A, count


A, count = bubble_sort(A, N)
string = list(map(str, A))
print(' '.join(string))
print(count)

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

count = 0
flag = True
while flag:
    flag = False
    for i in range(n - 1):
        if a[i] > a[i + 1]:
            a[i + 1], a[i] = a[i], a[i + 1]
            count += 1
            flag = True

print(" ".join([str(i) for i in a]))
print(count)

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

count = 0
for i in range(n):
    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

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

Solution (Python):
def bubblesort(n,l):
  flag=True
  m=0
  c=0
  for j in range(n):
    for i in range(n-1,m,-1):
      if int(l[i])<int(l[i-1]):
        l[i],l[i-1]=l[i-1],l[i]
        
        c+=1
    m+=1
  print(" ".join(l))
  print(c)
n=int(input())
l=list(input().split())
bubblesort(n,l)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))[::-1]
flag=1
count=0
while flag:
    flag=0
    for i in range(N-1):
        if A[i]<A[i+1]:
            A[i],A[i+1]=A[i+1],A[i]
            flag=1
            count+=1

print(*A[::-1])
print(count)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
flag = True
ans = 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]
            flag = True
            ans += 1
print(*A)
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
flag = True
ans = 0
for i in range(N):
    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
            ans += 1
    if not flag:
        break
print(*A)
print(ans)

Solution (Python):




def bubbleSort(A, N):
    sw = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in range(int(N) - 1, i, -1):
            if A[j - 1] > A[j]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = 1
                sw = sw + 1
        i = i + 1
    return A, sw

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

    A_sorted, num_sw = bubbleSort(A, N)

    for i in range(N-1):
        print(A[i], end=" ")
    print(A[N-1])
    print(num_sw)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
count = 0
flag = True
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]
			count += 1
			flag = True
print(*a)
print(count)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
count = 0

for i in range(n-1):
    for j in range(n-1, i, -1):
        if a[j-1] > a[j]:
            a[j-1], a[j] = a[j], a[j-1]
            count += 1

print(*a)
print(count)

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

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


def bubble_sort(a, n):
    flag = True
    count = 0
    while flag:
        flag = False
        for i in range(n-2, -1, -1):
            if a[i] > a[i+1]:
                a[i], a[i+1] = a[i+1], a[i]
                flag = True
                count += 1
    print(" ".join(map(str, a)))
    print(count)


bubble_sort(ll, n)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=0
flg=True
i=0
while flg:
    flg=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]
            flg=True
            ans+=1
    i+=1
print(' '.join(map(str,a)))
print(ans)

Solution (Python):
n = int(input())
nums = list(map(int, input().split()))
count = 0


while True:
    flag = 0

    for j in range(n - 1):
        if (nums[j + 1] < nums[j]):
            temp = nums[j]
            nums[j] = nums[j + 1]
            nums[j + 1] = temp
            count += 1
            flag = 1

    if flag != 1:
        break

print(' '.join([str(i) for i in nums]))
print(count)

Solution (Python):

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0

    flag = True
    while flag:
        flag = False
        for i in range(1, n):
            if a[i] < a[i-1]:
                a[i], a[i-1] = a[i-1], a[i]
                flag = True
                ans += 1
    print(*a)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def bubble_sort(A):
    flag = True
    i = 0
    global count
    while flag:
        flag = False
        for j in range(i, len(A)-1):
           if A[j+1] < A[j]:
                A[j], A[j+1] = A[j+1], A[j]
                count += 1
                flag = True
        if not flag:
            i += 1
            flag = True
        if i >= len(A)-1:
            flag = False

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

Solution (Python):
def bubble_sort(A):
    can_continue = True
    i = 0
    global count
    while can_continue:
        can_continue = False
        for j in range(i, len(A)-1):
           if A[j+1] < A[j]:
                A[j], A[j+1] = A[j+1], A[j]
                count += 1
                can_continue = True
        if not can_continue:
            i += 1
            can_continue = True
        if i >= len(A)-1:
            break

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

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

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

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

count = 0

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

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

Solution (Python):
def bubbleSort(A,N):
    flag = 1
    count = 0
    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]
                flag = 1
                count += 1
    return A,count

N = int(input())
A = list(map(int, input().split()))
A,count = bubbleSort(A,N)
print(" ".join(map(str,A)))
print("%d"%(count))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
flag = True
c = 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]
            flag = True
            c += 1
print(*a)
print(c)

Solution (Python):

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

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

print(' '.join(map(str,nums)))
print(count)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sw = 0
for i in range(N-1):
    for j in range(N-1,i,-1):
        if A[j] < A[j-1]:
            A[j], A[j-1] = A[j-1], A[j]
            sw += 1
print(" ".join(map(str, A)))
print(sw)

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

def sort():
    flag = 1
    swap = 0
    while flag:
        flag = 0
        for j in range(N-1,0,-1):
            if A[j] < A [j-1]:
                temp = A[j]
                A[j] = A[j-1]
                A[j-1] = temp
                swap += 1
                flag = 1
    pr(A)
    print(swap)

def pr(list):
    n = len(list)
    for i in range(n):
        if i < n-1:
            print(list[i],end=" ")
        else:
            print(list[i],end="\n")

sort()

Solution (Python):
n = input()
a = map(int, raw_input().split())

c = 0
for i in range(n):
    for j in range(n - 1, i, -1):
        if a[j] < a[j - 1]:
            a[j], a[j - 1] = a[j - 1], a[j]
            c += 1

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

Solution (Python):
N = input()
n = map(int,raw_input().split(' '))
c = 0
for i in range(0,N-1):
	for j in range(1,N-i):
		if n[j-1] > n[j]:
			temp = n[j-1]
			n[j-1] = n[j]
			n[j] = temp
			c += 1
print " ".join(str(i) for i in n)
print c

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

Solution (Python):
import sys

n = int(raw_input())
A = map(int, sys.stdin.readline().split())

cnt = 0

for i in xrange(len(A)-1):
    for j in xrange(len(A)-1, i, -1):
        if A[j] < A[j-1]:
            A[j], A[j-1] = A[j-1], A[j]
            cnt += 1

for x in A:
	print x,
print 
print cnt

Solution (Python):

def bubble_sort(buff):
	count = 0
	k = len(buff) - 1
	for i in xrange(k):
		for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	print " ".join(map(str, buff))
	print count

N = int(raw_input())
buff = map(int, raw_input().split(' '))
bubble_sort(buff)

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

for i in range(N):
    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

print(*A)
print(count)

Solution (Python):
def bubblesort(A):
    tot = 0
    flag = 1
    N = len(A)
    
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                tot += 1
    return A,tot

while True:
    try:
        N = int(raw_input())
    except EOFError:
        break
    A = map(int,raw_input().split())
    
    A,tot = bubblesort(A)
    print ' '.join(map(str,A))
    print tot

Solution (Python):
def bubble_sort(A):
    cnt = 0
    for i in range(len(A) - 1):
        for j in reversed(range(i+1, len(A))):
            if A[j] < A[j - 1]:
                cnt += 1
                tmp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = tmp
    return cnt

cnt = 0
amount = int(input())
nl = list(map(int, input().split()))

cnt = bubble_sort(nl)

for i in range(amount - 1):
    print(nl[i], end=" ")
print(nl[amount - 1])
print(cnt)

Solution (Python):


import sys

nums_len = int(sys.stdin.readline())
nums_str = sys.stdin.readline().split(' ')
nums = map(int, nums_str)

swap_count = 0
flag = True
while flag:
    flag = False
    for i in reversed(range(1, nums_len)):
        if nums[i] < nums[i - 1]:
            v = nums[i]
            nums[i] = nums[i - 1]
            nums[i - 1] = v
            swap_count += 1
            flag = True

print ' '.join(map(str, nums))
print swap_count

Solution (Python):
def bubbleSort( nums ):
	cnt = 0
	for i in range( 0, len( nums ) ):
		for j in range( len( nums )-1 , i, -1 ):
			if nums[j] < nums[ j-1 ]:
				nums[j], nums[ j-1 ] = nums[ j-1 ], nums[j]
				cnt += 1

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


n = int( raw_input( ) )
nums = [ int( val ) for val in raw_input( ).split( " " ) ]
bubbleSort( nums )

Solution (Python):
def bubbleSort( nums ):
	cnt = 0
	for i in range( 0, len( nums ) ):
		for j in range( len( nums )-1 , i, -1 ):
			if nums[j] < nums[ j-1 ]:
				nums[j], nums[ j-1 ] = nums[ j-1 ], nums[j]
				cnt += 1

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


n = int( input( ) )
nums = [ int( val ) for val in input( ).split( " " ) ]
bubbleSort( nums )

Solution (Python):


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

c = 0
flag = True
while flag:
    flag = False
    for i in range(N - 1, 0, -1):
        if A[i] < A[i - 1]:
            A[i], A[i - 1] = A[i - 1], A[i]
            c += 1
            flag = True
print(' '.join((str(x) for x in A)))
print(c)

Solution (Python):

n = int(input())
numbers = list(map(int, input().split()))
count = 0

for i in range(n-1):
    for j in range(n-1, i, -1):
        if numbers[j] < numbers[j-1]:
            numbers[j], numbers[j-1] = numbers[j-1], numbers[j]
            count += 1

print(*numbers)
print(count)

Solution (Python):
def bubbleSort(list_, n):
    list_ = list_[:]
    count = 0
    for i in xrange(n):
        for j in xrange(n-1, i, -1):
            if list_[j] < list_[j - 1]:
                list_[j], list_[j - 1] = list_[j - 1], list_[j]
                count += 1

    return list_, count

n = input()
nums = map(int, raw_input().split())
sorted_, count = bubbleSort(nums, n)
print ' '.join(map(str, sorted_))
print count

Solution (Python):
def bubbleSort(list_, n):
    count = 0
    for i in xrange(n):
        for j in xrange(n-1, i, -1):
            if list_[j] < list_[j - 1]:
                list_[j], list_[j - 1] = list_[j - 1], list_[j]
                count += 1

    return count

n = input()
nums = map(int, raw_input().split())
count = bubbleSort(nums, n)
print ' '.join(map(str, nums))
print count

Solution (Python):
def bubble_sort(A):
    count = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1, len(A))):
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                count += 1
    return A, count

if __name__ == '__main__':
    n = int(input())
    array, count = bubble_sort(list(map(int, input().split())))
    print(' '.join(map(str, array)))
    print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
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]
            count += 1
            flag = True

print(*a)
print(count)

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

Solution (Python):
def bubble(A,N):
	flag=1
	cnt=0
	while flag==1:
		flag=0
		for j in range(1,N):
			if A[j]<A[j-1]:
				k=A[j-1]
				A[j-1]=A[j]
				A[j]=k
				flag=1
				cnt+=1
	return A,cnt
n=int(raw_input())			
list=map(int,raw_input().split())
list,cnt=bubble(list,n)
for x in list:
	print x,
print
print cnt,

Solution (Python):


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

    print(' '.join(map(str, ary)))
    print(count)

n = int(input())
numbers = list(map(int, input().split()))
bubble_sort(numbers, n)

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

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

count=0
i=N-1

while i>0:
	j=0
	while j<i:
		if A[N-1-j]<A[N-2-j]:
			(A[N-1-j],A[N-2-j])=(A[N-2-j],A[N-1-j])
			count+=1
			
		j+=1
	i-=1


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

Solution (Python):
def bubble_sort(a,n):
    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()))
bubble_sort(a,n)

Solution (Python):
n = int(input())
s = input()
A = list(map(int,s.split()))
flag = 1
cnt = 0

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

for k in range(0,len(A)-1):
    print(A[k],end=" ")
print(A[len(A)-1])
print (cnt)

Solution (Python):
N = int(raw_input())
A = map(int, raw_input().split())
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]
            A[j] = A[j-1]
            A[j-1] = A_j
            flag = True
            count += 1
print " ".join(map(str, A))
print count