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

import sys
input=sys.stdin.readline

def main():
    numbers=[]
    number = int(input())
    numbers=list(map(int,input().split()))
    sort_finished=0
    swap_count=0
    for idx in range(number-1):
        for sort_check_idx in reversed(range(number)[sort_finished+1:]):
            if numbers[sort_check_idx] < numbers[sort_check_idx-1]:
                numbers[sort_check_idx], numbers[sort_check_idx-1] = numbers[sort_check_idx-1], numbers[sort_check_idx]
                swap_count+=1
        sort_finished += 1
    print(*numbers)
    print(swap_count)

if __name__=="__main__":
    main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
cnt = 0
for i in range(n-1):
    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)

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

n = int(input())
A = list(map(int, input().split()))
c = bubbleSort(A, n)
for i in range(n):
    if i == n - 1:
        print(A[i])
    else:
        print(A[i], end=' ')
print(c)

Solution (Python):
from sys import stdin
N = int(input())
nums = list(map(lambda x: int(x), stdin.readline().split(' ')))

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

output = ""
for i in nums:
    output += str(i) + ' '
print(output[:-1])
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):
length = int(input())
num_list = list(map(int, input().split()))

change_count = 0

for i in range(length):
    flag = 0
    for k in range(length-1,i,-1):
        if num_list[k-1] > num_list[k]:
            num_list[k-1], num_list[k] = num_list[k], num_list[k-1]
            change_count += 1
            flag = 1
    if flag == 0:
        break

print(" ".join(map(str,num_list)))
print(change_count)

Solution (Python):

def bubbleSort(A: list, N: int):
    cnt = 0
    isEnd = False
    while not isEnd:
        isEnd = True
        for i in range(1, N):
            if A[i] < A[i-1]:
                tmp = A[i]
                A[i] = A[i-1]
                A[i-1] = tmp
                isEnd = False
                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):
def bubble_sort(A, N):
  sw = 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]
        flag = True
        sw += 1
    i+= 1
  print(*A)
  print(sw)

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

Solution (Python):
n = int(input())
t = list(map(int, input().split(' ')))
x, y = 0, 0
while True:
    x = 0
    for i in range(n-1):
        if t[i] > t[i+1]:
            t[i],t[i+1] = t[i+1],t[i]
            y = y + 1
        else:
            x = x + 1
    if x == n-1:
        break
print(' '.join(map(str,t)))
print(y)

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

Solution (Python):
try:
    N = int(input())
    A = list(map(int,input().split()))
except ValueError:
    print('数値以外が入力されました')

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

print(*A)
print(str(times_swap))

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

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

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

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

flag = True
n = len(A)
counter = 0
while flag:
    flag = False
    for j in range(n-1, 0, -1):
        if A[j] < A[j-1]:
            tmp = A[j]
            A[j] = A[j-1]
            A[j-1] = tmp
            flag = True
            counter += 1

print(" ".join(str(x) for x in A))
print(counter)

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

while flag:
    flag = 0
    for i in reversed(range(n-1)):
        if A[i] > A[i+1]:
            a = A[i]
            A[i] = A[i+1]
            A[i+1] = a
            flag = 1
            count += 1
print(' '.join(map(str, A)))
print(count)

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    cnt = bubble_sort(A, N)
    print(*A)
    print(cnt)


if __name__ == '__main__':
    main()

Solution (Python):












def bubbleSort(A):
    flag = 1
    counter = 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
                counter += 1
    return A, counter
    
N = int(input())
A = list(map(int, input().split()))

ans = bubbleSort(A)
print(' '.join(str(num) for num in ans[0]))
print(ans[1])

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

Solution (Python):
def bubble_sort(A:list) -> list:
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in range(len(A)-1, 0, -1):
            if A[i] < A[i-1]:
                A[i], A[i-1] = A[i-1], A[i]
                count += 1
                flag = 1
    print(" ".join(list(map(str, A))))
    return count

if __name__ == '__main__':
    n = int(input())
    A = list(map(int, input().rstrip().split()))
    print(bubble_sort(A))

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]
                count += 1
                flag = 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):
def bubble_sort(array):
    i_n_srt = 0
    counter = 0
    flg = True
    while flg:
        flg = False
        for j in range(len(array) - 1, i_n_srt, -1):
            
            if array[j] < array[j - 1]:
                array[j], array[j - 1] = array[j - 1], array[j]
                
                counter += 1
                flg = True
        i_n_srt += 1
    return counter, array        

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

ans, array_new = bubble_sort(A)
print(" ".join(map(str, array_new)))
print(ans)

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

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

for i in range(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]
            count += 1
print(" ".join(map(str, l)))
print(count)

Solution (Python):
import math

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

c=0
for i in range(len(A)):
	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]
      	      	c+=1
print ' '.join(map(str, A))
print c

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

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

    return cnt

res = bubbleSort(a, n)

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

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

counter = 0
for i in range(1,N):
    for j in reversed(range(i, N)):
        if A[j] < A[j - 1]:
            temp = A[j]
            A[j] = A[j - 1]
            A[j - 1] = temp
            counter = counter + 1

print(' '.join([str(a) for a in A]))
print(counter)

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


def bubble_sort(A):
    flag = 1  
    counter = 0
    while flag:
        flag = 0
        for i in range(1, len(A))[::-1]:
            if A[i] < A[i - 1]:
                A[i], A[i - 1] = A[i - 1], A[i]
                flag = 1
                counter += 1
    print(" ".join([str(a) for a in A]))
    print(counter)


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


if __name__ == "__main__":
    main()

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

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

print(*aa)
print(swaps)

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

reversals = 0
need_to_sort = True
while need_to_sort:
    need_to_sort = False
    for index in range(len(arr) -1, 0, -1):
        if arr[index] < arr[index-1]:
            arr[index], arr[index-1] = arr[index-1], arr[index]
            need_to_sort = True
            reversals += 1
print(" ".join(map(str, arr)), reversals, sep="\n")

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

Solution (Python):
def bubble_sort(A):
    cnt = 0
    for i in range(0, len(A) - 1):
        for j in range(len(A) - 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)


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

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

flag = 1
i = 0
cnt = 0

while flag:
    flag = 0
    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
            flag = 1
            cnt += 1
    i += 1
    
for k in range(n-1):
    print(A[k], end=' ')
print(A[n-1])
print(cnt)

Solution (Python):
def BubbleSort1(A):
    ans=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]
                ans+=1
    
    return ans

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


ans=BubbleSort1(a)

for i in range(len(a)):
    if i:
        print(" ",end="")
    print(a[i],end="")
print()
print(ans)

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

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

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

print(*A)
print(cnt)

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

if __name__ == '__main__':
    main()

Solution (Python):
def bubble_sort(A, N):
    exchange_cnt = 0
    flg = True
   
    while flg:
        flg = False
        for i in range(1, N)[::-1]:
            if A[i - 1] > A[i]:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
                exchange_cnt += 1
    print(" ".join([str(a) for a in A]))
    print(exchange_cnt)

N = int(input().rstrip())
A = [int(num) for num in input().rstrip().split(" ")]
bubble_sort(A, N)

Solution (Python):
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]:
                A[j], A[j-1] = A[j-1], A[j]
                count += 1

                flag = 1
    print(*A)
    print(count)

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

Solution (Python):

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

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

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

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

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

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

bubbleSort(a, n)

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

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

ans=str(num_list[0])
for i in range(1,len(num_list)):
    ans+=" "+str(num_list[i])
print(ans)
print(count)

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

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

print(*A)
print(count)

Solution (Python):
import sys

input = sys.stdin.readline


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

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

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


if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin

def bubble_sort(A, N):
    flg = 1
    count = 0
    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]
                flg = 1
                count += 1
    return count
                
    
def main():
    read = stdin.readline
    N = int(read())
    data = list(map(int, read().split(" ")))
    count = bubble_sort(data, N)
    for i in range(N-1):
        print(data[i], end = " ")
    print(data[N-1])
    print(count)
    
    
if __name__ == "__main__":
    main()

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

def bubbleSort(lst,N):
    flag=1
    i=0
    cnt=0
    while flag:
        flag=0
        for j in range(N-1,i,-1):
            if lst[j]<lst[j-1]:
                lst[j],lst[j-1]=lst[j-1],lst[j]
                cnt+=1
                flag=1
        i+=1
    return lst,cnt
ans=bubbleSort(b,a)
print(" ".join(list(map(str,ans[0]))),ans[1],sep="\n")

Solution (Python):


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

n = int(input())
l = list(map(int, input().split()))
res_l, res = b_sort(l,n)
print(*res_l)
print(res)

Solution (Python):
def bubbleSort(table):
    swap_count = 0
    for nAchieve in range(len(table) - 1):
        for index in range(len(table) - 1, nAchieve, -1):
            if table[index] < table[index - 1]:
                table[index], table[index - 1] = table[index - 1], table[index]
                swap_count += 1
    return swap_count


def conversion(table):
    return ' '.join(map(str, table))


element = int(input())
table = list(map(int, input().split()))
swap_count = bubbleSort(table)
table = conversion(table)

print('{}\n{}'.format(table, swap_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]:
                cnt += 1
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
    print(*A)
    print(cnt)


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


if __name__ == '__main__':
    main()

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

change = 1
for i in range(n):
    if not change:
        break
    change = 0
    for j in range(n-i-1):
        if x[j] > x[j+1]:
            x[j],x[j+1] = x[j+1],x[j]
            change = 1
            c += 1

print(*x)
print(c)

Solution (Python):

from sys import stdin

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

n = int(stdin.readline().rstrip())
A = list(map(int, stdin.readline().split()))
ans = bubble_sort(A, n)
print(' '.join(map(str,ans[0])))
print(ans[1])

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

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

nums = []
for i in range(1, N):
    nums.append(i)
    
nums.sort(reverse=True)

flag = 1
cnt = 0
while flag:
    flag = 0
    for num in nums:
        if A[num] < A[num-1]:
            A[num-1], A[num] = A[num], A[num-1]
            flag = 1
            cnt += 1

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

Solution (Python):
from sys import stdin
N = int(input())
A = [int(x) for x in stdin.readline().split(' ')]

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

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


N = int(input())
A = [int(x) for x in input().split()]
cnt = bubble_sort(A)
print(' '.join(map(str, A)))
print(cnt)

Solution (Python):
def printList(array):
    print(' '.join(map(str, array)))

def bubbleSort(A, N):
    needToContinue = True
    swapCount = 0
    while needToContinue:
        needToContinue = False
        for index in range(N - 1, 0, -1):
            if A[index] < A[index - 1]:
                A[index], A[index - 1] = A[index - 1], A[index]
                swapCount += 1
                needToContinue = True
    printList(A)
    print(str(swapCount))


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

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

frag = True
ex = 0

while frag:
    frag = False
    for j in range(n-1, 0, -1):
        if arr[j-1] > arr[j]:
            arr[j-1], arr[j] = arr[j], arr[j-1]
            frag = True
            ex += 1



def print_space(li):
    for i in range(len(li)-1):
        print(li[i], end=' ')
    else:
        print(li[-1])

        
print_space(arr)
print(ex)

Solution (Python):
def bubbleSort(A, N):
    cnt = 0
    flg = True
    while flg:
        flg = False
        for i in range(N - 1):
            if A[i] > A[i + 1]:
                tmp = A[i]
                A[i] = A[i + 1]
                A[i + 1] = tmp
                flg = True
                cnt += 1
    return A, cnt


def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans, cnt = bubbleSort(A, N)
    print(*ans)
    print(cnt)


resolve()

Solution (Python):
def bubblesort(N,A):
    flag = 1
    counta = 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
                counta += 1
    print(" ".join(map(str,A)))
    print(counta)

bubblesort(int(input()),list(map(int,input().split())))

Solution (Python):
N = int(input())
A = list(map(int, input().split(' ')))
if len(A) != N:
    raise ValueError('N個の整数を入力してください。')
elif N < 1 or N > 100:
    raise ValueError('1から100までの整数を入力してください')

frag = 1
count = 0

def print_num(A):
    for i in range(len(A)):
        if i != N-1:
            print(A[i], end=' ')
        else:
            print(A[i])
    
while frag:
    frag = 0
    for j in reversed(range(1, N)):
        if A[j]<A[j-1]:
            v = A[j]
            A[j] = A[j-1]
            A[j-1] = v
            frag = 1
            count += 1
            
print_num(A)
print(count)

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

cnt = 0


def bubble_sort(A, n):
    global cnt
    for _ in range(n):
        for i in range(n-1):
            if A[i] > A[i+1]:
                A[i], A[i+1] = A[i+1], A[i]
                cnt += 1


bubble_sort(A, n)
print(*A)
print(cnt)

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

    return A,count

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

    return A,count

if __name__ == '__main__':
    N = int(input())
    A = [int(i) for i in input().split()]
    ans,count = bubblesort_re(A,N)
    for i in range(N):
        if i != N - 1:
            print(ans[i],end = " ")
        else:
            print(ans[i])
    print(count)

Solution (Python):


from sys import stdin
input = lambda: stdin.readline().rstrip()

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

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

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

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

def bubble_sort(A,N):
    flag = True
    cnt  = 0
    while flag:
        flag = False
        for j in range(N-1,1-1,-1): 
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1],A[j]
                cnt += 1
                flag = True
    return A, cnt
    
sorted_A, cnt = bubble_sort(A,N)
print(*A)
print(cnt)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
import functools

def BubbleSort(A):
    cnt = 0
    for i in range(len(A)):
        for j in range(len(A)-1,i,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                cnt += 1
    return cnt
n = int(input())
A = list(map(int,input().split()))
cnt = BubbleSort(A)
print(*A)
print(cnt)

Solution (Python):

def buble_sort(N,a_count):
  flag = 1
  while(flag):
    flag = 0
    for i in range(1,len(N)):
      if N[i] < N[i-1]:
        tmp = N[i-1]
        N[i-1] = N[i]
        N[i] = tmp
        flag = 1
        a_count += 1
  return a_count


x = 0
N = int(input())
a = list(map(int,input().split()))
w = buble_sort(a,x) 

for  i in range(len(a)):
  if i == len(a)- 1:
    print(a[i],end = "")
  else:
    print(a[i],end = " ")
  
print()
print(w)

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):
N = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(N-1):
    for j in range(N-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=list(map(int,input().split()))
def bubbleSort(A,N):
    flg=1
    global ct
    ct = 0
    while flg:
        flg = 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]
                flg=1
                ct+=1

    return ' '.join(map(str,A))

print(bubbleSort(A,N))
print(ct)

Solution (Python):
size = int(input())
l = []

for i in map(int, input().split()):
    l.append(i)

sort_cnt = 0
reversed_cnt = 0

for j in range(size - 1,0,-1):
    for i in range(size - 1,sort_cnt,-1):
        tmp = l[i-1]
        if l[i] < l[i-1]:
            l[i-1] = l[i]
            l[i] = tmp
            reversed_cnt += 1
    sort_cnt += 1

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

Solution (Python):
from typing import List, Tuple


def bubble_sort(A: List[int]) -> Tuple[List[int], int]:
    flag: bool = True
    count: int = 0

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

    return A, count


def main():
    _ = int(input())
    A: List[int] = list(map(int, input().split()))

    lst, count = bubble_sort(A)
    print(*lst, sep=" ")
    print(count)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    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(*A)
    print(cnt)

if __name__ == '__main__':
    resolve()

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

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

bubbleSort(numlist,n)

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

def bubble_sort(r):
    flag=True
    i=0
    count=0
    while flag:
        flag=False
        for i in range(n-1,i,-1):
            if r[i]<r[i-1]:
                r[i],r[i-1]=r[i-1],r[i]
                count+=1
                flag=True
    print(*r)
    print(count)

bubble_sort(R)

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

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

print(*A)
print(ans)

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

flag = 1
e = 0
while flag:
    flag = 0
    for j in range(n-1, 0, -1):
        if l[j] < l[j-1]:
            v = l[j]
            l[j] = l[j-1]
            l[j-1] = v
            flag = 1
            e += 1
print(*l)
print(e)

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