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 main():
    n = int(input())
    array = [int(i) for i in input().split(' ')]

    count = 0
    for i in range(n):
        for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                count += 1
                array[j], array[j-1] = array[j-1], array[j]
    print(' '.join(map(str, array)))
    print(count)

if __name__ == '__main__':
    main()

Solution (Python):

def bubble_sort(arr):
    no_swap = True
    swap_cnt = 0
    for i in range(len(arr), 0, -1):
        if not no_swap:
            break
        for j in range(0, i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                swap_cnt += 1
            no_swap = True
    return swap_cnt


if __name__ == '__main__':
    N = int(input())
    arr = list(map(int, input().split()))
    cnt = bubble_sort(arr)
    print(' '.join(map(str, arr)))
    print(cnt)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
nums = [int(_) for _ in stdin.readline().rstrip().split()]

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

ret, count = bubble_sort(nums, N)
print(*ret)
print(count)

Solution (Python):

if __name__ == '__main__':
    n = int(raw_input())
    A = map(int, raw_input().split())
    cnt = 0
    for i in range(0, n):
        for j in range(n - 1, i, -1):
            if A[j - 1] > A[j]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
    for x in A:
        print x,
    print
    print cnt

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

A = []

nums = input().split()

for i in range(N):
    A.append(int(nums[i]))

    

flag = 1
a = 0
cnt = 0

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

l = [str(i) for i in A]
print(" ".join(l))
print(cnt)

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

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

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

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

Solution (Python):
def p(a):
    for i in a[:-1]:
        print(i,end=' ')
    print(a[-1])


def bubble_sort(a):
    cnt = 0
    for i in range(len(a)-1):
        for j in range(i+1,len(a))[::-1]:
            if a[j-1]>a[j]:
                cnt+=1
                tmp = a[j]
                a[j] = a[j-1]
                a[j-1] = tmp
    p(a)
    print(cnt)


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

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

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

c = 0
for j in range(len(A)):
    for k in range(len(A) - 1, j, -1):
        if A[k] < A[k - 1]:
            A[k], A[k - 1] = A[k-1], A[k]
            c += 1
hyoji(A)
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
for i in range(n):
    judge=False
    for j in range(i+1,n)[::-1]:
        if a[j-1]>a[j]:
            a[j-1],a[j]=a[j],a[j-1]
            judge=True
            cnt+=1

print(*a)
print(cnt)

Solution (Python):
def print_format(N,input_list, count):
    for i in range(N):
        if i != N-1:
            print(input_list[i],end=" ")
        else :
            print(input_list[i])
    print(count)
def bubble_sort(N,input_list):
    count = 0
    flag = True
    while flag:
        flag = False
        for j in range(1,N)[::-1]:
            if input_list[j] < input_list[j-1]:
                input_list[j],input_list[j-1] = input_list[j-1],input_list[j]
                count += 1
                flag = True
    return input_list, count

def main():
    N = int(input())
    input_list = [int(i) for i in input().split()]
    input_list,count = bubble_sort(N,input_list)
    print_format(N,input_list,count)


if __name__ == "__main__":
    main()

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

cnt = 0
flg = True
while flg:
    flg = False
    for i in range(N-1,0,-1):
        if As[i-1] > As[i]:
            tmp = As[i]
            As[i] = As[i-1]
            As[i-1] = tmp        
            flg = True
            cnt += 1
print(" ".join([str(a) for a in As]))
print(cnt)

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

f = True
ans = 0

while f:
    f = False
    for i in range(1, len(a)):
        if a[i] < a[i - 1]:
            ans += 1
            a[i], a[i - 1] = a[i - 1], a[i]
            f = True

print(*a)
print(ans)

Solution (Python):
length = int(input())
List = list(map(int,input().split()))

count = 0
state = 1

while state:
    state = 0
    for i in range(length-1):
        if List[i] > List[i+1]:
            stuck = List[i]
            List[i] = List[i+1]
            List[i+1] = stuck
            state = 1
            count += 1

for i in range(length):
    if i == length - 1:
        print(List[length-1])
    else: print(List[i],end=' ')
print(count)

Solution (Python):




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

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

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

Solution (Python):
array_to_str = lambda A: ' '.join([str(a) for a in A])
input_number = lambda: int(input())
input_num_array = lambda: [int(a) for a in input().split()]

def bubble_sort(A, N):
  flag = 1
  exchange_num = 0
  while flag:
    flag = 0
    for j in range(N)[:0:-1]:
      if A[j] < A[j-1]:
        exchange_num += 1
        A[j],A[j-1] = A[j-1], A[j]
        flag = 1
  print(array_to_str(A))
  print(exchange_num)
  
N = input_number()
A = input_num_array()
bubble_sort(A, N)

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
i = 0

flag = True
while flag:
    flag = False
    for j in range(1, n)[::-1]:
        if li[j] < li[j-1]:
            tmp = li[j]
            li[j] = li[j-1]
            li[j-1] = tmp
            i += 1
            flag = True
            

print(str(li).replace(',', '')[1:-1])
print(i)

Solution (Python):
def bubbleSort(A, n):
    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
    print(*A)
    print(cnt)

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

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

L = A[:]
sorted_l = []
un_sorted_l = L[:]
counter = 0
while un_sorted_l:
    n = len(un_sorted_l)
    for i in reversed(range(1, n)):
        if un_sorted_l[i-1] <= un_sorted_l[i]:
            continue
        else:
            counter += 1
            un_sorted_l[i-1], un_sorted_l[i] = un_sorted_l[i], un_sorted_l[i-1]
    sorted_l.append(un_sorted_l[0])
    un_sorted_l = un_sorted_l[1:]


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

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

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

print(*A)
print(cnt)

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

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

swap_count = 0
for i in range(0, n):
    for j in reversed(range(i + 1, n)):
        if a_list[j - 1] > a_list[j]:
            temp = a_list[j]
            a_list[j] = a_list[j - 1]
            a_list[j - 1] = temp
            swap_count = swap_count + 1

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

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

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

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

Solution (Python):

N=int(input())
L=input().split( )
A=[int(L[i]) for i in range(N)]
c=0
j=N-1
flag=1
while flag:
    flag=0
    for j in range(N-1):
        if A[j+1]<A[j]:
            temp=A[j]
            A[j]=A[j+1]
            A[j+1]=temp
            c+=1
            flag=1
for j in range(N):
    if j<N-1:
        print(A[j],end=" ")
    else:
        print(A[j])
print(c)

Solution (Python):



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

flag = True
while flag:
    flag = False
    for i in range(len(data)-1,0,-1):
        if data[i] < data[i-1]:
            tmp = data[i]
            data[i] = data[i-1]
            data[i-1] = tmp
            count +=1
            flag = True
ans = ""
for s in data:
    if ans != "":
        ans = ans+ " "
    ans = ans + str(s)



print(ans)
print(count)

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
flg=True
counter=0
while flg:
    flg=False
    for j in reversed(range(1,n)):
        if l[j]<l[j-1]:
            l[j],l[j-1]=l[j-1],l[j]
            flg=True
            counter+=1
for i in range(n):
    if i!=n-1:
        print(l[i],end=' ')
    else:
        print(l[i])
print(counter)

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

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

for k in range(n-1):
    print(a[k],end=" ")
print(a[n-1])
print(number)

Solution (Python):
def bublleSort(A):
    cnt = 0

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

    return A, cnt


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

A_sorted, cnt = bublleSort(A)

print(*A_sorted)
print(cnt)

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()))
ans, count = bubbleSort(a, N)
print(' '.join(map(str, a)))
print(count)

Solution (Python):
if __name__ == '__main__':
	n = int(input())
	num = [int(i) for i in input().split()]
	
	
	flag = 1
	count = 0
	while flag != 0:
		flag = 0
		rev = list(range(1,n))
		rev.reverse()
		for j in rev:
			if num[j] < num[j-1]:
				tmp = num[j]
				num[j] = num[j-1]
				num[j-1] = tmp
				flag = 1
				count += 1
	for i in range(n):
		if i != n-1:
			print("%d" % (num[i]),end=" ")
		else:
			print("%d" % (num[i]))
	print(count)

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

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

num = 0
for i in range(1, N):
  while A[i] < A[i-1] and i-1 >= 0:
    temp = A[i-1]
    A[i-1] = A[i] 
    A[i] = temp
    i -= 1
    num += 1
 
print(*A)
print(num)

Solution (Python):
num = int(input())
line = list(map(int, input().split()))
count = 0
for i in range(1,num):
    for j in range(i):
        if line[i-j] < line[i-j-1]:
            a = line[i-j]
            line[i-j] = line[i-j-1]
            line[i-j-1] = a
            count+= 1
        else:
            break
line_str = [str(k) for k in line]
print(' '.join(line_str))
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,0,-1):
        if a[j]<a[j-1]:
            key = a[j]
            a[j] = a[j-1]
            a[j-1] = key
            count += 1

string = ''
for s_val in a[:-1]:
    string += str(s_val) + ' '
string += str(a[-1])
print(string)
print(count)

Solution (Python):

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

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

def bubbleSort(A, N):
    counter = 0
    i = 0
    flag = 1
    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
                counter += 1
        i += 1
    print(" ".join(map(str, A)), counter, sep="\n")
bubbleSort(A, N)

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

print(" ".join([str(i) for i in A]))
print(ans)

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

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

Solution (Python):
N = int(input())
a = list(map(int, input().strip().split()))
rev = 0
counter = 0
while(rev >= 0):
    for i in range(1, N):
        if(a[i-1] > a[i]):
            a[i-1], a[i] = a[i], a[i-1]
            rev += 1
            counter += 1
    if(rev == 0):
        for j in range(0, N-1):
            print(a[j], end=" ")
        print(a[N-1])
        print(counter)
        break
    rev = 0

Solution (Python):
N = int(input())
a = list(map(int, input().strip().split()))
rev = 0
counter = 0
while(rev >= 0):
    for i in range(1, N):
        if(a[i-1] > a[i]):
            a[i-1], a[i] = a[i], a[i-1]
            rev += 1
            counter += 1
    if(rev == 0):
        for j in range(0, N-1):
            print(a[j], end=" ")
        print(a[N-1])
        print(counter)
        break
    rev = 0

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

flag = 1
count = 0
while flag == 1:
  flag = 0
  for i in range(N - 1, 0, -1):
    if int(list[i]) < int(list[i - 1]):
      tmp = list[i]
      list[i] = list[i - 1]
      list[i - 1] = tmp
      count += 1
      flag = 1
print(' '.join(list))
print(count)

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

c = 0
flag = 1
while flag:
    flag=0
    for j in reversed(range(1, n)):
        if a[j] < a[j-1]:
            b = a[j]
            a[j] = a[j-1]
            a[j-1] = b
            flag = 1
            c += 1
print(*a)
print(c)

Solution (Python):
num = int(input())
x = list(map(int, input().split()))
n = 0

flag = 1
while flag:
    flag = 0
    for j in range(1, num):
        if x[j] < x[j-1]:
            x[j], x[j-1] = x[j-1], x[j]
            flag = 1
            n += 1
print(' '.join([str(s) for s in x]))
print(n)

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

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

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


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

result, change_n = bubbleSort(a, n)


print (" ".join(map(str,result)))
print(change_n)

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

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

count = 0
flag = 1

for i in range(N):
    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
                count += 1
                flag = 1

show(A)
print(count)

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

	return A, count

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

A, count = bubble_sort(A, N)
print(' '.join(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 i in range(n-1):
        if A[i] > A[i+1]:
            v = A[i]
            A[i] = A[i+1]
            A[i+1] = v
            flag = True
            count += 1
print(" ".join(map(str, A)))
print(count)

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

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

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

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

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

for i in range(N - 1):
    for j in reversed(range(i + 1, N)):
        if num[j - 1] > num[j]:
            p = num[j]
            num[j] = num[j - 1]
            num[j - 1] = p
            c += 1

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

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]:
            count += 1
            nums[j], nums[j-1] = nums[j-1], nums[j]
for i in range(n-1):
    print(nums[i], end = ' ')
print(nums[n-1])
print(count)

Solution (Python):
def bubbleSort(A,N):
    flag = 1
    swapCount = 0
    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
                swapCount += 1
                flag = 1
    str = ""
    for i in range(N):
        str += "{} ".format(A[i])
    print(str[:len(str)-1])
    print(swapCount)


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

    bubbleSort(A,N)

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

while True:
    if f == 0 :
        break
    
    for i in range(f):
        if A[i] > A[i+1]:
            v = A[i]
            A[i] = A[i+1]
            A[i+1] = v
            change_sum += 1
    f += -1    

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

print(change_sum)

Solution (Python):
import sys






"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        
        
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    n = ip.I()
    a = ip.IL()
    flag = True
    ans = 0
    while flag:
        flag = False
        for j in r(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                flag = True
                ans += 1
    p(*a)
    p(ans)


main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
c = 0
for _ in range(n):
    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
print(" ".join(map(str,a)))
print(c)

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


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

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

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

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 = [int(i) for i in input().split()]
flag = True
cnt = 0
while flag:
    flag = False
    for i in range(1,n)[::-1]:
        if a[i] < a[i-1]:
            a[i], a[i-1] = a[i-1], a[i]
            cnt += 1
            flag = True
print(' '.join([str(i) for i in a]))
print(cnt)

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

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

A, count = bubbleSort(A, N)
print(*A)
print(count)

Solution (Python):
def readInt():
  return int(input())


def readInts():
  return [int(i) for i in input().split()]


N = readInt()
AS = readInts()

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

print(*AS)
print(cnt)

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

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

tmp = input().split(" ")
nums = []
for i in tmp:
    nums.append(int(i))

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

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

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

print(*a)
print(count)

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

N = int(input())
A = list(map(int, input().split()))
cnt = bubble_sort(A)
print(' '.join(list(map(str, A))))
print(cnt)

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

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

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

print(" ".join([str(_v) for _v in v]))
print(c)

Solution (Python):
n = int(input())
nums = [int(num) for num in input().split(" ")]
flag = 1
exchange_num = 0
result_string = ""        

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

for i in range(n):
    if result_string == "":
        result_string += str(nums[i])
    else:
        result_string += " "
        result_string += str(nums[i])

print(result_string)
print(exchange_num)