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):


from math import sqrt, floor

def intinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def show(a):
    
    _str = ""
    for i in range(len(a) - 1):
        _str += str(a[i]) + " "
    _str += str(a[len(a) - 1])
    print(_str)

def bubble_sort(a):
    
    count = 0
    for i in range(1, len(a)):
        k = i - 1
        while k >= 0:
            
            if a[k] > a[k + 1]:
                a[k], a[k + 1] = a[k + 1], a[k]
                count += 1
            else: break
            k -= 1
    return count

def main():
    N = int(input())
    A = intinput()
    count = bubble_sort(A)
    show(A); print(count)

if __name__ == "__main__":
    main()

Solution (Python):

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

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

B = " ".join([str(num) for num in A])
print(B)
print(times)

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

n = int(input())
x = input().split()
y=[]
for i in range(n):
    y += [int(x[i])]
    
count = bubbleSort(y,n)
for j in range(n-1):
    print(y[j],end=' ')
print(y[n-1])
print(count)

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

n = int(input())
x = input().split()
y=[]
for i in range(n):
    y += [int(x[i])]
    
count = bubbleSort(y,n)
for j in range(n-1):
    print(y[j],end=' ')
print(y[n-1])
print(count)

Solution (Python):
def bubble_sort(a, n):
    flag = True
    counter = 0
    while flag:
        flag = False
        for j in range(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                counter += 1
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp
                flag = True
    return counter


def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x, end=' ')
    print(a[-1])


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

counter = bubble_sort(a, n)
print_list_split_whitespace(a)
print(counter)

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

flag = True
judge = True
count=0

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

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

Solution (Python):
N = int(input())
ary = [int(_) for _ in input().split()]


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

    print(' '.join([str(_) for _ in ary]))

    if verbose:
        print(count)


bubble_sort(ary)

Solution (Python):
N = int(input())
ary = [int(_) for _ in input().split()]


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

    print(' '.join([str(_) for _ in ary]))

    if verbose:
        print(count)


bubble_sort(ary)

Solution (Python):

A = []



LENGTH =int(input())
A = input().split()


N = 0
M = LENGTH - 1
CHANGE = 0



while N <= LENGTH -1 :
    M = LENGTH - 1


    while M >= N + 1:


        if int(A[M]) < int(A[M-1]) :
            tmp = A[M-1]
            A[M-1] = A[M]
            A[M] = tmp
            CHANGE += 1
        M -= 1
    N += 1

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

Solution (Python):



import sys

if __name__ == '__main__':
    N = int(raw_input())
    l = map(int, raw_input().split())
    cnt = 0
    for i in range(0, N):
        for j in range(N - 1, i, -1):
            if l[j] < l[j - 1]:
                l[j], l[j - 1] = l[j - 1], l[j]
                cnt += 1
    print ' '.join(map(str, l))
    print cnt

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

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

if __name__ == '__main__':
    main()

Solution (Python):
def print_list():
    for i,e in enumerate(A):
        if i == N - 1:
            print(e)
        else:
            print(e, end=" ")

N = int(input())
A = [int(e) for e in input().split()]
flag = 1; cnt = 0
while flag == 1:
    flag = 0
    for i in range(N-1, 0, -1):
        if A[i] < A[i-1]:
            w = A[i]; A[i] = A[i-1]; A[i-1] = w
            cnt += 1
            flag = 1
print_list()
print(cnt)

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

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

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

Solution (Python):
from sys import stdin
def stdinput():
    return stdin.readline().strip()

def main():
    n = int(stdinput())
    A = list(map(int, stdinput().split()))

    done = True
    count = 0
    while done:
        done = 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]
                done = True
                count += 1
    
    print(' '.join(map(str, A)))
    print(count)

if __name__ == '__main__':
    main()

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

Solution (Python):
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

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

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

print_arr(arr)
print(count)

Solution (Python):
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()
	
def bubble_sort(arr):
	cnt = 0
	n = len(arr)
	for i in range(n):
		for j in range(n - 1, i, -1):
			if arr[j - 1] > arr[j]:
				temp = arr[j - 1]
				arr[j - 1] = arr[j]
				arr[j] = temp
				cnt += 1
	return cnt	
	
n = int(input())
arr = list(map(int, input().split()))

cnt = bubble_sort(arr)
print_arr(arr)
print(cnt)

Solution (Python):
N = int(input())
arr = [int(n) for n in input().split()]

swap_cnt = 0
swap_flg = True
i = 0
while swap_flg:
    swap_flg = False
    for j in range(N - 1, i, -1):
        if (arr[j] < arr[j - 1]):
            arr[j], arr[j - 1] = arr[j - 1], arr[j]
            swap_cnt += 1
            swap_flg = True
    i += 1

print(' '.join(map(str, arr)))
print(swap_cnt)

Solution (Python):
times = int(input())
numbers = list(map(int, input().split()))
count = 0

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

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

Solution (Python):
def bubble_sort(l):
    flag = 1
    count = 0
    while flag == 1:
        flag = 0
        j = len(l) - 1
        while(0 < j):
            if l[j] < l[j-1]:
                swap = l[j]
                l[j] = l[j-1]
                l[j-1] = swap
                count = count + 1
                flag = 1
            j = j - 1
    return l,count


n = int(input())

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

if len(l) != n:
    print("Error")
    exit()

l,count = bubble_sort(l)

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

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

Solution (Python):
N = int(input())
arr = [int(n) for n in input().split()]

swap_cnt = 0
swap_flg = True
i = 0
while swap_flg:
    swap_flg = False
    for j in range(N - 1, i, -1):
        if (arr[j] < arr[j - 1]):
            arr[j], arr[j - 1] = arr[j - 1], arr[j]
            swap_cnt += 1
            swap_flg = True
    i += 1

print(' '.join(map(str, arr)))
print(swap_cnt)

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

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]
            flag = True
            ans += 1
for i in range(n):
    if i:
        print(" ", end = "")
    print(a[i], end = "")
print()
print(ans)

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

def run():
    n = int(input())
    a = list(map(int, input().split()))
    c, a = bubbleSort(a,n)
    print(' '.join([str(i) for i in a]))
    print(c)

if __name__ == '__main__':
    run()

Solution (Python):
N = int(input())
l = input().split()
l = [int(s) for s in l]
count = 0
for j in range(N):
    for i in range(N-1):
        if( l[i] > l[i+1]):
            l[i], l[i+1] = l[i+1], l[i]
            count += 1
for i in range(0,len(l)-1):
    print(l[i],end=' ')
print(l[N-1])
print(count)

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

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

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

if __name__ == '__main__':
    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(" ".join(map(str, A)))
    print(cnt)

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

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

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

if __name__ == '__main__':
    flg = True
    cnt = 0
    while flg:
        for i in range(N):
            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
    print(" ".join(map(str, A)))
    print(cnt)

Solution (Python):
N = int(input())
S = list(map(int, input().split(" ")))
Exchange_Number_of_times = 0
flag = True
while flag:
    flag = False
    for j in range(N - 1, 0, -1):
        f = j - 1
        if S[j] < S[f]:
            S[j], S[f] = S[f], S[j]
            flag = True
            Exchange_Number_of_times += 1
print(' '.join(map(str,S)))
print(Exchange_Number_of_times)

Solution (Python):
def bubbleSort(A, N):
    flag = True
    swap = 0

    while flag:
        flag = False

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

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

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    bubbleSort(A, N)

main()

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

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

resultArray, cnt = bubbleSort(array)

print(*resultArray)
print(cnt)

Solution (Python):
A=int(input())
B=[int(i) for i in input().split()]
flag=True
s=0
while flag:
    flag=False
    for j in range(1,A):
        if B[j]<B[j-1]:
            B[j],B[j-1]=B[j-1],B[j]
            flag=True
            s+=1
for i in range(A):
    if i!=A-1:
        print(B[i],end=" ")
    else:
        print(B[i])
print(s)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
flag = 1
p = 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
            p += 1
            flag = 1
for i in range(N) :
    if i == N-1 :
        print(A[i])
    else :
        print(A[i],end=" ")
print(p)

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

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

Solution (Python):
n = int(input())
nlist = list(map(int, input().split()))
c= 0
for i in range(n):
    for k in range(i+1,n):
        j = n - 1 -k + i + 1
        if nlist[j] < nlist[j-1]:
            m = nlist[j]
            nlist[j] = nlist[j-1]
            nlist[j-1] = m
            c += 1
for i in range(n):
    if (i == n-1):
        print('{}'.format(nlist[i]), end = '')
    else : 
        print('{}'.format(nlist[i]), end = ' ')
print()
print(c)

Solution (Python):
n = int(input())
nlist = list(map(int, input().split()))
def BubbleSort(C, n):
    c = 0
    for i in range(n-1):
        for w in range(i+1, n):
            j = n-w+i
            if (C[j] < C[j-1]):
                a = C[j]
                C[j] = C[j-1]
                C[j-1] = a
                c += 1
    C.append(c)
BubbleSort(nlist, n)
for i in range(n):
    if (i == n-1):
        print('{}'.format(nlist[i]), end = '')
    else : 
        print('{}'.format(nlist[i]), end = ' ')
print()
print(nlist[-1])

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

c = 0
flag = 1
while flag:
    flag = 0
    for j in range(1,N):
        if a[j] < a[j-1]:
            v = a[j]
            a[j] = a[j-1]
            a[j-1] = v
            c +=1
            flag =1
print(" ".join([str(n) for n in a]))
print(c)

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

c = 0
flag = 1
while flag:
    flag = 0
    for j in range(1,N):
        if a[j] < a[j-1]:
            v = a[j]
            a[j] = a[j-1]
            a[j-1] = v
            c +=1
            flag =1
print(" ".join([str(n) for n in a]))
print(c)

Solution (Python):


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

def bubbleSort(A, N):
    """N 個の要素を含む 0-オリジンの配列 A"""
    nswap = 0
    flag = True 
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                swap(A, j, j-1) 
                nswap += 1
                flag = True
    return nswap

N = int(input())
A = [int(t) for t in input().split()]
if len(A) != N: raise

nswap = bubbleSort(A, N)
print(' '.join(map(str, A)))
print(nswap)

Solution (Python):
input()
arr = list(map(int, input().split()))
flag = False
count = 0
while not flag:
    flag = True
    for i in range(len(arr)-1):
        if arr[i] > arr[i+1]:
            count += 1
            flag = False
            t = arr[i+1]
            arr[i+1] = arr[i]
            arr[i] = t  
print(" ".join(map(str, arr)))
print(count)

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

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

Solution (Python):

from __future__ import division, print_function
from sys import stdin
stdin.readline()
data = [int(s) for s in stdin.readline().split()]
times = 0
for i in range(len(data)):
    for j in range(len(data)-1, i, -1):
        if data[j] < data[j-1]:
            data[j], data[j-1] = data[j-1], data[j]
            times += 1
print(*data)
print(times)

Solution (Python):

i = input()
n = map(int, raw_input().split())
cnt=0

for i in xrange(len(n)):
    for j in xrange(len(n)-1, i, -1):
        if n[j] < n[j-1]:
            n[j], n[j-1] = n[j-1], n[j]
            cnt+=1
print ' '.join(map(str,n))
print cnt

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

Solution (Python):

i=int(raw_input())
n=map(int, raw_input().split())
ans=0
chk=10




for m in range(len(n)):
    for j in range(len(n)-1, m, -1):
        if n[j]<n[j-1]:
            n[j],n[j-1]=n[j-1],n[j]
            ans+=1

    if ans==chk: break
    chk=ans
print ' '.join(map(str,n))
print ans

Solution (Python):


i=int(raw_input())
n=map(int, raw_input().split())
ans=0
chk=10





for m in range(len(n)):
    for j in range(len(n)-1, m, -1):
        if n[j]<n[j-1]:
            n[j],n[j-1]=n[j-1],n[j]
            ans+=1

    if ans==chk: break
    chk=ans
print ' '.join(map(str,n))
print ans

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

count = 0
for i in range(0,n):
    for j in range(n-1,-1+i,-1):
        if elem[j] < elem[j-1] and j>0:
            count += 1
            tmp = elem[j-1]
            elem[j-1] = elem[j]
            elem[j] = tmp
print " ".join(map(str,elem))
print count

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

Solution (Python):
def bubble(n, a):
    count = 0
    for i in range(n-1):
        for j in range(n-1,i,-1):
            if a[j-1] > a[j]:
                tmp = a[j]
                a[j] = a[j-1]
                a[j-1] = tmp
                count += 1
    return count, a
    
n = int(raw_input())
a = map(int, raw_input().split())

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

Solution (Python):
if __name__ == "__main__":
    import sys
    N = int(sys.stdin.readline().strip())
    A = [int(e) for e in sys.stdin.readline().strip().split()]
    cnt = 0
    for i in range(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
    print " ".join(map(str, A))
    print cnt

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

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

def bubblesort(A, number):
    flag = True
    count, change = 0, 0
    while flag:
        flag = False
        for i in range(1, number-count):
            if A[-i-1] > A[-i]:
                A[-i-1], A[-i] = A[-i], A[-i-1]
                flag = True
                change += 1
        count += 1
    return print(*A), print(change)

bubblesort(A, N)

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

Solution (Python):

number=eval(input())
swap=0
arr=list(map(int, input().split(' ')))
for i in range(0,number-1):
    j=number-1
    while j>i:
        if arr[j]<arr[j-1]:
            arr[j],arr[j-1]=arr[j-1],arr[j]
            swap+=1
        j-=1

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

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

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

for k in range(n-1):
    print(str(A[k])+" ",end = "")
print(str(A[n-1]))
print(count)

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

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

    return A, count

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

    answer_list, answer_count = bubbleSort(A, N)

    print(*answer_list)
    print(answer_count)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
count = 0
for j in range(n):
    for i in range(n-1):
        if l[i]>l[i+1]:
            t = l[i]
            r = l[i+1]
            l[i] = r
            l[i+1] = t
            count += 1
for s in range(n-1):
        print(l[s],end=' ')
print(l[-1])
print(count)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
	for j in range(i):
		if A[j] > A[i]:
			ans += 1
A.sort()
s = str(A[0])
for i in range(1, N):
	s += " " + str(A[i])
print(s)
print(ans)

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

def bubbleSort(a:list, n:int):
    count = 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 
                count += 1
    return count, a

count, a = bubbleSort(a, n)
print(" ".join(map(str, a)))
print(count)

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

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

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

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

print(*A)
print(cnt)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
cnt = 0

flag = 1
while flag:
    flag = 0
    for i in range(N - 1, 0, -1):
        if A[i] < A[i - 1]:
            a = A[i]
            b = A[i - 1]
            A[i] = b
            A[i - 1] = a
            flag = 1
            cnt += 1
print(' '.join(map(str, A)))
print(cnt)

Solution (Python):


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

nums = input()


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

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

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

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

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

Solution (Python):
def bubble_sort(a, n):
    flag = 1
    switch = 0
    while flag:
        flag = 0
        for j in range(n-1, 0, -1):
            if a[j] < a[j-1]:
                temp = a[j]
                a[j] = a[j-1]
                a[j-1] = temp
                flag = 1
                switch += 1
    print(' '.join([str(i) for i in a]))
    print(switch)


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


if __name__ == "__main__":
    main()

Solution (Python):
def bubbleSort(A):
    flag = 1
    count = 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
                count += 1

    return [count,A]

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

answer = bubbleSort(lst)

print(" ".join(map(str,answer[1])))
print(answer[0])

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

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

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

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

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

print(' '.join(str(x) for x in L))
print(cnt)

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

cnt = 0




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

print(" ".join([str(a) for a in A]))
print(cnt)

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

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

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

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

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

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

flag = 1
sorted_index = 0
num_of_sorting = 0
while flag:
  flag = 0
  for i in range(N-1, sorted_index, -1):  
    if A[i] < A[i-1]:
      temp = A[i]
      A[i] = A[i-1]
      A[i-1] = temp
      flag = 1
      num_of_sorting += 1
  sorted_index += 1

print(' '.join(map(str, A)))
print(f"{num_of_sorting}")

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

frag = True
count = 0
while frag:
    frag = False
    for i in reversed(range(1, n)):
        if nums[i] < nums[i-1]:
            nums[i], nums[i-1] = nums[i-1], nums[i]
            count += 1
            frag = True
print(' '.join(map(str, nums)))
print(count)

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


def bubblesort(A, n):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(n-1):
            if A[i] > A[i+1]:
                A[i], A[i+1] = A[i+1], A[i]
                flag = 1
                count += 1
    show(A, n)
    print(count)


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


if __name__ == "__main__":
    main()

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

flag = True   
i = 0         
count = 0     

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

print(str(A).replace('[', '').replace(']', '').replace(',', ''))
print(count)

Solution (Python):
import sys

def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('')
        
def main():
    N = int(input())
    data = list(map(int, input().split()))
    counter = 0
    
    for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j] < data[j-1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
                counter += 1
            j -= 1
        
    print_data(data)
    print(counter)
    
            
if __name__ == '__main__':
    main()

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

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

print(*lst)
print(count)