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(A,N):
    c=0
    flag=1
    while flag:
        flag=0
        j=N-1
        while j>0:
            if(int(A[j])<int(A[j-1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
    return c

N=int(input())
A=input().split()
c=bubbleSort(A,N)
S=""
for i in range(N-1):
    S+=A[i]+" "
S+=str(A[-1])
print(S)
print(c)

Solution (Python):
itemCount = int(input())
items = list(map(int, input().split(" ")))

def bubbleSort(items, itemCount):
    hasPrevious = True
    swapCount = 0
    while hasPrevious:
        hasPrevious = False
        for index in reversed(range(1, itemCount )):
            if items[index] < items[index - 1]:
                items[index], items[index - 1] = [items[index - 1], items[index]]
                hasPrevious = True
                swapCount += 1
    print(" ".join(map(str, items)))
    print(swapCount)

bubbleSort(items, itemCount)

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

def BubbleSort(A, N):
 flag = 0
 sw = 0
 i = 0
 while flag == 0:
  flag = 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]
    flag = 0
    sw += 1
  i += 1
 return str(sw)

sw = BubbleSort(A, N)
print(" ".join(map(str, A)))
print(sw)

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

flag = True
while flag:
    flag = False
    for j in range(n-1,0,-1):
      if int(a[j]) < int(a[j-1]):
       a[j],a[j-1] = a[j-1],a[j]
       flag = True
       a_swap += 1

print(" ".join(a))
print(a_swap)

Solution (Python):
n = int(input())
a = list(map(int,input().strip().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):
x = int(input())
y = input()
y = list(map(int, y.split()))
r = sorted(y)
c = 0
while y != r:
    for i, j in enumerate(y[:-1]):
        if y[i] > y[i+1]:
            y[i], y[i+1] = y[i+1], y[i]
            c += 1
print(*y)
print(c)

Solution (Python):


def print_list(X):
    first_flag = True
    for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
    print(print_line)

def bubble_sort(A, N):
    
    
    flag = True
    counter = 0

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

                flag = True
                counter += 1
        
    print_list(A)
    print(counter)

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

bubble_sort(A, N)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
n = N
cnt = 0
while n > 0:
    for i in range(N-1, 0, -1):
        if A[i] < A[i-1]:
            t = A[i]
            A[i] = A[i-1]
            A[i-1] = t
            cnt += 1
    n -= 1
for i in range(N):
    if i == N - 1:
        print(str(A[i]))
    else:
        print(str(A[i]) + " ", end = "")
print(cnt)

Solution (Python):


 
def bubbleSort(arr, n):
 
    flag = True
    swap_num = 0
 
    while flag:
        flag = False
        for j in xrange(n-1, 0, -1):
            if arr[j] < arr[j-1]:
                arr[j], arr[j-1] = arr[j-1], arr[j]
                swap_num += 1
                flag = True
        pass
    return swap_num
 
 
n = int(raw_input())
arr = map(int, raw_input().split())
 
num = bubbleSort(arr, n)
print " ".join(map(str, arr))
print num

Solution (Python):
n = int(input())
s = input().split(" ")
nums = list(map(int,s))
flag = 1
koukan = 0
while flag:
    flag = 0
    for i in range(n-1, 0, -1):
        if nums[i] < nums[i-1]:
            w = nums[i]
            nums[i] = nums[i-1]
            nums[i-1] = w
            koukan += 1
            flag = 1
for i in range(n):
    if i == n-1:
        print(nums[i])
    else:
        print(nums[i],end=" ")
print(koukan)

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

    A, count = bubbleSort(A, N)

    print(' '.join([str(a) for a in A]))
    print(count)

def bubbleSort(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

if __name__ == '__main__':
    main()

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

flag = True
count = 0
while(flag):
    flag = False
    for i in range(1, N):
        if(a[-i] < a[-i-1]):
            hold = a[-i]
            a[-i] = a[-i-1]
            a[-i-1] = hold
            flag = True
            count += 1
print(" ".join(list(map(str, a))))
print(count)

Solution (Python):

"""
Created on Sat Jun 17 09:42:24 2017

@author: syaga
"""

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    flag = 1
    num = 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
                flag = 1
                num += 1
    print(" ".join(map(str, A)))
    print(num)

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

c = 0

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

print(" ".join([str(x) for x in a]))
print(c)

Solution (Python):
def bubbleSort(A, N):
      ctn = 0
      for i in range(0,N):
            for j in range(0,N-i-1):
                  if int(A[j]) > int(A[j+1]):
                        t = A[j]
                        A[j] = A[j+1]
                        A[j+1] = t
                        ctn = ctn + 1
      for i in range(0,N):
            if i < N-1:
                  print(A[i],end=' ')
            else:
                  print(A[i])
      print(ctn)

if __name__ == "__main__":
      N = int(input())
      A = input()
      A = A.split()
      bubbleSort(A, N)

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

flag = True
c = 0
while flag:
    flag = False
    for j in reversed(range(1, N)):
        if  A[j - 1] > A[j]:
            A[j - 1],    A[j] = A[j], A[j - 1]
            c += 1
            flag = True
    
print(' '.join(map(str, A)))
print(c)

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

def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
     
    sw = bubble_sort(A, N)                            

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

if __name__ == '__main__':
    main()

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

if __name__ == "__main__":
    N = int(input())
    A = [int(i) for i in input().split()]

    A, c = bubble(A, N)
    print (*A)
    print (c)

Solution (Python):
N = int(input())
A = [int(A) for A in input().split()]
cnt = 0
for i in range(1, N):
    for j in range(N-1, i-1, -1):
        if A[j] < A[j-1]:
            t = A[j]
            A[j] = A[j-1]
            A[j-1] = t
            cnt += 1
for i in range(N):
    if i == N - 1:
        print(A[i])
    else:
        print("{}".format(A[i]), end=' ')

print(cnt)

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


def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
    
    sw = bubble_sort(A, N)

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


if __name__ == '__main__':
    main()

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

def BubbleSort(N, A):
    flag = 1
    cnt = 0
    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
    return A, cnt

res = BubbleSort(N, A)
print(" ".join(map(str, res[0])))
print(res[1])

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

def print_list(A):
    for a in A[:-1]:
        print ("%d "%a, end="")
    print(A[-1])

def BubbleSort(A, N):
    count = 0
    flag = True
    while flag:
        flag = False
        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
                count += 1
                flag = True
                
    return A, count

A_sorted, c = BubbleSort(A, N)

print_list(A_sorted)
print(c)

Solution (Python):
import sys

def swap(a, i, j):
    tmp = a[j]
    a[j] = a[i]
    a[i] = tmp
    return a

def list_to_string(a):
    s = ""
    for n in a:
        s += str(n) + " "
    return s.strip()

num = int(sys.stdin.readline().strip())

array = map(lambda x: int(x), sys.stdin.readline().strip().split(" "))

flag = True
swap_num = 0

while flag:
    flag = False
    for i in reversed(range(1, len(array))):
        if array[i] < array[i - 1]:
            swap(array, i, i-1)
            flag = True
            swap_num += 1
            
print list_to_string(array)
print str(swap_num)

Solution (Python):
c = int(input())
s = input().split()
count = 0
for j in range(c):
    for i in range(c-1,j,-1):
        if int(s[i]) < int(s[i-1]):
            s[i],s[i-1] = s[i-1],s[i]
            count += 1
print(" ".join(s))
print(count)

Solution (Python):


"""
??????????????????
??????????????????????????????????????¨?????????????????????Bubble??????
?°´??¢???????????£???????????????????????????????´????????????????????????????
??????????????????????¬???????????????¢?????´???????????§??°????????????????????????????????????

1 bubbleSort(A, N) // N ??????????´?????????? 0-????????????????????? A
2   flag = 1 // ????????£??\????´?????????¨??????
3   while flag
4     flag = 0
5     for j ??? N-1 ?????? 1 ?????§
6       if A[j] < A[j-1]
7         A[j] ??¨ A[j-1] ?????????
8         flag = 1

"""
import math

def bSort(A, N):
    cnt = 0
    f = True
    s = N-1
    e = 0
    while f == True:
        f = False

        for j in range(s, e, -1):
            x = A[j]
            y = A[j-1]


            if x < y:

                A[j] = y
                A[j-1] = x
                f = True
                cnt += 1
        e += 1
        if e > s:
            break
    return cnt


N = int(input().strip())
A = list(map(int,input().strip().split()))
cnt = bSort(A,N)
print(" ".join(map(str,A)))
print(cnt)

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

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

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

Solution (Python):
def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    a = list(A)
    ans, n = bubbleSort(a, N)
    print(*ans, sep=' ')
    print(n)

def bubbleSort(a, n):
    j = 0
    flag = 1
    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
                j += 1
    return a, j

main()

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

change = 0
flag = True
while flag:
	flag = False
	for idx in range(1,n):
		if data[idx-1] > data[idx]:
			data[idx-1],data[idx] = data[idx],data[idx-1]
			flag = True
			change += 1

print(' '.join([str(d) for d in data]))
print(change)

Solution (Python):
n = int(input())
k = [int(i) for i in input().split()]
M = 0
for i in range(n-1):
    for l in range(n-i-1):

        if k[n-2-l] > k[n-1-l]:
            v = k[n-1-l]
            k[n - 1 - l]=k[n-2-l]
            k[n - 2 - l]=v
            M+=1
        else:
            pass

print(' '.join(map(str, k)))
print(M)

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

k=0
for i in range(len(nums)-1):
    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]
            k+=1

print(*nums)
print(k)

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

ans = 0
for i in range(n):
    for j in range(n-1):
        if A[j+1] < A[j]:
            A[j], A[j+1] = A[j+1], A[j]
            ans += 1
print(*A)
print(ans)

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

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
            count += 1
            flag = 1
            

for i in range(n):
    if i == n -1:
        print a[i]
    else:
        print a[i],

print count

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

def bubblesort(A, N):
    flag = True
    i = 0
    cnt = 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
                cnt += 1
        i += 1     
    print(*A)
    print(cnt)

bubblesort(A,N)

Solution (Python):

N=int(input())
A=[int(i) for i in input().split()]
fl=1
C=0
while fl==1:
    fl=0
    for j in range(N-1):
        if A[N-1-j]<A[N-2-j]:
            t=A[N-1-j]
            A[N-1-j]=A[N-2-j]
            A[N-2-j]=t
            C+=1
            fl=1
for j in range(N):
    A[j]=str(A[j])
print(" ".join(A))
print(C)

Solution (Python):


import sys, math, os


PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


def printA(A):
    print(' '.join([str(a) for a in A]))

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

        
N = int(input())
A = [int(_) for _ in input().split()]
A, swaps = bubble_sort(A)
printA(A)
print(swaps)

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

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

def bubblesort(ary):
    cnt = 0
    for i in range(q):
        for j in range(q-1, i, -1):
            if ary[j] < ary[j-1]:
                ary[j-1], ary[j] = ary[j], ary[j-1]
                cnt += 1
    return (ary, cnt)

ary, cnt = bubblesort(sort1)
print(" ".join(map(str, ary)))
print(cnt)

Solution (Python):
N = int(input())
A = [int(n) for n in input().split()]
flg = 1
cnt = 0
while flg:
    flg = 0
    for i in range(N-1, 0, -1):
        if A[i] < A[i-1]:
            t = A[i]
            A[i] = A[i-1]
            A[i-1] = t
            flg = 1
            cnt += 1
for i in range(N):
    if i == N - 1:
        print(A[i])
    else:
        print(A[i], end=" ")
print(cnt)

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


N = int(raw_input())
L = map(int, raw_input().split())

bubbleSort(L, N)

Solution (Python):
def bubbleSort(A, N):
    c = 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]
                c += 1
                flag = 1
    print " ".join(map(str, A))
    print c


N = int(raw_input())
L = map(int, raw_input().split())

bubbleSort(L, N)

Solution (Python):
n=int(input())
z=list(map(int,input().split()))
flag=1
a=0
cnt=0
while flag:
    flag=0

    for i in reversed(range(a,n)):
        if i ==0:
            break
        if z[i]<z[i-1]:
            tmp=z[i]
            z[i]=z[i-1]
            z[i-1]=tmp
            flag=1
            cnt+=1
        
    a+=1
print(' '.join(list(map(str,z))))
print(cnt)

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

count = 0
flag  = 1

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

print(*a)
print(count)

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

count = 0
flag  = 1

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

print(*a)
print(count)

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

count = 0
flag  = 1

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

print(*a)
print(count)

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

count = 0
first = 0

while first < n:
    for i in range( n-1, first, -1 ):
        if a[i] < a[i-1]:
            t = a[i]
            a[i] = a[i-1]
            a[i-1] = t
            count += 1
    first += 1

print(*a)
print(count)

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

count = 0
first = 0

while first < n:
    for i in range( n-1, first, -1 ):
        if a[i] < a[i-1]:
            t = a[i]
            a[i] = a[i-1]
            a[i-1] = t
            count += 1
    first += 1

print(*a)
print(count)

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

count = 0
first = 0

while first < n:
    for i in range( n-1, first, -1 ):
        if a[i] < a[i-1]:
            t = a[i]
            a[i] = a[i-1]
            a[i-1] = t
            count += 1
    first += 1

print(*a)
print(count)

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

count = 0
first = 0

while first < n:
    for i in range( n-1, first, -1 ):
        if a[i] < a[i-1]:
            t = a[i]
            a[i] = a[i-1]
            a[i-1] = t
            count += 1
    first += 1

print(*a)
print(count)

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

last  = 0
count = 0
flag  = True

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

print(*a)
print(count)

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

last  = 0
count = 0
flag  = True

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

print(*a)
print(count)

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

Solution (Python):

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

    swapOperation = 0

    flag = 1
    while flag == 1:
        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
                swapOperation += 1
    print(" ".join(map(str,A)))
    print(swapOperation)

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

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

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


N = int(raw_input())
A = map(int, raw_input().split())

bubble_sort(A, N)

Solution (Python):


def bsort(array):
    i = 0
    counter = 0
    while i < len(array)-1:
        for c in range(len(array)-1,i,-1):
            if array[c]<array[c-1]:
                a = array[c-1]
                array[c-1] = array[c]
                array[c] = a
                counter += 1

        i = i+1
    resul = ' '.join([str(i) for i in array])
    print(resul)
    print(counter)

n = int(input())

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

bsort(intarr)

Solution (Python):
def bubble_sort(A, N):
    flag = True
    i, c = 0, 0
    while flag:
        flag = False
        for j in range(1, N-i):
              if A[j] < A[j-1]:
                  A[j],  A[j-1] = A[j-1], A[j]
                  flag = True
                  c += 1
        i += 1
    
    print " ".join(map(str, A))
    print c


N = int(raw_input())
A = map(int, raw_input().split())
 
bubble_sort(A, N)

Solution (Python):
def bubbles(N):
    count = 0
    for i in range(len(N)):
        for j in range(len(N)-1, i, -1):
            if N[j] < N[j-1]:
                N[j], N[j-1] = N[j-1], N[j]
                count += 1
    c = 1
    for i in N:
        print(i, end='')
        if c < len(N):
            print(' ', end='')
            c += 1
    print('')
    return count

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

print(bubbles(numbers))

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

print(count)

Solution (Python):
def inputInline():
    N=int(input())
    numbers=list(map(int,input().split(" ")))
    return numbers
def bubbleSort(list):
    N=len(list)
    count=0
    flag=True
    while flag:
        flag=False
        for i in range(N-1):
            if list[i]>list[i+1]:
                temp=list[i]
                list[i]=list[i+1]
                list[i+1]=temp
                flag=True
                count+=1
    return (list,count)
result=bubbleSort(inputInline())
print(" ".join(list(map(str,result[0]))))
print(result[1])

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
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	return count

def main():
	n = int(input())
	data = list(map(int, input().split()))

	c = bubblesort(data, n)
	print(*data)
	print(c)

if __name__ == "__main__":
	main()

Solution (Python):

n = int(input())
nums = [int(x) for x in input().split(" ")]
flag = True
count = 0
while flag:
    flag = False
    for i in range(n - 1, 0, -1):
        if nums[i] < nums[i - 1]:
            nums[i], nums[i - 1] = nums[i - 1], nums[i]
            flag = True
            count += 1
print(" ".join([str(i) for i in nums]))
print(count)

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

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

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]
            count += 1
        flag = 1
        
    i += 1

for w in range(n):
    if w != n-1:
        print(a[w],end=" ")
    else:
        print(a[w])
print(count)

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


n = int(input())
a = list(map(int, input().split()))
count = bubbleSort(a, n)
print(" ".join(map(str, a)))
print(count)

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

A, c = bubblesort(int(input()), list(map(int, input().split())))
print(' '.join([str(v) for v in A]))
print(c)

Solution (Python):
def bubbleSort(lst, n):
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in range(n-1, 0, -1):
            if lst[j] < lst[j-1]:
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp
                flag = 1
                i = i + 1
    print(" ".join([str(n) for n in lst]))
    print(i)


if __name__ == "__main__":
    n = int(input())
    lst = [int(n) for n in input().split()]
    bubbleSort(lst, n)

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

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

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

bubbleSort(A, n)

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

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

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

temp = 0
cnt = 0
for i in range(n):
	for j in range(n-i-1):
		if a[j] > a[j+1]:
			temp = a[j]
			a[j] = a[j+1]
			a[j+1] = temp
			cnt += 1

for i in range(n):
	if i != n-1:
		print(a[i]),
	else:
		print(a[i])

print cnt

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

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

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

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


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

A,count = bubbleSort(A,N)

for i in range(len(A)):  
    if i == len(A) - 1:
        print(A[i])
    else:
        print(A[i], end=' ')

print(count)

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

Solution (Python):

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

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

nSwap = 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]
            nSwap += 1

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

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

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

    nums_str = [str(i) for i in nums]
    print(" ".join(nums_str))
    print(count)

if __name__ == '__main__' :
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
c = 0
flag = True
while flag:
    flag = False
    for j in range(n - 1, 0, -1):
        if a[j] < a[j - 1]:
            t = a[j]
            a[j] = a[j - 1]
            a[j - 1] = t
            c += 1
            flag = True
print(*a)
print(c)

Solution (Python):
def bubbleSort(A, N):
    s = 0
    flag = 0
    while flag == 0:
        flag = 1
        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 = 0
                s += 1
    t = str(A[0])
    for i in range(1,N):
        t = t + " " + str(A[i])
    print(t)
    print(s)

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

bubbleSort(a, n)

Solution (Python):
NN=raw_input()
N=raw_input()
A = [int(i) for i in N.split()]
swap=0

for i in range(int(NN)):
  for j in reversed(range(i+1,int(NN))):
    if A[j] < A[j-1] :
      t = A[j]
      A[j] = A[j-1]
      A[j-1] = t
      swap+=1
      
print ' '.join([str(k) for k in A])
print swap