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())
a=[int(i) for i in input().split()]

f=1 
j=0
while f:
    f=0
    for i in range(n-1):
        if a[n-i-1]<a[n-i-2]:
            a[n-i-1],a[n-i-2]=a[n-i-2],a[n-i-1]
            f=1
            j+=1
print(" ".join(list(map(str,a))))
print(j)

Solution (Python):
def bb(A,n):
    cnt=0
    for k in range(n):
        for i in range(n-1,k,-1):
            if A[i] < A[i-1]:
                tmp=A[i]
                A[i]=A[i-1]
                A[i-1]=tmp
                cnt=cnt+1
    return A,cnt

n=int(input())
A=list(map(int,input().split()))
A,cnt=bb(A,n)
As=[str(i)for i in A]
As = ' '.join(As)
print(As)
print(cnt)

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

frag = 1
point = 0
while frag == 1:
    frag = 0
    for i in range(n-1,0,-1):
        if li[i] < li[i-1]:
            li[i],li[i-1] = li[i-1],li[i]
            frag = 1
            point += 1
print(' '.join(map(str, li)))
print(point)

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

Solution (Python):
n=int(input())
s=list(map(int,input().split()))
swap=0

def bubble_sort(A,N,swap):
    flag=True
    while flag:
        flag=False
        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=True
    return A,swap

Rst,swap=bubble_sort(s,n,swap)

print(Rst[0],end='',sep='')
for i in range(1,n):
    print(" ",Rst[i],end='',sep='')
print()
print(swap)

Solution (Python):
def bubble_sort(a, n):
    
    cnt = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1, n)):
            if a[j] < a[j-1]:
                a[j], a[j-1] = a[j-1], a[j]
                cnt += 1
                flag = True
    print(" ".join([str(x) for x in a]))
    print(cnt)


def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    bubble_sort(a, n)


if __name__ == '__main__':
    main()

Solution (Python):


N=int(input())
A=list(map(int,input().split()))
counter=0
flag=True
string=""
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]
            counter+=1
            flag=True

A_str=map(str,A)
string=" ".join(A_str)
print(string)
print(counter)

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

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

flag = 1
cnt = 0

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

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

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

swap_count = 0
def swap(A, i, j):
    t = A[i]
    A[i] = A[j]
    A[j] = t

for i in range(0, len(A)):
    for j in range(len(A) - 1, i, -1):
        if A[j] < A[j-1]:
            swap(A, j, j-1)
            swap_count += 1

print(" ".join([str(v) for v in A]))
print(swap_count)

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

cnt = 0
for i in range(n - 1):
    last = n - i - 1
    for j in range(last):
        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):


swap = 0
n = int(input())
a = list(map(int, input().split()))
for i in range(n - 1):
    for j in range(n - 1 - i):
        if a[j] > a[j + 1]:
            temp = a[j]
            a[j] = a[j + 1]
            a[j + 1] = temp
            swap += 1
print(' '.join([str(i) for i in a]))
print(swap)

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


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

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, 0, -1):
        if A[i-1] > A[i]:
            A[i-1], A[i] = A[i], A[i-1]
            flag = 1
            count += 1
print(*A)
print(count)

Solution (Python):
n=int(input())
box=list(map(int,input().split()))
count=0
for i in range(n):
    p=n-1
    while p!=i:
        if box[p]<box[p-1]:
            box[p],box[p-1]=box[p-1],box[p]
            count +=1
        p -=1
print(*box)
print(count)

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

count = 0
dec = 0

is_reverse = True
while is_reverse:
    is_reverse = False
    
    for i in range(n-1-dec):
        if l[i] > l[i+1]:
            l[i] , l[i+1] = l[i+1], l[i]
            is_reverse = True
            count += 1
    
    dec += 1

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

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

main(int(input()))

Solution (Python):


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


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

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

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

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

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

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

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

flg = True
ans = 0

while flg:
    flg = False
    for i in range(N-1):
        if A[i] > A[i+1]:
            A[i], A[i+1] = A[i+1], A[i]
            flg = True
            ans += 1
    
print(' '.join(list(map(str, A))))
print(ans)

Solution (Python):

def print_result(arr, n):
    for i in range(n):
        if i > 0:
            print(' ', end='')
        print(arr[i], end='')
    print('\n', end='')


def bubble_sort(arr, n):
    flag = True
    i = 0
    sw_count = 0
    while flag:
        flag = False
        for j in range(n-1, i, -1):
            if arr[j] < arr[j-1]:
                arr[j], arr[j-1] = arr[j-1], arr[j]
                flag = True
                sw_count += 1
        i += 1
    return sw_count


n = eval(input())
arr = [eval(x) for x in input().split()]
sw = bubble_sort(arr, n)
print_result(arr, n)
print(sw)

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

flag = True

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

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

print(count)

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

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

Solution (Python):

def main():
    N, *A = map(int, open(0).read().split())
    ans = 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]
                ans += 1
    print(*A)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    count=0
    flag = True
    while(flag):
        flag = False
        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 = True
    print(*A)
    print(count)

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]:
			v = A[j]
			A[j] = A[j-1]
			A[j-1] = v
			flag = 1
			cnt += 1
print(" ".join(list(map(str,A))
))
print(cnt)

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

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

def show():
    for i in range(len(lst)):
        print(lst[i], end="")
        if i < len(lst)-1:
            print(" ", end="")
        else:
            print()

f = True
count = 0
while f:
    f = False
    for j in reversed(range(1,len(lst))):
        if lst[j] < lst[j-1]:
            tmp = lst[j]
            lst[j] = lst[j-1]
            lst[j-1] = tmp
            count += 1
            f = True
show()
print(count)

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
flag = 0
for i in reversed(range(n)):
    for j in range(i):
        if li[j] > li[j+1]:
            li[j], li[j+1] = li[j+1], li[j]
            flag += 1

print(*li)
print(flag)

Solution (Python):
def test():

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

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

	for i in range(n-1):
		print(f"{A[i]} ",end = '')
	print(f"{A[n-1]}")
	print(swap_count)


if __name__ == "__main__":
	test()

Solution (Python):



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

tmp = 0
cnt = 0
flag = 1

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

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

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

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]
                flag = 1
                cnt += 1
    return A, cnt

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

A, cnt = bubbleSort(A, N)
print(" ".join([str(i) for i in A]))
print(cnt)

Solution (Python):
n = int(input())
A =[int(i) for i in 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(list(map(str,A))))
print(count)

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

print(' '.join([str(a) for a in A]))
print("{}".format(count))

Solution (Python):

N = int(input())

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

num = 0

flag = True

while flag:

    flag = False

    for j in range(N-1):

        j = N-1-j

        if A[j] < A[j-1]:

            oaj = A[j]

            A[j] = A[j-1]
            A[j-1] = oaj

            flag = True

            num += 1

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

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

def BubbleSort(A):
    N = len(A)
    count = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in reversed(range(i+1,N)):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                count += 1
                flag = 1
        i += 1
    for k in range(N-1):
        print(A[k], end=" ")
    print(A[N-1])
    print(count)

BubbleSort(A)

Solution (Python):

def bubbleSort(list, n):
    cnt = 0  
    for i in range(n-1):
        for j in reversed(range(i+1, n)):
            if list[j] < list[j-1]:
                list[j], list[j-1] = list[j-1], list[j]
                cnt += 1
    return cnt

N = int(input())
array = list(map(int, input().split()))
ans = bubbleSort(array, N)

print(array[0], end="")
for i in range(1, N):
    print(" %d" % array[i], end="")
print("\n%d" % ans)

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

cnt = 0
for a in A:
    for i in range(r - 1, 0, -1):
        if A[i] < A[i-1]:
            A[i-1], A[i] = A[i], A[i-1]
            cnt+=1
print(" ".join(map(str, A)))
print(cnt)

Solution (Python):


def printout(array, n):
    output = ""
    for i in range(n - 1):
        output += str(array[i]) + " "
    output += str(array[N - 1])
    print(output)


N = int(input())
array = [int(i) for i in input().split()]
count = 0;
for i in range(N):
    for j in reversed(range(i+1,N)):
        if (array[j-1] > array[j]):
            tmp = array[j]
            array[j] = array[j-1]
            array[j-1] = tmp
            count += 1
printout(array,N)
print(count)

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

for i in range(0,n-1):
    flag=0
    for j in reversed(range(i+1,n)):
        if a[j-1]>a[j]:
            ab=a[j-1]
            a[j-1]=a[j]
            a[j]=ab
            flag=1
            cnt+=1
def convert(list): 
    res=" ".join(map(str, list)) 
    return res   
print(convert(a))
print(cnt)

Solution (Python):
def getInput():
    n = int(input())
    a = [int(i) for i in input().split(" ")]
    return n, a

def swap(x, y):
    v = x
    x = y
    y = v
    return x, y

def bubbleSort(n, a):
    cnt = 0
    for j in range(n):
        init = True
        for i in range(n-1, -1, -1):
            if init:
                init = False
                continue
            if a[i] > a[i+1]:
                a[i], a[i+1] = swap(a[i], a[i+1])
                cnt += 1
    print(" ".join([str(i) for i in a]))
    print(cnt)
    return

n, a = getInput()
bubbleSort(n, a)

Solution (Python):
roop_num = int(input())
values = [int(value) for value in input().split()]

exchange_num = 0

while roop_num:
  for i in range(1,roop_num):
    if values[i - 1] > values[i]:
      values[i - 1], values[i] = values[i], values[i - 1]
      exchange_num += 1
  roop_num -= 1

ans = map(lambda value:str(value), values)

print(' '.join(ans).strip())
print(exchange_num)

Solution (Python):
def bubbleSort(A, N):
    count = 0
    for i in reversed(range(N)):
        for j in range(i):
            if A[j] > A[j+1]:
                
                A[j], A[j+1] = A[j+1], A[j]
                count += 1
    output(A, count)

def output(A, N):
    for a in range(len(A)):
        if a == len(A)-1:
            print(A[a])
            print(N)
        else:
            print(A[a], end=" ")

def main():
    N = int(input())
    list = input().split(" ")
    list = [int(s) for s in list]
    
    bubbleSort(list, N)

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split(' ')))
    c = 0
    flag = 1
    while flag:
        flag = 0
        j = 0
        for i in range(n-1, j, -1):
            if a[i] < a[i-1]:
                a[i], a[i-1] = a[i-1], a[i]
                c += 1
                flag = 1
        j -= 1
    print(' '.join(map(str, a)))
    print(c)


if __name__ == '__main__':
    main()

Solution (Python):
num_inputs = int(input())
array = input().split(' ')
flag = True
count = 0

while flag:
    flag = False
    for i in reversed(range(1, num_inputs)):
        if int(array[i]) < int(array[i - 1]):
            buf = array[i]
            array[i] = array[i - 1]
            array[i - 1] = buf
            count += 1
            flag = True

print(' '.join(array))
print(str(count))

Solution (Python):
def bubble_sort(A):
    flag = True
    swap = 0
    while flag:
        flag = False
        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]
                swap += 1
                flag = True
    return swap
 
if __name__=='__main__':
   N=int(input()) 
   A=list(map(int,input().split()))
   swap = bubble_sort(A)
   print(*A)
   print(swap)

Solution (Python):
def bubblesort(array):
    flag = True
    res = 0
    while flag:
        flag = False
        for i in range(len(array)-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                res += 1
                flag = True
    return (array, res)
      
int(input())
arr, c = bubblesort([int(i) for i in input().split()])
print(*arr,sep=' ')
print(c)

Solution (Python):
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
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):
def bubblesort(a,n,c):
    c=0
    flag=1
    while flag:
        flag=0
        for j in range(n-1,0,-1):
            if a[j]<a[j-1]:
                m=a[j]
                a[j]=a[j-1]
                a[j-1]=m
                flag=1
                c+=1
    return a,c

n=int(input())
a=list(map(int,input().split()))
c=0
ans=bubblesort(a,n,c)
a=ans[0]
c=ans[1]
print(" ".join(map(str,a)))
print(c)

Solution (Python):
def bubbleSort(A,N):
    global cnt
    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
                cnt+=1
N = int(input())
A = list(map(int,input().split()))
cnt = 0
bubbleSort(A,N)
print(*A)
print(cnt)

Solution (Python):
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
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):
def bubblesort(array):
    flag = True
    res = 0
    while flag:
        flag = False
        for i in range(len(array)-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                res += 1
                flag = True
    return (array, res)
     
int(input())
arr, c = bubblesort([int(i) for i in input().split()])
print(*arr,sep=' ')
print(c)

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

def bubbleSort(n,A):
    c = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1,n)):
            if A[j] < A[j-1]:
                c+=1
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = True
    return (A,c,)


R,C = bubbleSort(n,A)
print(" ".join(["{0["+ str(i) + "]}" for i in range(n)]).format(R))
print(C)

Solution (Python):
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
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 = int(input())
A = list(map(int, input().split()))

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

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

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

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

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

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

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

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

cnt = 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
            cnt += 1

print(*A)
print(cnt)

Solution (Python):
class inte:
	n = 0
	def __init__(self, n):
		self.n = n
	def chmax(self, n):
		self.n = n if self.n < n else self.n
	def chmin(self, n):
		self.n = n if self.n > n else self.n

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

def sort(a):
	count = 0
	while True:
		for i in reversed(range(1, len(a))):
			if a[i] < a[i - 1]:
				a[i], a[i - 1] = a[i - 1], a[i]
				count += 1
				break
		else:
			return count

count = sort(A)
print(*A)
print(count)

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

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

print(*a, sep=' ')
print(count)

Solution (Python):

def BubleSort(N, data):
    flag = 1
    cnt = 0
    while flag:
        flag = 0
        for i in range(N-1, 0, -1):
            if data[i] < data[i-1]:
                data[i], data[i-1] = data[i-1], data[i]
                flag = 1
                cnt = cnt + 1
    return data, cnt

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

new_data, cnt = BubleSort(N, data)

for n in range(N):
    if n:
        print(" ", end="")
    print(data[n], end="")
print()
print(cnt)

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):
count = int(input())
numbers = input().split(" ")
for i in range(len(numbers)):
    numbers[i] = int(numbers[i])


if __name__ == "__main__":
    swap_count = 0

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

    print(*numbers)
    print(swap_count)

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

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

def bubble_sort(a, n):
    counter = 0
    for i in range(n-1, 0, -1):
        for j in range(i):
            if a[j] > a[j+1]:
                a[j], a[j+1] = a[j+1], a[j]
                counter += 1

    return a, counter

a, counter = bubble_sort(a, n)
print(" ".join(map(str, a)))
print(counter)

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

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):
def bubble_sort(A):
    num = 0
    for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                num += 1
                
    print(' '.join(list(map(str, A))))
    print(num)

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

Solution (Python):
def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        j = N-1
        while j != 0:
            if A[j] < A[j-1]:
                out = A[j-1]
                A[j-1] = A[j]
                A[j] = out
                flag = 1
                count += 1
            j -= 1
    print(' '.join(map(str, A)))
    print(str(count))
            
if __name__ == '__main__':
    N = int(input()) 
    M = list(map(int,input().split()))
    bubbleSort(M, N)

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

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

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

print(' '.join(repr(e) for e in A))
print(counter)

Solution (Python):






import sys
import math


class mymath:
    
    pi = 3.14159265358979323846264338

    
    def pnum_eratosthenes(self, n):
        ptable = [0 for i in range(n+1)]
        plist = []

        for i in range(2, n+1):
            if ptable[i]==0:
                plist.append(i)
                for j in range(i+i, n+1, i):
                    ptable[j] = 1
        return plist

    
    def gcd(self, a, b):
        if b == 0:
            return a
        return self.gcd(b, a%b)

    
    def lcm(self, a, b):
        return (a*b)//self.gcd(a,b)

    
    def mul(self, A, B):
        ans = []
        for a in A:
            c = 0
            for j, row in enumerate(a):
                c += row*B[j]
            ans.append(c)
        return ans

mymath = mymath()


class output:
    
    def list(self, l):
        l = list(l)
        
        for i, num in enumerate(l):
            print(num, end="")
            if i != len(l)-1:
                print(" ", end="")
        print()

output = output()































def get_input():
    N = []
    while True:
        try:
            N.append(input())
            
            
        except EOFError:
            break
    return N

def is_integer(n):
    try:
        float(n)
    except ValueError:
        return False
    else:
        return float(n).is_integer()

def dist(A, B):
    d = 0
    for i in range(len(A)):
        d += (A[i]-B[i])**2            
    d = d**(1/2)
    return d


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



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

for i, n in enumerate(A):
    print(n, end='')
    if i != N-1:
        print(' ', end='')
print()

print(count)

Solution (Python):
def bubblesort(A, n):
    flag = 1
    count = 0
    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]
                count = count + 1
                flag = 1
        
    A_str = [str(a) for a in A]
    A_str = " ".join(A_str)
    print(A_str)
    print(count)

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

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


if __name__ == "__main__":
    elem_num = int(input())
    elems = list(map(lambda x: int(x), input().split()))
    elems, swap_num = bubble_sort(elems)
    print(" ".join([str(elem) for elem in elems]))
    print(f"{swap_num}")

Solution (Python):
def show(A):
    L = list(map(str, A))
    print(" ".join(L))

N = int(input())
A = list(map(int, input().split()))
flag = True
cnt = 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
            cnt += 1

show(A)
print(cnt)

Solution (Python):
def bubble_sort(A):
    flag = True
    swap = 0
    while flag:
        flag = False
        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]
                swap += 1
                flag = True
    return swap
  
if __name__=='__main__':
   N=int(input()) 
   A=list(map(int,input().split()))
   swap = bubble_sort(A)
   print(*A)
   print(swap)

Solution (Python):
def bubble_sort(A):
    flag = True
    swap = 0
    while flag:
        flag = False
        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]
                swap += 1
                flag = True
    return swap
 
if __name__=='__main__':
   N=int(input()) 
   A=list(map(int,input().split()))
   swap = bubble_sort(A)
   print(*A)
   print(swap)