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):
factor = int(input())
numbers = list(map(int, input().split()))

def bublesort(array, n):
    counter = 0
    flag = 1
    while(flag):
        i=0
        flag=0
        temp = 0
        for j in range(len(array)-1, i, -1):
            if array[j] < array[j-1]:
                temp = array[j]
                array[j] = array[j-1]
                array[j-1] = temp
                flag = 1
                counter += 1
        i += 1
    return counter


repetition = bublesort(numbers, factor)
print(" ".join(list(map(str,numbers))))
print(repetition)

Solution (Python):
def swap(a, j):
    
    k = a[j]
    a[j] = a[j-1]
    a[j-1] = k


def bubbleSort(a, n):
    sw = 0
    flag = 1
    while flag:
        flag = 0
        for j in range(n-1,0,-1):
            if a[j] < a[j-1]:
                swap(a, j)
                flag = 1
                sw += 1
    print(' '.join(list(map(str, a))))
    print(sw)

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

Solution (Python):
N = int(input())
A = input()
B = A.split()
flag = 1
total = 0
while flag :
    flag = 0
    for i in range(N-1, 0, -1) :
        if int(B[i]) < int(B[i-1]) :
            B[i], B[i-1] = B[i-1], B[i]
            flag = 1
            total += 1
print(" ".join(B))
print(total)

Solution (Python):
n = int(input())
x = list(map(int, input().split())) 
flag = 1
i = 0
v = 0
j = n-1
cont = 0
for i in range(n):
    for k in range(n-1, i, -1):
        if x[k] < x[k-1]:
            v = x[k-1]
            x[k-1] = x[k]
            x[k] = v
            cont = 1 + cont
print(*x)
print(cont)

Solution (Python):
n = int(input())
cards = list(map(int,input().split()))
cnt = 0
flag = 1
while flag:
    flag = 0
    for j in range(n-1,0,-1):

        if cards[j] < cards[j-1]:
            a = cards[j]
            b = cards[j-1]
            cards[j] = b
            cards[j-1] = a
            cnt += 1
            flag = 1

print(*cards)
print(cnt)

Solution (Python):

n = int(input())
a = list(map(int, input().split()))
cnt = 0
frag = 1
while frag:
    frag = 0
    for i in reversed(range(1, n)):
        if a[i-1] > a[i]:
            a[i-1],a[i] = a[i], a[i-1]
            cnt += 1
            frag = 1
print(*a)
print(cnt)

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

Solution (Python):
list_size = input()                                                                                                                                                                                         
list_str = input()                                                                                                                                                                                          
val_list = list(map(int, list_str.split()))                                                                                                                                                                 
                                                                                                                                                                                                            
def swap(first_idx, second_idx, val_list):                                                                                                                                                                  
    val = val_list[first_idx]                                                                                                                                                                               
    val_list[first_idx] = val_list[second_idx]                                                                                                                                                              
    val_list[second_idx] = val                                                                                                                                                                              
                                                                                                                                                                                                            
counter = 0                                                                                                                                                                                                 
while(True):                                                                                                                                                                                                
    finish = True                                                                                                                                                                                           
    for idx_num in range(0, int(list_size) - 1):                                                                                                                                                            
        if val_list[idx_num + 1] < val_list[idx_num]:                                                                                                                                                       
            swap(idx_num, idx_num + 1, val_list)                                                                                                                                                            
            finish = False                                                                                                                                                                                  
            counter += 1                                                                                                                                                                                    
    if finish == True: break                                                                                                                                                                                
                                                                                                                                                                                                            
print_list = ','.join(list(map(str, val_list)))                                                                                                                                                             
print_list = print_list.replace(',', ' ')                                                                                                                                                                   
print(print_list)                                                                                                                                                                                           
print(counter)

Solution (Python):
def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        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 = 1
    print(' '.join(map(lambda x: str(x), A)))
    print(count)
    return(A)
N = input()

line = raw_input()
line = line.strip().split()
line = map(lambda x: int(x), line)
bubbleSort(line, N)

Solution (Python):


def printA(a, n):
    for i in range(n):
        print(a[i],end="")
        if i < n - 1:
            print(" ", end="")
        else:
            print()

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

n = int(input().rstrip())
a = list(map(int,input().rstrip().split()))
c = bubbleSort(a, n)
printA(a,n)
print(c)

Solution (Python):
def bubblesort(array):
    flag = True
    res = 0
    while flag:
        flag = False
        for i in range(len(array)-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                res += 1
                flag = True
    return (array, res)
    
int(input())
arr, c = bubblesort([int(i) for i in input().split()])
print(*arr,sep=' ')
print(c)

Solution (Python):
def bubbleSort(A: list, N:int) -> list:
    """
    A[N]	サイズがNの整数型配列。
    i	未ソートの部分の先頭を指すループ変数で、配列の先頭から末尾に向かって移動します。
    j	未ソートの部分の隣り合う要素を比較するためのループ変数で、Aの末尾であるN-1 から開始しi+1 まで減少します。
    """
    has_finished = True
    i = 0
    count = 0
    while has_finished and len(A) > 1:
        has_finished = False
        for j in reversed(range(i + 1, N)):
            if A[j] < A[j - 1]:
                
                a, b = A[j - 1], A[j]
                A[j - 1], A[j] = b, a
                has_finished = True
                count += 1
        i += 1
    return A, count

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

print(*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 bubbleSort(A,N):
    count = 0
    flag = True
    while flag:
        flag = False
        for i in range(1,N):
            if A[i-1] > A[i]:
                A[i-1],A[i]=A[i],A[i-1]
                count+=1
                flag = True
    show(A)
    print(count)
X = int(input())
Y = list(map(int,input().split()))
bubbleSort(Y,X)

Solution (Python):

def bubble_sort(array):
    N = len(array)
    is_reversed_items_exist = True
    reverse_time = 0

    while  is_reversed_items_exist:
        is_reversed_items_exist = False
        for i in range(N - 1):
            if array[i] > array[i + 1]:
                array[i], array[i + 1] = array[i + 1], array[i]
                is_reversed_items_exist = True
                reverse_time += 1
    return reverse_time
    
def putsarray(array):
    for i in range(len(array) - 1):
        print(array[i], end=' ')
    print(array[-1])

if __name__ == "__main__":
    N = int(input())
    array = input().split()
    array = [int(n) for n in array]
    reverse_time = bubble_sort(array)
    putsarray(array)
    print(reverse_time)

Solution (Python):
def bubble_sort(A, N):
    flag = True
    exchange_count = 0
    while flag:
        flag = False
        for j in reversed(range(1,N)):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = True
                exchange_count += 1
    print(*A)
    print(exchange_count)

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

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

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

flag = 1
counter = 0

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

show(A)
print(counter)

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

Solution (Python):
def bubbleSort(A, N):
    '''list of int, int -> list of int(sorted)
    N = len(A)
    return list(bubble sorted)
    '''
    assert(len(A) == N)
    outA = A.copy()
    flag = True
    numofcng = 0
    while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if outA[j] < outA[j - 1]:
                outA[j], outA[j - 1] = outA[j - 1], outA[j]
                numofcng += 1
                flag = True
    return outA, numofcng

def printlist(L):
    for itm in L[:-1]:
        print(itm, end = ' ')
    print(L[-1])

n = int(input())
A = list(map(int, input().split()))
outA, num = bubbleSort(A, n)
printlist(outA)
print(num)

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

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

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

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

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

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

                a[j], a[j-1] = a[j-1], a[j]
                count += 1
                flag = 1
        i += 1
    print(' '.join(map(str, a)))
    print(count)



def trace(a, n):
    output = ''
    for i in a:
        if i == a[n-1]:
            output += str(i)
        else:
            output += str(i) + ' '
    print(output)


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

bubbleSort(a, n)

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

N = int(input())
A = list(map(int, input().split()))
A, t = bobbleSort(A, N)
print(' '.join(map(str, A)), t, sep='\n')

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(n):
    for j in reversed(range(i, n - 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):

"""
Created on Sat Jan 26 22:45:33 2019

@author: Yamazaki Kenichi
"""

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

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

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

cnt = 0
for i in range(1, N):
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if I[j] < I[j-1]:
                tmp = I[j]
                I[j] = I[j-1]
                I[j-1] = tmp
                flag = True
                cnt += 1
print(str(I).replace(',','').replace('[','').replace(']',''))
print(cnt)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
flag=1
count=0
while flag:
    flag=0
    j=1
    while j < n:
        if A[j]<A[j-1]:
            A[j], A[j-1]=A[j-1], A[j]
            count+=1
            flag=1
        j+=1

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

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


n = int(input())
a = [int(i) for i in input().split()]
count = bubbleSort(a, n)
print(" ".join(map(str, a)))
print(count)

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

flag = True
count = 0

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

print(*A)
print(count)

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

Solution (Python):
def bubbleSort(A, N):
    flg = 1
    i = 0
    cnt = 0
    while flg:
        flg = 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]
                flg = 1
                cnt += 1
        i += 1
    return cnt

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

c = bubbleSort(a, n)

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

Solution (Python):
def showSeq(seq):
  for i in range(len(seq)):
    if i<len(seq)-1: 
      print(seq[i],end=' ')
    else:
      print(seq[i])
      
      
n = input()
list1 = list(map(int,input().split()))
count = 0

for i in range(len(list1)-1):
  for j in range(len(list1)-1,i,-1):
    if list1[j]<list1[j-1]:
      list1[j],list1[j-1] = list1[j-1],list1[j] 
      
      count+=1
showSeq(list1)
print(count)

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

if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))
    a, num = bubbleSort(a, N)
    for i in range(len(a)):
        if i != len(a)-1:
            print(a[i], end=" ")
        else:
            print(a[i])
    print(num)

Solution (Python):
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]

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

print(*A)
print(count)

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

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

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

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

def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        j = N - 1
        while j > 0:
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                count += 1
                flag = 1
            j -= 1
    return count


c = bubbleSort(A, N)
for i in range(len(A)):
    if i != len(A) - 1:
        print(A[i], end=" ")
    else:
        print(A[i])
print(c)

Solution (Python):
def printer(A):
    ret = str(A[0])
    for i in range(1, len(A)):
        ret += " " + str(A[i])
    print(ret)
    return True


def bubblesort(A):
    ret = 0
    flag = True
    num = len(A)
    while flag is True:
        flag = False
        for i in reversed(range(1, num)):
            if A[i - 1] > A[i]:
                tmp = A[i - 1]
                A[i - 1] = A[i]
                A[i] = tmp
                flag = True
                ret += 1
        
    return ret


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

ans = bubblesort(A)

printer(A)
print(ans)

Solution (Python):
import sys


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


n = int(sys.stdin.readline())
a = [int(v) for v in sys.stdin.readline().strip().split()]
count = bubble_sort(a)
print(' '.join(map(str, a)))
print(count)

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 = [int(i) for i in input().split()]
A=[]
for i in range(n):
    A.append(a[i])

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

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

flag = 1
i = 0
k = 0
while flag:
    flag = 0
    for j in range(n - 1, i, -1):
        if a[j] < a[j - 1]:
            t = a[j]
            a[j] = a[j - 1]
            a[j - 1] = t
            flag = 1
            k += 1
    i += 1
for i in range(0, n-1):
    print(a[i], end=" ")
print(a[n-1])
print(k)

Solution (Python):

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

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

Solution (Python):
def bubbleSort(A, N):
    cnt = 0
    flag = 1
    while flag:
        flag = 0
        for j in range(N - 1, 0, -1):
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = 1
                cnt = 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):




import sys
input = sys.stdin.readline


def bubbleSort(l):
    cnt = 0    
    flg = True
    while True:
        flg = False
        for i in range(len(l)-1):
            if l[i] > l[i + 1]:
                (l[i],l[i+1]) = (l[i+1],l[i])
                cnt += 1
                flg = True
        if flg == False:
            break
    return l,cnt

def main():
    n = int(input())
    l = list(map(int,input().split()))
    (ans,cnt) = bubbleSort(l)
    print (*ans)
    print (cnt)
if __name__ == '__main__':
    main()

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


def bubble_sort(Ary):
    N = len(Ary)
    tento_su = 0
    flag = 1 
    while flag == 1: 
        flag = 0
        for i in range(N-1):
            if Ary[-1-i]<Ary[-2-i]:
                X = Ary[-1-i]
                Ary[-1-i] = Ary[-2-i]
                Ary[-2-i] = X
                flag = 1
                tento_su += 1
    return Ary, tento_su

A_sorted , tento_su = bubble_sort(A)
print(' '.join(map(str, A_sorted)))
print(tento_su)

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


def bubblesort(n,a):
    count = 0
    flag = 1
    while flag:
        flag = False
        for i in reversed(range(1,n)):
            if a[i-1] > a[i]:
                a[i], a[i-1] = a[i-1], a[i]
                flag = True
                count += 1
    return a, count

a, count = bubblesort(n,a)
print(' '.join(map(str, a)))
print(count)

Solution (Python):
def main():
	n = int(input())
	r = [int(a) for a in input().split()]
	ct = bubbleSort(r, n)
	printlist(r)
	print(ct)

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


def printlist(a):
	values = map(str, a)
	print(' '.join(values))

main()

Solution (Python):
n=int(input())
*a,=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(*a)
print(c)

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

a = input().split()

a = [int(x) for x in a]

flag = True
i = 0
count = 0

while flag:
    flag = False

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

    i += 1

for i,x in enumerate(a):
    if i != len(a)-1:
        print(x, end=" ")
    else:
        print(x, end="")
print()
print(count)

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

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

c=0
for i in range(n):
    j=n-1
    while j>0:
        if l[j]<l[j-1]:
            x=l[j]
            l[j]=l[j-1]
            l[j-1]=x
            c+=1
        j-=1
        
print(" ".join(map(str,l)))
print(c)

Solution (Python):


N = int(input())
aN = list(map(str, input().split()))

done = False
cnt = 0
while not done:
    done = True
    for i in range(N-1, 0, -1):
        if int(aN[i]) < int(aN[i-1]):
            tmp = aN[i]
            aN[i] = aN[i-1]
            aN[i-1] = tmp
            done = False
            cnt += 1
print(' '.join(aN))
print(cnt)

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

Solution (Python):
def solve():
    _ = input()
    l = list(map(int, input().split()))
    count = 0
    is_sorted = True
    while is_sorted:
        is_sorted = False
        for i in range(len(l) - 1, 0, -1):
            if l[i] < l[i - 1]:
                l[i], l[i-1] = l[i-1], l[i]
                is_sorted = True
                count += 1

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


if __name__ == '__main__':
    solve()

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

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

    bubble_sort(n, nums)

if __name__ == "__main__":
    main()

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


n=int(input())
nums=list(map(int, input().split()))
count=BubbleSort(nums)
maplist=map(str, nums)
print(' '.join(maplist))
print(count)

Solution (Python):
def bubble_sort(num_list):
    cnt = 0
    flag = 1

    while flag:
        flag = 0
        for i in range(len(num_list)-1,0,-1):
            if num_list[i] < num_list[i-1]:
                num_list[i],num_list[i-1] = num_list[i-1],num_list[i]
                cnt += 1
                flag = 1
    return cnt

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

ans = bubble_sort(num_list)
ans_list = [str(i) for i in num_list]
print(' '.join(ans_list))
print(ans)

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

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

cnt = 0

for i in range(n):
    for j in range(n-1, i, -1):
        if blist[j] < blist[j-1]:
            cnt += 1
            blist[j], blist[j-1] = blist[j-1], blist[j]

for i in range(n):
    if i == n-1:
        print(blist[i])
    else:
        print(str(blist[i]) + " ", end="")

print(cnt)

Solution (Python):
import sys

def main():
    
    

    inputslen = int(sys.stdin.readline().rstrip())
    inputs= sys.stdin.readline()

    sortvalue = [int(i) for i in inputs.split()]
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, inputslen):
            j = inputslen - i
            compvalue = sortvalue[j]
            if compvalue < sortvalue[j-1]:
                sortvalue[j] = sortvalue[j-1]
                sortvalue[j-1] = compvalue
                flag = 1
                count += 1

    printvalue = map(str, sortvalue)
    print(' '.join(printvalue))
    print(count)

if __name__ == '__main__':
    main()

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

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

print(*A)
print(count)

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

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

Solution (Python):
class Bubble_sort:
    def __init__(self):
        self.swap_num=0
    
    def sort(self,n,list):
        for i in range(n):
            for j in range(i+1,n)[::-1]:
                if(list[j] < list[j-1]):
                    list[j],list[j-1]=list[j-1],list[j]
                    self.swap_num+=1
        return list




n=int(input())
data=list(map(int,input().split()))
bubble_sort=Bubble_sort()
print(" ".join(list(map(str,bubble_sort.sort(n,data)))))
print(bubble_sort.swap_num)

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

count = 0
flag = 1
i = 0
while flag:
    flag = 0
    for j in range(N-1, i, -1):
        if A[j] < A[j-1]:
            A[j], A[j-1] = A[j-1], A[j] 
            flag = 1
            count += 1
    i += 1
        
for k in range(N):
        if k > 0:
            print(' ', end='')
        print(A[k], end='')

print('')
print(count)

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]
                cnt += 1
                flag = True
    return A, cnt
    
result = bubbleSort(a, n)
print(' '.join(map(str, result[0])))
print(result[1])