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(x) for x in input().split()]

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

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

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

Solution (Python):
num = int(input())
statements = input().split()
count = 0

for j in range(num):
    for i in range(num-1):
        statements[i] = int(statements[i])
        statements[i+1] = int(statements[i+1])
        if statements[i] > statements[i+1]:
            temp = statements[i] 
            statements[i] = statements[i+1]
            statements[i+1] = temp
            count += 1

print(*statements)
print(count)

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

count = 0
for b in range(len(A)-1):
    for a in range(0,len(A)-1-b):
        if A[-a-1] < A[-a-2]:
            A[-a-1],A[-a-2] = A[-a-2],A[-a-1]
            count += 1

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
flag = 1
count = 0
while flag:
    flag = 0
    for j in range(1,N):
        if A[N-j] < A[N-j-1]:
            dummy = A[N-j]
            A[N-j] = A[N-j-1]
            A[N-j-1] = dummy
            flag = 1
            count += 1
for i in range(N-1):
    print(A[i],end=" ")
print(f"{A[N-1]}\n{count}")

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

N = int(input())
A = list(map(int, input().split()))
kaisu = bubbleSort(A, N)
for i in range(N) :
    print(A[i], end="")
    if i != N-1 :
        print(" ", end="")
print()
print(kaisu)

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]:
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp
            flag = 1
            cnt += 1

print(*a)
print(cnt)

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
    print(' '.join(map(str,A)))
    print(cnt)


x =int(input())
y = list(map(int,input().split(' ')))
bubbleSort(y,x)

Solution (Python):
import math
import itertools

def main():
  n = int(input())
  A = list(map(int, input().split()))
  len_A = len(A)
  times = 0

  for i in range(len_A):
    for j in reversed(range(i+1, len_A)):
      if A[j] < A[j-1]:
        tmp = A[j]
        A[j] = A[j-1]
        A[j-1] = tmp
        times += 1

  print(*A)
  print(times)

if __name__ == '__main__':
  main()

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


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

bubbleSort(A, N)

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

bubble_sort(ar,num)

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

n = int(input())
a = [ int(i) for i in input().split()]
b , count = bubblesort(a,n)
for i in range(len(b)):
    if i == len(b)-1:
        print(b[i])
    else:
        print(b[i],end=' ')
print(count)

Solution (Python):
n = int(input())
As = input().split()
A = [int(i) for i in As]
f = True
c = 0

while f:
    f = False
    for i in range(n-1):
        if A[i+1] < A[i]:
            w = A[i+1]
            A[i+1] = A[i]
            A[i] = w
            c += 1
            f = True

for i in range(n-1):
    print("{} ".format(A[i]), end="")
print(A[n-1])
print(c)

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

flag = True
j = 1
cnt = 0

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

result = [str(i) for i in A]
print(' '.join(result))
print(cnt)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
flg = True
cnt = 0
while flg:
	flg = False
	for i in range(len(a) - 1, 0, -1):
		if a[i] < a[i - 1]:
			a[i - 1], a[i] = a[i], a[i - 1]
			cnt = cnt +1
			flg = True

print(' '.join([str(n) for n in a]))
print(cnt)

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

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


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

Solution (Python):
num = int(input())
sort_list = list(map(int,input().split()))

cnt = 0
for i in range(num):
    for j in reversed(range(i+1,num)):
        if sort_list[j] < sort_list[j-1]:
            sort_list[j],sort_list[j-1] = sort_list[j-1],sort_list[j]
            cnt += 1

print(" ".join([str(i) for i in sort_list]))
print(cnt)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
cnt=0
flg=1
while flg:
    flg=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]
            cnt+=1
            flg=1

[print(A[x],end=' ') for x in range(N-1)];print(A[N-1]);print(cnt)

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

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

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



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

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

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

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

Solution (Python):

import sys
from functools import reduce


def to_str(x, y):
    return str(x) + ' ' + str(y)


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

count = 0
flag = 1
while flag:
    flag = 0
    for j in range(n):
        if j <= n - 2 and A[j] > A[j + 1]:
            tmp = A[j + 1]
            A[j + 1] = A[j]
            A[j] = tmp
            count += 1
            flag = 1

print(reduce(to_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]):
            (a[j-1], a[j]) = (a[j], a[j-1])
            flag=True
            count += 1
print(' '.join(map(str,a)))
print(count)

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

swapcnt = 0

def bubbleSort(A):
  global N,swapcnt
  for i in range(N):
    for j in  range(N-1,i,-1):
      if A[j] < A[j-1]:
        x = A[j]
        A[j] = A[j-1]
        A[j-1] = x
        swapcnt += 1

bubbleSort(A)
strA = [str(a) for a in A]
print(" ".join(strA))
print(swapcnt)

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

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

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

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

print(*A)
print(count)

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

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

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

Solution (Python):
def bubbleSort(sort_list):
    flag = True
    count = 0
    while flag:
        flag = False

        for j in range(len(sort_list) - 1, 0, -1):
            if sort_list[j] < sort_list[j - 1]:
                tmp = sort_list[j]
                sort_list[j] = sort_list[j - 1]
                sort_list[j - 1] = tmp
                count += 1
                flag = True

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


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

    bubbleSort(sort_list)

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

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

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

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

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

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

trace()

print(count)

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

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]:
                temp = A[j]
                A[j] = A[j-1]
                A[j-1] = temp
                flag = 1
                cnt += 1
    return(A, cnt)

out_A, cnt_sort = bubbleSort(A,N)

out_A_str = list(map(str, out_A))
print(" ".join(out_A_str))
print(str(cnt_sort))

Solution (Python):


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


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


ans_list, ans_count = bubbleSort(a_list, N)
print(" ".join(list(map(str, ans_list))))
print(ans_count)

Solution (Python):


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

def bubbleSort(A):
	n = len(A)
	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

res, cnt = bubbleSort(A)
print(*res)
print(cnt)

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

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

        else:
            print(nums[i])

def bubble_sort(nums,n):
    count=0
    flag=1
    while flag:
        flag=0
        for j in range(n-1,0,-1):
            
            if nums[j-1]>nums[j]:
                flag=1
                nums[j-1],nums[j]=nums[j],nums[j-1]
                count+=1
            

    return count,nums

count,nums=bubble_sort(nums,n)
show(nums)
print(count)

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

counter=0
for E in range(0,N):
    for i in range(N-1,E,-1):
        if S[i]<S[i-1]:
            this_value=S[i]
            S[i]=S[i-1]
            S[i-1]=this_value
            counter+=1
        else:pass

print(*S)
print(counter)

Solution (Python):
N = int(input())
A = [int(a) for a in 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]
            cnt += 1
            flag = True

print(*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 range(n-1,0,-1):
        if a[j] < a[j-1]:
            cnt += 1
            a[j],a[j-1] = a[j-1],a[j]
            flag = 1
print(*a)
print(cnt)

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

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
flag=True
cnt=0
while flag:
    flag=False
    j=0
    for i in range(N-1, j, -1):
        if A[i-1]>A[i]:
            A[i-1],A[i]=A[i],A[i-1]
            flag=True
            cnt+=1
    j+=1
print(*A)
print(cnt)

Solution (Python):
def bubble_sort(data, n):
    end_of_line = 0
    change_num = 0

    while end_of_line < n-1:
        for i in reversed(range(end_of_line,n)):
            if data[i-1] > data[i]:
                v = data[i]
                data[i] = data[i-1]
                data[i-1] = v
                change_num += 1
                break
            if end_of_line >= i-1:
                end_of_line += 1
                break
    
    return data, change_num


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

sorted_data, chage_num = bubble_sort(data,num)

print(' '.join(map(str,sorted_data)))
print("%d"%chage_num)

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

if __name__=="__main__":
    n=int(input())
    l=[int(x) for x in input().split()]
    bubble(l,n)

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

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

Solution (Python):
def BubbleSort(A,N):
    count = 0
    flag = 1
    while flag == 1:
        flag = 0
        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
                flag = 1
    L=[str(ai) for ai in A]
    L=" ".join(L)
    print(L)
    return count
    
n = int(input())
a = list(map(int,input().split()))
count = BubbleSort(a,n)
print(count)

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

f = 1
c = 0
while f:
    f = 0
    for i in range(1, n)[::-1]:
        if A[i - 1] > A[i]:
            A[i - 1], A[i] = A[i], A[i - 1]
            c += 1
            f = True

print(*A)
print(c)

Solution (Python):
def bubblesort(a, n):
    n_exchange = 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]
                n_exchange += 1
                flag = True

    return a, n_exchange

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

a_sort, n_exchange = bubblesort(a, n)

print(' '.join([str(x) for x in a_sort]))
print(n_exchange)

Solution (Python):
def BubbleSort(l):
  countSwap = 0
  for i in range(len(l)-1):
    for j in reversed(range(i+1, len(l))):
      if l[j] < l[j-1]:
        temp = l[j]
        l[j] = l[j-1]
        l[j-1] = temp
        countSwap += 1
  print(" ".join(map(str, l)))
  print(countSwap)

N = input()
l = list(map(int, input().split()))
BubbleSort(l)

Solution (Python):
from sys import stdin
input = stdin.readline

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


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


flag = True
ans = 0

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

p(A)
print(ans)

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

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


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

Solution (Python):


n = int(input())
l = list(map(int, input().split()))
c = 0
flag = 1
while flag:
    flag = 0
    for i in reversed(range(1, n)):
        if l[i] < l[i - 1]:
            l[i], l[i - 1] = l[i - 1], l[i]
            flag = 1
            c += 1
print(' '.join(map(str, l)))
print(c)

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

swap=0

flag = 1
while flag:
    flag = 0
    for i in reversed(list(range(1,len(lst)))):
        if lst[i-1]>lst[i]:
            lst[i-1],lst[i] = lst[i],lst[i-1]
            flag = 1
            swap+=1

print(' '.join(map(str,lst)))
print(swap)

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

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

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

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

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

print(' '.join(map(str, l)))
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 reversed(range(i+1, n)):
        if a[j] < a[j-1]:
            a[j], a[j-1] = a[j-1], a[j]
            flag = 1
            count += 1
    i += 1
for k in range(len(a)):
    if k == n - 1:
        print(a[k])
    else:
        print(a[k], end=' ')
print(count)

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


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

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

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

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

n = int(input())
lst = list(map(int, input().split()))
cnt = bubble_sort(lst)
print(*lst)
print(cnt)

Solution (Python):


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


def bubbleSort(A, N):
    isExist = True
    cnt = 0
    while isExist:
        isExist = False
        for i in range(1, N):
            j = N - i
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                isExist = True
                cnt += 1
    print(' '.join([str(i) for i in A]))
    print(cnt)


bubbleSort(A, N)

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] 
            cnt +=1
            flag = 1
print(' '.join(map(str, A)))
print(cnt)

Solution (Python):
def bubblesort(A):
    N = len(A)
    cnt = 0
    for i in range(N):
        for j in range(N-1):
            if A[j] > A[j+1]:
                A[j],A[j+1] = A[j+1],A[j]
                cnt += 1
    print(*A)
    print(cnt)
            

def main():
    N = int(input())
    A = list(map(int,input().split()))
    bubblesort(A)

if __name__ == "__main__":
    main()

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

flag = 1
perm = 0
while flag == 1:
    flag = 0
    for j in range(N-1, 0, -1):
        if A[j] < A[j-1]:
            temp = A[j]
            temp2 = A[j-1]
            A[j-1] = temp
            A[j] = temp2
            perm += 1
            flag = 1
print(" ".join(list(map(str, A))))
print(perm)

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

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


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

cnt = bubbleSort(L,n)

for item in range(len(L)):
	if item == len(L)-1:
		print(L[item])
	else:
		print(L[item],end=" ")

print(cnt)

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

  return count

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

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

def b_s(li):
    num = 0
    for index in range(len(li)):
        for j in range(len(li) - 1, index, -1):
            if li[j] < li[j-1]:
                li[j], li[j-1] = li[j-1], li[j]
                num += 1
    return li, num

li, num = b_s(li)
print(*li)
print(num)

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


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


if __name__ == "__main__":
    main()

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

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
flag=1
counter=0
while flag:
    flag=0
    for j in range(1,n):
        if A[j] < A[j-1]:
            tmp=A[j]
            A[j]=A[j-1]
            A[j-1]=tmp
            flag=1
            counter+=1
for k in range(len(A)):
    if k != len(A)-1:
        print(A[k],end=' ')
    else:
        print(A[k])
print(counter)

Solution (Python):




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


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

Solution (Python):
def bubbleSort(A, N):
    cnt = 0
    flag = 1
    
    while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j] < A[j-1]:
                A = A[:j-1] + A[j-1:j+1][::-1] + A[j+1:]
                cnt += 1
                flag = 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()))
flag = 1
cnt = 0
while flag:
  flag = 0
  for i in range(len(a)):
    if i > 0 and a[i] < a[i-1]:
      a[i],a[i-1] = a[i-1],a[i]
      flag = 1
      cnt += 1
print(" ".join((str(i) for i in a)))
print(cnt)

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

Solution (Python):
N=int(input())
A=[int(x) for x in input().split()]
i=0
flag=True
count=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]
            count+=1
            flag=True


    i += 1
for i in range(N):print(A[i],end=' \n'[i+1==N])
print(count)

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


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

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

Solution (Python):

def bubble(a):
	ct=0
	for i in range(len(a)-1):
		for j in range(len(a)-1,i,-1):
			if a[j]<a[j-1]:
				t=a[j]
				a[j]=a[j-1]
				a[j-1]=t
				ct+=1
	return a,ct
n=int(input())
table=list(map(int,input().split()))
b,c=bubble(table)
print(*b)
print(c)

Solution (Python):
def main():
    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]:
                a[j],a[j-1] = a[j-1],a[j]
                c+=1
    print(' '.join(map(str,a)))
    print(c)


if __name__ == '__main__':
    main()

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

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

swap_count = 0

for i in range(len(num_list)-1):
    for k in range(len(num_list)-1,i,-1):
        if num_list[k] < num_list[k-1]:
            num_list[k],num_list[k-1] = num_list[k-1],num_list[k]
            swap_count += 1

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

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

Solution (Python):


import sys

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

if len(A) != N:
  print("[ERROR] Number of Input integers is invalid")
  sys.exit()

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

print(' '.join(map(str, A)))
print(swap_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] 
                cnt += 1
                flag = True
        i += 1
    return A, cnt

ans, c = bubbleSort(A[:], n)
print(' '.join(map(str, ans)))
print(c)

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

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

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

print(" ".join(map(str,table)))
print(swap_count)