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()]
c = 0
flag = 1
while flag:
    flag = 0
    for j in range(N-1, 0, -1):
        if A[j] < A[j-1]:
            A[j], A[j-1] = A[j-1], A[j]
            flag = 1
            c += 1
print(" ".join(map(str, A)))
print(c)

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]:
            x = a[j]
            a[j] = a[j-1]
            a[j-1] = x
            flag = True
            count = count + 1

print(*a)
print(count)

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

Solution (Python):
def print_list(a):
    out = ''
    for ai in a:
        out += str(ai) + ' '
    else:
        out = out[:-1]
    print(out)

def bubble_sort(a, n):
    flag = True
    cnt = 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
                cnt += 1
                flag = True
    
    print_list(a)
    print(str(cnt))
    return a



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

a = bubble_sort(a, n)

Solution (Python):
def BubbleSort(A, N):
    cnt = 0  
    t = 0
    for i in range(N):   
        t = cnt
        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        
        if t == cnt:
            return A, cnt

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

ans, cnt = BubbleSort(A,N)
print(*ans)    
print(cnt)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
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
a=map(str,A)
print(" ".join(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 reversed(range(1,N)):
        if A[i] < A[i-1]:
            A[i-1],A[i] = A[i],A[i-1]
            flag = True
            count += 1
print(*A)
print(count)

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

print(*working_list)
print(exchange_count)

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

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

bubbleSort(ls,len(ls))

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

def BubbleSort(A, N):
    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)

main()

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

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

Solution (Python):
input()
A = [int(i) for i in input().split()]
flag = 1
n = 0
while flag:
    flag = 0
    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]
            flag = 1
            n += 1
print(" ".join(map(str, A)))
print(n)

Solution (Python):


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

flag = 1
count = 0
while(flag):
    flag = 0
    for j in reversed(range(1, length)):
        if list[j] < list[j-1]:
            list[j], list[j-1] = list[j-1], list[j]
            flag = 1
            count += 1

result = ''
for l in list:
    result += str(l) + ' '

print(result[:-1])
print(count)

Solution (Python):
def bubble_sort(A, N):
    flag = True
    swap_count = 0
    while flag:
        flag = False
        for j in range(1, N)[::-1]:
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                swap_count += 1
                flag = True
    return A, swap_count
N = int(input())
A = list(map(int, input().split(' ')))
sorted_A, swap_count = bubble_sort(A, N)
print(" ".join(map(str, sorted_A)))
print(swap_count)

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

def bubblesort(A,n):
    count=0
    flag=1
    while flag:
        flag=0
        for j in reversed(range(1,len(A))):
            if A[j]<A[j-1]:
                tmp=A[j-1]
                A[j-1]=A[j]
                A[j]=tmp
                flag=1
                count+=1
    return(A,count)

result,count=bubblesort(A,n)
print(" ".join([str(i) for i in result]))
print(count)

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

Solution (Python):
def show (a,n):
  for i in range(n):
    if i > 0:
      print("",end=" ")
    print(a[i],end="")
  print("")

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-1],a[j] = a[j],a[j-1]
        flag = 1
        count += 1
  show(a,n)
  print(count)

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

bubbleSort(a,n)

Solution (Python):
def BubbleSort(A, N):
    if N == 1:
        print(str(A[0]) + "\n" + str(0))
    else:
        count = 0
        for i in range(N): 
            for j in range(N - i - 1):
                if A[N - j - 1] < A[N - j - 2]: 
                    count += 1
                    A[N - j - 1], A[N - j - 2] = A[N - j - 2], A[N - j - 1] 
        str_A = map(str, A)
        print(" ".join(str_A) +  "\n" + str(count))

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

BubbleSort(a, n)

Solution (Python):






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

def test(a):
    return a

def outputList(A):
    if(len(A) == 1):
        print(A[0])
    else:
        for i in range(len(A)-1):
            print(f"{A[i]} ",end="")
        print(A[-1])
        

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

outputList(ans)
print(count)

Solution (Python):

def ALDS1_2_A():
    """
    1 bubbleSort(A, N) // N 個の要素を含む 0-オリジンの配列 A
    2   flag = 1 // 逆の隣接要素が存在する
    3   while flag
    4     flag = 0
    5     for j が N-1 から 1 まで
    6       if A[j] < A[j-1]
    7         A[j] と A[j-1] を交換
    8         flag = 1

    ＜問題＞
    数列 A を読み込み、バブルソートで昇順に並び変え出力するプログラムを作成してください。
    また、バブルソートで行われた要素の交換回数も報告してください。

    ＜入力＞
    入力の最初の行に、数列の長さを表す整数 N が与えられます。２行目に、N 個の整数が空白区切りで与えられます。

    ＜出力＞
    出力は 2 行からなります。１行目に整列された数列を 1 行に出力してください。数列の連続する要素は１つの空白で区切って出力してください。
    2 行目に交換回数を出力してください。

    ＜制約＞
    1 ≤ N ≤ 100
    0 ≤ A の要素 ≤ 100
    """
    N = int(input())
    A = [int(e) for e in input().split(" ")]
    flag = 1    
    switch_cnt = 0
    idx_not_sorted = 0
    while flag:
        flag = 0
        for j in range(N-1, idx_not_sorted, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                switch_cnt += 1
                flag = 1
        idx_not_sorted += 1
    print(" ".join(map(str, A)))
    print(switch_cnt)


def main():
    ALDS1_2_A()


if __name__ == '__main__':
    main()

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

Solution (Python):
def bubble_sort(a, n):

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

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


def main():
    n = int(input())
    a = list(map(int, input().split(' ')))
    bubble_sort(a, n)


if __name__ == "__main__":
    main()

Solution (Python):
N, *A = map(int, open(0).read().split())
F = True
ans = 0
while F:
    F = False
    for i in range(N-1):
        if A[i] > A[i+1]:
            A[i], A[i+1] = A[i+1], A[i]
            F = True
            ans += 1
print(' '.join(map(str, A)))
print(ans)

Solution (Python):



def bubbleSort(A, N):
    flag = 1
    cnt = 0;
    while flag:
        flag = 0;
        for i in reversed(range(1, N)):
            if A[i-1] > A[i]:
                A[i-1], A[i] = A[i] , A[i-1]
                flag = 1;
                cnt += 1
    showResult(A, cnt)

def showResult(A, cnt):
    N = len(A)
    for i in range(N-1):
        print(A[i], end = " ")
    print(A[N - 1 ])
    print(cnt)

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



bubbleSort(A, N)

Solution (Python):
flag = 0

N = int(input())

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

for i in range(N-1):
  for j in range(N-1):
    if l[N-1-j] < l[N-2-j]:
      a = l[N-1-j]
      b = l[N-2-j]
      l[N-2-j] = a
      l[N-1-j] = b
      flag += 1

print(" ".join(map(str, l)))
print(flag)

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


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

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

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

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

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

    return (a,cnt)

_ = input()
l = [ int(s) for s in input().split() ]
(nl,swap) =  bubble_sort(l)

print(*nl)
print(swap)

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

flag=1
count=0

while flag==1:
  flag=0
  for i in range(n-1,0,-1):
    if A[i-1] > A[i]:
      v =A[i]
      A[i] = A[i-1]
      A[i-1] =v
      flag=1
      count+=1
print(*A, sep=' ')
print(count)

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

class BitSum:
    def __init__(self, n):
        self.n = n + 3
        self.table = [0] * (self.n + 1)

    def update(self, i, x):
        i += 1
        while i <= self.n:
            self.table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    bit=BitSum(105)
    swap_n=0
    for i,a in enumerate(aa):
        swap_n+=i-bit.sum(a)
        bit.update(a,1)
    aa.sort()
    print(*aa)
    print(swap_n)

main()

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

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

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

Solution (Python):
def bubbleSort(l):
    k = 1
    count = 0
    for j in range(len(l)-1):
        for i in range(len(l)-k):
            if l[i] > l[i+1]:
                a = l[i]
                l[i] = l[i+1]
                l[i+1] = a
                count += 1
        k += 1

    ans_l = map(str,l)
    return ans_l,count

n = int(input())
l = list(map(int,input().split()))
ans_l,ans_count = bubbleSort(l)
print(' '.join(ans_l))
print(ans_count)

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

flag = True
cnt=0

for j in range(n):
  flag = True
  while flag:
    flag = False
    i = n 
    
    while i > 1:
      i -= 1
      if(ary[i-1] > ary[i]):
        ary[i], ary[i-1] = ary[i-1], ary[i]
        cnt += 1
print(' '.join(map(str,ary)))
print(cnt)

Solution (Python):
def bubble_sort(arr, n):
    count = 0
    for j in range(n - 1):
        for i in reversed(range(j, n - 1)):
            if arr[i] > arr[i + 1]:
                arr[i], arr[i + 1] = arr[i + 1], arr[i]
                count += 1
    return arr, count


def main():
    n = int(input())
    arr = list(map(int, input().split()))
    
    
    sorted_arr, count = bubble_sort(arr, n)
    print(" ".join(map(str, sorted_arr)))
    print(count)
    


if __name__ == "__main__":
    main()

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

flg = True
cnt = 0
i = 0
while flg:
    flg = False
    
    for j in range(N-1,i,-1):
        if A[j] < A[j-1]:
            A[j],A[j-1] = A[j-1],A[j]
            cnt += 1
            flg = True
    
    i += 1
print(*A)
print(cnt)

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

bubble_sort(A)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
def flag(A):
    j = 0
    for i in range(len(A)-1):
        if A[i] > A[i+1]:
            j = 1
    return j != 0 
n = 0
while flag(A):
    for i in range(N-1):
        if A[i] > A[i+1]:
            A_i = A[i]
            A[i] = A[i+1]
            A[i+1] = A_i
            n += 1
print(*A)
print(n)

Solution (Python):
def bubbleSort(numlist):
    n = len(numlist)
    swaptime = 0
    for i in range(n):
        for j in range(n - 1, i, -1):
            if numlist[j] < numlist[j - 1]:
                swaptime += 1
                numlist[j], numlist[j - 1] = numlist[j - 1], numlist[j]
    return swaptime

def print_numlist(numlist):
    if len(numlist) == 1:
        print(numlist[0])
    else:
        for i in range(0, len(numlist) - 1):
            print(numlist[i], end = ' ')
        print(numlist[-1])

if __name__ == '__main__':
    n = int(input())
    numlist = [int(i) for i in input().replace('\n', '').split(' ')]
    swapnum =  bubbleSort(numlist)

    print_numlist(numlist)
    print(swapnum)

Solution (Python):
n=int(input())
l=list(map(int, input().split()))
l_res=sorted(l)
cnt=0
for i in reversed(range(n)):
    for j in range(i):
        if l[j]>l[j+1]:
            l[j],l[j+1]=l[j+1],l[j]
            cnt+=1
print(*l_res)
print(cnt)

Solution (Python):
1
2
3
4
5
6
7
8

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

print(*nums)
print(count)

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

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

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

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

while flag:
    flag = False
    i = 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]
            counter += 1
            flag = True
    i += 1
A = [str(i) for i in A]
print(' '.join(A))
print(counter)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
change = 0
for i in range(n):
	u = n-1
	while u > i:
		if a[u-1] > a[u]:
			a[u-1], a[u] = a[u], a[u-1]
			change += 1
		u -= 1
print(*a)
print(change)

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

flag = 1
counter = 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
      counter += 1
print(*A, sep=" ")
print(counter)

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

flag = 1
while flag:
    flag = 0
    for j in range(1, N)[::-1]:
        if A[j] < A[j-1]:
            v = A[j]
            A[j] = A[j-1]
            A[j-1] = v
            flag = 1
            sum += 1
print(*A)
print(sum)

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-1]>A[j]:
            A[j-1],A[j]=A[j],A[j-1]
            flag=1
            count+=1


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

Solution (Python):
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


N = int(input())

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

swap_count = 0

for i in range(len(table)-1):
    for k in range(len(table)-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("%d"%(swap_count))

Solution (Python):
def main():
    n = int(raw_input().strip())
    data = map(int, raw_input().split())
    change = bubble_sort(n, data)
    print " ".join([str(x) for x in data])
    print change

def bubble_sort(n, data):
    change = 0
    for i in xrange(n):
        for j in xrange(n - 1, i, -1):
            if data[j] < data[j - 1]:
                change += 1
                data[j], data[j - 1] = data[j - 1], data[j]
    return change

if __name__ == '__main__':
    main()

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


n = int(input())
ls = list(map(int, input().split()))
num = bubbleSort(ls, n)
for i in range(len(ls)):
    if i:
        print(" ", end="")
    print(ls[i], end="")
print()
print(num)

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

for i in range(N-1):
  flag = False
  for j in range(N-2, i-1, -1):
    if A[j] > A[j+1]:
      t = A[j]
      A[j] = A[j+1]
      A[j+1] = t
      flag = True
      r += 1

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

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 reversed(range(1,n)):
      if a[j] < a[j-1]:
        temp = a[j]
        a[j] = a[j-1]
        a[j-1] = temp
        flag = 1
        count += 1
  return count

count = bubbleSort(a,n)
print(*a)
print(count)

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

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

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

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

for i in range(len(a)):
    for j in reversed(range(1,n)):
        if a[j]<a[j-1]:
            a[j-1],a[j]=a[j],a[j-1]
            s+=1
for i in range(n):
    if i==0:
        print(a[i],end="")
    else:
        print("{: }".format(a[i]),end="")
print()
print(s)

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

def bubble_sort(a):
    global cnt
    for i in range(len(a)):
        l = 0
        r = 1

        while r < len(a):
            if a[l] > a[r]:
                a[l], a[r] = a[r], a[l]
                cnt += 1
            l += 1
            r += 1


    
    
    
    

bubble_sort(a)
print(*a)
print(cnt)

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

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

def bubbleSort(N):
    Flag=True
    count=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
                count+=1
    print(*A)
    print(count)
bubbleSort(N)

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

Solution (Python):
def func(n, A):
    flag = True
    count = 0
    i = 0
    while (flag):
        flag = False
        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 = True
                count += 1
        i += 1
    print (" ".join(map(str, A)))
    print (count)

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

Solution (Python):

def print_output(_list):
    for index in range(len(_list)):
        if index == len(_list) - 1:
            print(_list[index])
        else:
            print(_list[index],end=' ')

target_length = int(input())
target_list = list(map(int,input().split()))

bubble_count = 0
for index in range(1, target_length):
    for compare_index in reversed(range(index)):
        if target_list[compare_index + 1] < target_list[compare_index]:
            target_list[compare_index + 1] ,target_list[compare_index] = target_list[compare_index] ,target_list[compare_index + 1]
            bubble_count += 1
        else:            
            break
print_output(target_list)
print(bubble_count)

Solution (Python):
import sys
from sys import stdin

cnt = 0

def BubbleSort(L):
    global cnt
    for i in range(len(L)):
        for j in range(len(L)-1, i, -1):
            if L[j-1] > L[j]:
                L[j-1], L[j] = L[j], L[j-1]
                cnt += 1
    
n = int(input())
NUM_LIST = list(map(int, input().split()))

BubbleSort(NUM_LIST)
STR = list(map(str, NUM_LIST))
print(" ".join(STR))
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
	print(*A)
	print(count)


if __name__ == "__main__":
	n = int(input())
	a = list(map(int, input().split()))
	bubblesort(a, n)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(len(A)-1):
    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
            cnt += 1
print(" ".join(map(str, A)))
print(cnt)

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

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

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

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

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

Solution (Python):

def babble_sort(A, N):
    cnt = 0
    flag = 1
    while flag:
      flag = 0
      i = 0
      for j in reversed(range(i + 1, N)):
        if A[j] < A[j-1]:
          c = A[j]
          A[j] = A[j -1]
          A[j-1] = c
          cnt += 1
          flag = 1
      i += 1
    for b in range(N):
      print(A[b], end=" \n"[b + 1 == N])
    print(cnt)


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

babble_sort(A, N)

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


def bubbleSort(li: list, n: int):
    count = 0
    for i in range(n-1):
        for j in reversed(range(i+1, n)):
            if li[j-1] > li[j]:
                li[j-1], li[j] = li[j], li[j-1]
                count += 1
    print(*li)
    print(count)


bubbleSort(A, N)

Solution (Python):
def bubble_sort(nums, n):
    count = 0
    flag = True
    while flag:
        flag = False
        for i in reversed(range(1, n)):
            if nums[i] < nums[i-1]:
                tmp = nums[i]
                nums[i] = nums[i-1]
                nums[i-1] = tmp
                count += 1
                flag = True

    nums_str = map(str, nums)
    print(" ".join(nums_str))
    print(count)

if __name__=="__main__":
    n = int(input())
    nums = list(map(int, input().split()))
    bubble_sort(nums, n)

Solution (Python):
n,*a = map(int,open(0).read().split())

ans = 0 

def BublbleSort(a,m):
    global ans
    flag = 1
    while flag:
        flag = 0
        for j in range(m-1,0,-1):
            if a[j] < a[j-1]:
                a[j],a[j-1]=a[j-1],a[j]
                ans += 1
                flag = 1
BublbleSort(a,n)
  
            
print(*a)

print(ans)

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

def BubbleSort(seq):
    count = 0
    for i in range(N):
        for j in range(1, N):
            if seq[j] < seq[j-1]:
                swp = seq[j]
                seq[j], seq[j-1] = seq[j-1], swp
                count += 1

    print(" ".join([str(x) for x in seq]))
    print(count)

BubbleSort(seq)

Solution (Python):
N = int(input())
A = [int(x) for x 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]:
            A[j], A[j-1] = A[j-1], A[j]
            count += 1
            flag = 1

for _ in range(N):
    if _ == N-1:
        print(A[_], sep=' ',end='')
    else:
        print(A[_], sep='',end=' ')
print()
print(count)

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


def main():
    N = int(input())
    P = list(map(int, input().split()))
    bubbleSort(N, P)


def bubbleSort(N, P):
    change_count = 0
    i = 0
    while True:
        for j in range(N - 1, i, -1):
            if P[j] < P[j - 1]:
                tmp = P[j]
                P[j] = P[j - 1]
                P[j - 1] = tmp
                change_count += 1
        i += 1
        
        if i == N:
            break

    print(' '.join(map(str, P)))
    print(change_count)


if __name__ == '__main__':
    main()

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

ans = 0

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

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

Solution (Python):
nums_count = int(input())
nums = list(map(int, input().split()))
swap_count = 0

for i in range(nums_count):
    for j in range(nums_count-1, i, -1):
        if nums[j-1] > nums[j]:
            nums[j-1], nums[j] = nums[j], nums[j-1]
            swap_count += 1

for num in nums[:-1]:
    print(num, end=' ')
print(nums[-1])

print(swap_count)

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

Solution (Python):
def pintarr(arr):
    arr = [str(c) for c in arr]
    print(' '.join(arr))


def bub_sort(it, key):
    count = 0
    for start_ind in range(len(it)):
        for target_ind in range(len(it)-1, start_ind, -1):
            if it[target_ind] < it[target_ind-1]:
                it[target_ind], it[target_ind-1] = it[target_ind-1], it[target_ind],
                count += 1
    pintarr(it)
    print(count)


if __name__ == '__main__':

    t = int(input())
    a = input()
    a = [int(c) for c in a.split(' ')]
    bub_sort(a, t)