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

bubble_sort(int(input()), [int(i) for i in input().split()])

Solution (Python):
def bubbleSort(A, N):
    flag = True
    c = 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]
                c += 1
                flag = True
    return A,c
num = int(input())
li1 = [int(e) for e in input().split()]
li2 = bubbleSort(li1,num)
print(*li2[0],sep=" ")
print(li2[1])

Solution (Python):
n = int(input())
numL = [0 for i in range(n)]

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

flag = 1
ctr = 0
while flag:
    flag -= 1
    for i in range(n-1,0,-1):
        if numL[i] < numL[i-1]:
            ctr += 1
            tmp = numL[i]
            numL[i] = numL[i-1]
            numL[i-1] = tmp
            flag = 1
print(*numL)
print(ctr)

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

  return count, A

n = int(raw_input())
a = map(int, raw_input().split())
count, a = bubble(n,a)

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

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

c = 0 
flag = 1
while flag:
    flag = 0
    j = N-1
    while j >= 1:
        if A[j] < A[j-1]:
            c +=1 
            v = A[j]
            A[j] = A[j-1]
            A[j-1] = v
            flag = 1
        j -=1

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

Solution (Python):
def bubble_sort(A, N):
    flag = 1
    cnt = 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
                cnt += 1
    print(' '.join(list(map(str, A))))
    print(cnt)
                

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

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

Solution (Python):
def Bubble_Sort(n, L):
    flag = True
    j = 0
    count = 0
    while flag:
        flag = False
        for i in range(n-1, j, -1):
            if L[i] < L[i - 1]:
                L[i], L[i - 1] = L[i - 1], L[i]
                count += 1
                flag = True
    return count

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

s = Bubble_Sort(n, L)
print(*L)
print(s)

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

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

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

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

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

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

def bubble_sort(A, N):
    num_exchange = 0
    FLAG = 1
    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]
                num_exchange += 1
                FLAG = 1
    show(A)
    print(num_exchange)

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

Solution (Python):
from typing import MutableSequence


def bubble_sort(a_list: MutableSequence, n:int) -> int:
    """バブルソート"""
    sw_cnt = 0
    for i in range(n - 1):
        for j in range(n -1, i, -1):
            if a_list[j - 1] > a_list[j]:
                a_list[j - 1], a_list[j] = a_list[j], a_list[j - 1]
                sw_cnt += 1

    print_list = [str(i) for i in a_list]
    print(' '.join(print_list))
    print(sw_cnt)

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

    bubble_sort(input_list, n)

Solution (Python):
def print_list(A):
    print(" ".join(map(str, A)))

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

def bubble(A):
    N = len(A)
    sorted = False
    count = 0
    for i in range(N):
        if (sorted):
            break
        sorted = True
        for j in reversed(range(i + 1, N)):
            if A[j] < A[j - 1]:
                swap(A, j, j - 1)
                count += 1
            sorted = False
    print_list(A)
    print(count)
    

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

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


def bubbleSort(a, n):
    flag = 1
    ans = 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
                ans += 1

    print(*a)
    print(ans)


bubbleSort(l, n)

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

num_swap = 0
flag = 1

while flag:
    flag = 0
    for j in reversed(range(N - 1)):
        if li[j + 1] < li[j]:
            temp = li[j + 1]
            li[j + 1] = li[j]
            li[j] = temp
            flag = 1
            num_swap += 1

print(*li)
print(num_swap)

Solution (Python):
def main(N, A):
    count = 0
    flag = True
    i = 0
    while flag:
        flag = False
        for j in list(reversed(list(range(i + 1, N)))):
            a = A[j-1]
            b = A[j]
            if a > b:
                A[j-1] = b
                A[j] = a
                count += 1
                flag = True
        i += 1
    print(' '.join(map(str, A)))
    print(count)
    return (A, count)


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

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

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

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

def bubble(a:list, n:int) -> None:
    if not isinstance(a, list):
        return None

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

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

bubble(a, n)

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

def  print_vec (A: list):
    print (" ".join(map (str, A)))

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

def bubble_sort(A: list) -> Tuple[list, int]:
    if len (A) <= 1:
        return (A, 0)
    swap_cout = 0
    for i in range(len(A)):
        for j in range(len(A) - 1, i + 1 - 1, -1):
            if A[j] < A[j - 1]:
                swap_cout += 1
                swap (A, j, j - 1)
    return (A, swap_cout)



if __name__ == "__main__":
    n = int(input())
    res = 0
    vec = list (map (int, input ().split ()))
    result=  bubble_sort(vec)
    print_vec (result [0])
    print(result [1])

Solution (Python):
n = int(input())
A = [int(a) for a in input().split()]
cnt = 0
flg = True
while flg:
    flg = 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
            flg = True
A = [str(a) for a in A]
print(" ".join(A))
print(cnt)

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

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

count = 0

print(*sorted(seq))

for i in range(len(seq)):
    for j in reversed(range(i + 1, len(seq))):
        if seq[j] < seq[j - 1]:
            seq[j], seq[j - 1] = seq[j - 1], seq[j]
            count += 1

print(count)

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

for i in range(1, N):
    for j in range(N-i):
        if data[N-1-j] < data[N-1-j-1]:
            data[N-1-j], data[N-1-j-1] = data[N-1-j-1], data[N-1-j]
            count += 1
            
print("%d" %data[0], end="")
for i in range(1, N):
    print(" %d" %data[i], end="")
print()
print(count)

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

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

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


N = int(input())
A = [int(i) for i in input().split()]
cnt = bubbleSort(A, N)
for i in range(len(A)):
    if i:
        print(" ", end = "")
    print(A[i], end = "")
print()
print(cnt)

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

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

print(*A)
print(cnt)

Solution (Python):
N = int(input())
num_list = [int(item) for item in input().split()]

def bubbleSort(A, N):
    is_flag = 1
    cnt = 0

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

bubbleSort(num_list, N)

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


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

count = bubble_sort(A, N)

print(*A)
print(count)

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

Solution (Python):
N, *A = map(int, open(0).read().split())

count = 0
loop = True
while loop:
    loop = 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]
            loop = True
            count += 1

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

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

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

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

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

Solution (Python):
import sys

def bubble_sort(size, numbers):
    swap_count = 0

    for i in range(size):
        sorted = True
        for j in range(size - 1, i, -1):
            if numbers[j] < numbers[j - 1]:
                sorted = False
                swap_count += 1
                numbers[j], numbers[j - 1] = numbers[j - 1], numbers[j]
        if sorted:
            break

    return swap_count

def main(stdin=None, stdout=None):
    if stdin is None:
        stdin = sys.stdin
    if stdout is None:
        stdout = sys.stdout

    size = int(stdin.readline().strip())
    numbers = [int(x) for x in stdin.readline().strip().split(" ")]

    swap_count = bubble_sort(size, numbers)
    stdout.write(" ".join([str(x) for x in numbers]) + "\n")
    stdout.write(str(swap_count) + "\n")

main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
numSwap = 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]
            numSwap += 1
        
a = [str(s) for s in a]
print(' '.join(a))
print(numSwap)

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


def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in reversed(range(1, N)):
            if A[i] < A[i - 1]:
                x = A[i]
                y = A[i - 1]
                A[i] = y
                A[i - 1] = x
                flag = 1

                count += 1

    print(*A)
    print(count)

main()

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

def bublesort(b,a):
    count =0
    for j in range(len(b)-1):
        for i in range(len(b)-1,0,-1):
            if b[i-1]>b[i]:
                b[i-1],b[i]=b[i],b[i-1]
                count +=1
    return count,b

A,B =bublesort(b,a)
print(*B)
print(A)

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

Solution (Python):






n = int(input())
arr = [ int(i) for i in input().split(" ")]

ans = 0

for i in range(len(arr) - 1):
  j = len(arr)-1
  for _ in range(len(arr)-i-1):
    if arr[j-1] > arr[j]:
      b = max(arr[j], arr[j-1])
      s = min(arr[j], arr[j-1])
      arr[j] = b
      arr[j-1] = s
      ans += 1
    j = j - 1
    
print(" ".join([str(i) for i in arr]))
print(ans)

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

cnt = 0
flag = 1
while flag == 1:
    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(" ".join([str(s) for s in A]))
print(cnt)

Solution (Python):

input_number = int(input())
input_list = list(map(int, input().split()))
exchange_count = 0


for i in range(input_number):
    for j in range(input_number-1, i, -1):
        if input_list[j] < input_list[j-1]:
            input_list[j], input_list[j-1] = input_list[j-1], input_list[j]
            exchange_count += 1
    

print(" ".join(map(str, input_list)))
print(exchange_count)

Solution (Python):
def BubbleSort(seq):
    count = 0

    for i in range(len(seq)):
        for j in reversed(range(i + 1, len(seq))):
            if seq[j] < seq[j - 1]:
                seq[j], seq[j - 1] = seq[j - 1], seq[j]
                count += 1

    print(*seq)
    print(count)

def main():
    n = input()
    seq = [int(a) for a in input().split()]
    BubbleSort(seq)

main()

Solution (Python):






def BubbleSort(A: list):
    number_of_swaps = 0
    flag = 1
    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
                number_of_swaps += 1
    print(*A, sep=" ")
    print(number_of_swaps)


def main():
    length = int(input())
    A = [int(e) for e in input().split(" ")]
    BubbleSort(A)

if __name__ == "__main__":
    main()

Solution (Python):
def babble_sort(n, data):
    __count = 0
    for i in range(n-1):
        for j in range(n-i-1):
            if data[j] > data[j+1]:
                tmp = data[j+1]
                data[j+1] = data[j]
                data[j] = tmp
                __count += 1
    return data, __count

def show(data, count):
    for i in range(len(data)):
        if i != (len(data)-1):
            print(data[i], end = " ")
        else:
            print(data[i])
    print(count)

if "__main__" == __name__:
    n = int(input())
    data = list(map(int, input().rsplit()))
    data, count = babble_sort(n, data)
    show(data, count)

Solution (Python):
def show(A):
    N = len(A)
    for i in range(N):
        if i :
            print(' ', end = '')
        print(A[i], end = '')
    print()

def bubleSort(A, N):
    flag = 1
    exchange = 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]
                exchange += 1
                flag = 1
    show(A)
    print(exchange)

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

bubleSort(A, N)

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

    flag = True
    ans = 0
    i = 0

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

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

bubble_sort()

Solution (Python):
def printData(data) :
    for i, value in enumerate(data) :
        if i > 0 :
            print(" ", end = "")
        print(value, end = "")
    print()
    
def bubbleSort(data) :
    flag = 1
    count = 0
    for i in range(len(data)):
        if not flag :
            break
        flag = 0
        for j in reversed(range(i + 1, len(data))):
            if data[j] < data[j - 1] :
                buf = data[j]
                data[j] = data[j - 1]
                data[j - 1] = buf
                flag = 1
                count += 1
    
    printData(data)
    print(count)
    
    
input()
data = [int(char) for char in input().split()]
bubbleSort(data)

Solution (Python):


n = int(input())

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

count = 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
            count += 1

print(*A)
print(count)

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

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

flag = 1
cnt = 0

for _ in range(n-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
                cnt += 1
print(" ".join(map(str, a)))
print(cnt)

Solution (Python):
def BubbleSort(A, N):
    j = 0
    flag = 1
    k = 0
    while flag:

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

                A[i-1], A[i] = A[i], A[i-1]
        
                
        
                
        
                
        
                flag = 1
        
                j += 1
        
    
    print(' '.join([str(x) for x in A]))
    print(j)

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

BubbleSort(A, N)

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

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

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

Solution (Python):
def main():
	length = int(input())
	target = list(map(int,input().split()))
	bubbleSort(target)

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


	print(*target,sep=' ')
	print(count)


if __name__ == '__main__':
	main()

Solution (Python):
def bubblesort(a,n):
    count = 0
    for i in range(n):
        for j in reversed(range(i+1,n)):
            if a[j] < a[j-1]:
                a[j-1],a[j] = a[j],a[j-1]
                count += 1
    print(" ".join(map(str,a)))
    print(count)
N = int(input())
A = [int(s) for s in input().split()]
bubblesort(A,N)

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


def bubble_sort(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 A, count

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

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


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

a, count = bubble_sort(a, n)

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

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

ans = 0
f = True
while f:
    f = 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]
            ans += 1
            f = True
s = ''
for a in A:
    s += str(a) + ' '
print(s.rstrip())
print(ans)

Solution (Python):

def bubble_sort(a,n):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(n-1):
            j = n - 1 - i
            if a[j] < a[j-1]:
                v = a[j]
                a[j] = a[j-1]
                a[j-1] = v
                flag = 1
                count += 1
    return a, count

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

arr, count = bubble_sort(a, n)
res = str(arr[0])
for i in range(n-1):
    res += " " + str(arr[i+1])

print(res)
print(count)

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

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

ans1, ans2 = Bubble_Sort(A, N)
print(' '.join(map(str, ans1)))
print(ans2)

Solution (Python):
"""
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
"""


def bubble_sort(lists, n):
    flag = 1
    x = 0
    while flag:
        flag = 0
        for j in range(n - 1, 0, -1):
            if lists[j] < lists[j - 1]:
                lists[j], lists[j - 1] = lists[j - 1], lists[j]
                x += 1
                flag = 1

    return lists, x


def main():
    N = int(input())
    lists = list(map(int, input().split(' ')))
    lists_answer, x = bubble_sort(lists, N)
    print(' '.join(map(str, lists_answer)))
    print(x)


if __name__ == '__main__':
    main()

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

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

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

flag = 1 
trade = 0
while flag:
    flag = 0
    for j in range(1, n):
        if a[j-1] > a[j]:
            a[j], a[j-1] = a[j-1], a[j]
            flag = 1
            trade += 1
print(' '.join(list(map(str, a))))
print(trade)

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

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

def bs(a, n):
  cnt=0
  f = True
  while f:
    f = False
    for i in range(n-1):
      v = a[i]
      if v>a[i+1]:
        a[i] = a[i+1]
        a[i+1] = v
        cnt+=1
        f = True
  return a,cnt

b,c = bs(a, n)

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

print()
print(c)

Solution (Python):
def bubble_sort(N, A):
    num = 0
    for k in range(0, N-1):
        for i in range(N-1, k, -1):
            if A[i] < A[i-1]:
                A[i], A[i-1] = A[i-1], A[i]
                num += 1
    return [num, *A]

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    ans = bubble_sort(N, A)
    print(*ans[1:])
    print(ans[0])

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
'''
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
'''


def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(N - 1):
            if A[i] > A[i + 1]:
                tmp = A[i]
                A[i] = A[i + 1]
                A[i + 1] = tmp
                flag = 1
                count += 1
    print(' '.join(list(map(str, A))))
    print(count)


bubbleSort(A, N)

Solution (Python):
from sys import stdin


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

if __name__ == "__main__":
    N = int(stdin.readline())
    A = [int(item) for item in stdin.readline().split()]
    cnt = bubbleSort(A, N)
    print(*A)
    print(cnt)

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


def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    count = bubble_sort(a, n)
    print(*a)
    print(count)


if __name__ == '__main__':
    main()

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

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

Solution (Python):
n = int(input())
arr = [x for x in list(map(int, input().split()))]

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

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

Solution (Python):
import re
n=int(input())
a=list(map(int,input().split()))
change_num=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]
			change_num+=1
			flag=True
print(re.sub("[\[\]\,]","",str(a)))
print(change_num)

Solution (Python):
def bubble_sort(A):
    a = list(A)
    swap_count = 0
    flag = 1
    while flag:
        flag = 0
        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]
                swap_count += 1
                flag = 1
    print(' '.join(map(str, a)))
    print(swap_count)

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

Solution (Python):

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

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

exc = bubblesort(a,n)
print(*a)
print(exc)

Solution (Python):
def b_sort(A):
    flg = True
    swap = 0
    while flg:
        flg = False
        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]
                swap += 1
                flg = True
    return swap

if __name__=='__main__':
    N = int(input())
    A = list(map(int,input().split()))
    swap = b_sort(A)
    print(*A)
    print(swap)