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):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    bubbleSort(A, N)

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

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

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
line = input()
arr= line.split(" ")
nums = []
for c in range(n):
    nums.append(int(arr[c]))

flag = "true"
convert_n = 0
while flag == "true":
    flag = "false"
    for i in range(len(nums)-1, 0, -1):
        if nums[i] < nums[i-1]:
            tmp = nums[i]
            nums[i] = nums[i-1]
            nums[i-1] = tmp
            flag = "true"
            convert_n += 1
            
n_list_str = ""
for num in nums:
    n_list_str += str(num)
    n_list_str += " "
print(n_list_str.rstrip(" "))
print(convert_n)

Solution (Python):
import sys

ERROR_INPUT = 'input is invalid'


def main():
    n = get_length()
    arr = get_array(length=n)

    sourtLi, count = bubbleSort(li=arr, length=n)
    print(*sourtLi)
    print(count)
    return 0


def get_length():
    n = int(input())
    if n < 0 or n > 100:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def get_array(length):
    nums = input().split(' ')
    return [str2int(string=n) for n in nums]


def str2int(string):
    n = int(string)
    if n < 0 or n > 100:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def bubbleSort(li, length):
    count = 0
    for n in range(length):
        for n in range(1, length - n):
            if li[n] < li[n - 1]:
                li[n], li[n - 1] = li[n - 1], li[n]
                count += 1

    return li, count


main()

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

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]
                flag = 1
                count += 1
        i += 1
    print(*a)
    print(count)
                   
bubbleSort(a,n)

Solution (Python):


import sys
import math

class Algo:
  @staticmethod
  def bubbleSort(r, n):
    flag = 1
    count = 0
    while flag:
      flag = 0
      for j in range(n-1, 0, -1):
        if r[j] < r[j-1]:
          r[j], r[j-1] = r[j-1], r[j]
          count += 1
          flag = 1

    for i in range(0, n):
      if i == n-1:
        print(r[i])
      else:
        print(r[i], " ", sep="", end="")
    print(count)

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

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

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

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

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

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


arr_length = int(input())
arr_num = [int(i) for i in input().split(" ")]

bubbleSort(arr_num, arr_length)

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


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

Solution (Python):
def bubble(l):
	count = 0
	N = len(l)
	flag = 1

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

	return l, count


if __name__ == '__main__':
	n = int(input())
	l = input().split()

	for i in range(n):
		l[i] = int(l[i])

	l, c = bubble(l)
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i)
			print(c)

Solution (Python):

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



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

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

s_A, count = bubble_sort(A, n)

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

Solution (Python):
def bubble_sort_aoj(nums, len):
    '''バブルソート：隣接項の比較'''
    flag = True
    count = 0
    
    while flag:
        flag = False
        
        for i in range(len - 1, 0, -1):
            if nums[i] < nums[i-1]:
                nums[i], nums[i-1] = nums[i-1], nums[i]
                flag = True
                count += 1
    
    return nums, count

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

    ans = bubble_sort_aoj(A, N)

    print(' '.join([str(x) for x in ans[0]]))
    print(ans[1])

Solution (Python):
input()
A = [int(i) for i in input().split()]
c = 0
def bubble_sort(A):
    global c
    flag = True
    while flag:
        flag = False
        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]
                c += 1
                flag = True
bubble_sort(A)
print(*A)
print(c)

Solution (Python):



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

    return A, inv_num


if __name__ == '__main__':

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

    A, inv_num = bubbleSort(A, N)
    print(" ".join(map(str, A)))
    print(inv_num)

Solution (Python):
def bubbleSort(a):
    sw = 0
    for _ in range(len(a) - 1):
        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]
                sw += 1
    return a, sw

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

a, sw = bubbleSort(a)

print(*a)
print(sw)

Solution (Python):
n=int(input())
s=input().split(' ')

flag=1
m=0

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

print(' '.join(s))
print(m)

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
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = 1
    print(*A)
    print(cnt)
N = int(input())
A = list(map(int, input().split()))
bubbleSort(A, N)

Solution (Python):
def bubbleSort(A, N):
    cnt = 0
    for _ in range(N):
        for j in range(N - 1, 0, -1):
            if A[j] < A[j - 1]:
                cnt += 1
                A[j], A[j - 1] = A[j - 1], A[j]
    print(*A)
    print(cnt)
N = int(input())
A = list(map(int, input().split()))
bubbleSort(A, N)

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

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

print(*arr)
print(count)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c=0
for i in range(n-1):
 for j in range(0,n-i-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):
count = int(input());
data = [int(n) for n in input().split(" ")];

def bubble_sort(data):
    count = len(data);
    o = 0;
    for i in range(count):
        for j in range(count - 1, i, -1):
            if data[j] < data[j - 1]:
                o += 1;
                temp = data[j];
                data[j] = data[j - 1];
                data[j - 1] = temp;
    show(data);
    print(o);

def show(data):
    print(" ".join(str(n) for n in data));

bubble_sort(data);

Solution (Python):
def bubbleSort(A,N):  
    flag = 1
    i = 0  
    while flag == 1:
        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
                i += 1
    print(*A)
    print(i)

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

bubbleSort(A,N)

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

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

    print(' '.join(nums))
    print(count)

bubbleSort(nums,n)

Solution (Python):

def pprint(numlist):
  print(" ".join(map(str,numlist)))
N = int(input())
A = list(map(int,input().split()))
count = 0
flag = True
while flag:
  flag = False
  j = N - 1
  while j >= 1:
    if A[j-1] > A[j]:
      v = A[j]
      A[j] = A[j-1]
      A[j-1] = v
      flag = True
      count += 1
    j -= 1
pprint(A)
print(count)

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

Solution (Python):
def bSort(A):
    flag = 1
    i = 0
    count = 0
    while flag:
        flag = 0
        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
                flag = 1
                count += 1
        i += 1
    return A, count

if __name__ == '__main__':
    length = input()
    data = [int(x) for x in input().split()]
    data_s, c = bSort(data)
    print (' '.join([str(x) for x in data_s]))
    print (c)

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

flag = 1
count = 0
while flag:
    flag = 0
    for j in range(n-1,0,-1):
        if A[j] < A[j-1]:
            A[j],A[j-1] = A[j-1],A[j]
            flag = 1
            count += 1
print(" ".join(map(str,A)))
print(count)

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

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c=0
for i in range(n-1):
 for j in range(n-i-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 bsort(A, N):
    flag = 1
    swap = 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
                swap += 1
    return swap

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

swap = bsort(A, N)
print " ".join([repr(i) for i in A])
print swap

Solution (Python):



import sys

def bubble_sort(A):
    swap_count = 0
    for i in range(len(A)):
        for x in range(len(A)-1, i, -1):
            if A[x] < A[x-1]:
                swap_count += 1
                A[x], A[x-1] = A[x-1], A[x]

    return swap_count

n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

swap_count = bubble_sort(A)
s_list = list(map(str, A))
print(' '.join(s_list))
print(swap_count)

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

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


def bubbleSort(array):
    flag = True
    count = 0
    while flag:
        flag = False
        for j in sorted(range(1, len(array)), reverse=True):
            if array[j] < array[j - 1]:
                array[j], array[j - 1] = [array[j - 1], array[j]]
                flag = True
                count += 1
    show(array)
    return count


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

print(bubbleSort(array))

Solution (Python):
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    
    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]
                flag = True
                count += 1
    
    print(*a)
    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
def Bubble(A, N):
    f = 1
    c = 0
    while f:
        f = 0
        j = N - 1
        while j > 0:
            if A[j] < A[j - 1]:
                c += 1
                a = A[j]
                A[j] = A[j - 1]
                A[j - 1] = a
                f = 1
            j -= 1
    return A, c

N = int(input())
A = list(map(int, input().split(' ')))
A, c = Bubble(A, N)
ans = ' '.join(map(str, A)) + '\n' + str(c)
print(ans)

Solution (Python):
def bubbleSort(A, N):
    flag = 1
    i = 0
    swapnum = 0
    while flag:
        flag = 0
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                temp = A[j-1]
                A[j-1] = A[j]
                A[j] = temp
                swapnum += 1
                flag = 1
        i = i + 1
    return swapnum
def showlist(A):
    for i in range(len(A)):
        if i==len(A)-1:
            print(A[i])
        else:
            print(A[i], end=' ')
N = eval(input())
A = list(map(int, input().split()))
swapnum = bubbleSort(A, N)
showlist(A)
print(swapnum)

Solution (Python):
def BubbleSort(A,N):
    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 = count +1
    print(' '.join(map(str,A)))
    print(count)
            
N = int(input())
A = input()
A = list(map(int,A.split()))
BubbleSort(A,N)

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

cnt = 0
for i in range(int(n)-1):
    for j in range(int(n)-1):
        if nums[j] > nums[j+1]:
            tmp = nums[j+1]
            nums[j+1] = nums[j]
            nums[j] = tmp
            cnt += 1

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

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


def bubble_sort(a):
    c = 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]
                c += 1
                flag = 1

        i += 1

    print(*a)
    print(c)


bubble_sort(array)

Solution (Python):
def BubbleSort(A,N):
    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 = count +1
    print(' '.join(map(str,A)))
    print(count)
             
N = int(input())
A = list(map(int,input().split()))
BubbleSort(A,N)

Solution (Python):
import sys

l = []
for i in sys.stdin:
    l.append(i.split())

def bubble_sort(data):
    count = 0
    for i in range(0,len(data)):
        for j in range(len(data)-1,i,-1):
            if data[j] < data[j-1]:
                count += 1
                temp = data[j]
                data[j] = data[j-1]
                data[j-1] = temp
    for i in range(0,len(data)):
        print(data[i],end='')
        print(" ",end='') if i < len(data)-1 else print()
    print(count)

for i in range(0,len(l[1])):
    l[1][i] = int(l[1][i])

bubble_sort(l[1])

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

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

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

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
f = 1; c = 0
while f > 0:
  f = 0
  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; c += 1
print(*l)
print(c)

Solution (Python):
def bubble_sort(alist):
    """Sort alist by bubble sort.
    Returns (number of swap operations, sorted list)

    >>> bubble_sort([5, 3, 2, 4, 1])
    ([1, 2, 3, 4, 5], 8)
    """
    size = len(alist)
    count = 0

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

    return (alist, count)


def run():
    _ = int(input()) 
    nums = [int(i) for i in input().split()]

    (sorted_list, num_swap) = bubble_sort(nums)

    print(" ".join([str(i) for i in sorted_list]))
    print(num_swap)


if __name__ == '__main__':
    run()

Solution (Python):




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





flag = 1
i = 0
cnt = 0


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



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

Solution (Python):
num = int(input())
cards = [int(i) for i in input().split()]

def bubble_sort(num, cards):
	flag = 1
	count = 0
	while flag:
		flag = 0
		for j in range(num-1, 0, -1):
			if cards[j] < cards[j-1]:
				card_tmp = cards[j]
				cards[j] = cards[j-1]
				cards[j-1] = card_tmp
				flag = 1
				count += 1
	return output(cards), count

def output(cards):
	sorting = [str(i) for i in cards]
	return (" ".join(sorting))

output, count = bubble_sort(num, cards)
print(output)
print(count)

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


num = int(input())
lst = [int(i) for i in input().split()]

lst = bubbleSort(lst,num)
print(*lst[0])
print(lst[1])

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

c = 0
a = 0

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

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

Solution (Python):
def bubbleSort(A):
    steps = 0
    flag = 1
    completed_idx = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1] > A[i]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
        completed_idx += 1

    for e in A[:-1]:
        print(e, end=' ')
    print(A[-1])
    print(steps)

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

Solution (Python):
def bubbleSort(A, N): 
    A = list(map(int, A.split()))
    flag = 1 
    count = 0  
    while flag:
        flag = 0
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
                count += 1
    print(" ".join(map(str,A)))
    print(count)

N = int(input())
A = input()
bubbleSort(A, N)

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

Solution (Python):
def bubble_sort(l):
    num = 0
    for index in range(len(l)):
        for low in range(len(l)-1, index, -1):
            if l[low] < l[low-1]:
                l[low], l[low-1] = l[low-1], l[low]
                num += 1

    return (l, num)
if __name__ == '__main__':
    n = int(input())
    l = list(map(int,input().split()))
    lsorted, num = bubble_sort(l)
    print(" ".join(list(map(str,lsorted))))
    print(num)

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

ans = 0

for i in range(x):
    for j in range(x-1):
        if li[j] > li[j+1]:
            li[j], li[j+1] = li[j+1], li[j]
            ans += 1

print(*li)
print(ans)

Solution (Python):





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

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

print(' '.join([str(x) for x in A]))
print(exc_num)

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

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

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

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

flag = True
count = 0

while flag:
    flag = False
    for i in range(1, N)[::-1]:
        if A[i] < A[i-1]:
            A[i], A[i-1] = A[i-1], A[i]
            flag = True
            count += 1
print(' '.join(map(str, A)))
print(count)

Solution (Python):
def bubble_sort(ls):
    flag=True
    i=0
    ch=0
    while flag:
        flag = False
        for j in reversed(range(i+1,len(ls))):
            if ls[j] < ls[j-1]:
                ls[j],ls[j-1] = ls[j-1],ls[j]
                flag = True
                ch += 1
        i += 1
    return ch

if __name__ == "__main__":
    ls=[]
    n=int(input())
    ls = list(map(int,input().split()))
    ch=bubble_sort(ls)
    for i in range(len(ls)):
        if i == len(ls)-1:
            print(ls[i])
        else:
            print(ls[i],end=" ")
    print(ch)

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

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

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

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
 
print(" ".join(map(str, a)))
print(counter)

Solution (Python):

def BubleSort(l):
    cnt = 0

    for idx in range(len(l), 0, -1):
        for jdx in range(1, idx):
            if l[jdx] < l[jdx-1]:
                l[jdx-1], l[jdx] = l[jdx], l[jdx-1]
                cnt += 1

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

def main():
    _ = input()
    in_list = list(map(int, input().split(' ')))
    BubleSort(in_list)

main()

Solution (Python):
def bubble_sort(A):
    flag = True
    count = 0
    while flag:
        flag = 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]
                flag = True
                count += 1
    return A, count

if __name__ == '__main__':
    n = int(input())
    A = [int(x) for x in input().split()]
    result_A, count = bubble_sort(A)
    print(*result_A)
    print(count)

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):
    if i == N - 1:
        print("{0}".format(A[i]))
    else:
        print("{0} ".format(A[i]), end="")

print(count)

Solution (Python):
def bubbleSort(a,n):
    flag = 1
    c=0
    while(flag):
        flag = 0
        for i in reversed(range(1,len(a))):
            if(a[i] < a[i-1]):
                a[i],a[i-1] = a[i-1],a[i]
                c += 1
                flag = 1
    return [a,c]

n = int(input())
a = list(map(int,input().split(' ')))
sa , c = bubbleSort(a, n)
str = " ".join([str(i) for i in sa])
print("{}\n{}".format(str,c))

Solution (Python):
def bubble_sort(args):
    count = 0
    result = args[:]
    l = len(result) - 1
    for i in range(0, l):
        for j in range(l, i, -1):
            if (result[j] < result[j-1]):
                result[j], result[j-1] = result[j-1], result[j]
                count += 1
    return result, count

if __name__ == '__main__':
    n = int(input())
    args = list(map(int, input().split()))[:n]
    r, c = bubble_sort(args)
    print(' '.join(map(str, r)))
    print(c)

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

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

    sl, cnt = bubble_sort(l)
    print(' '.join(map(str, sl)))
    print(cnt)

Solution (Python):
def swap(l, i, j):
    tmp = l[i]
    l[i] = l[j]
    l[j] = tmp
    return l

def bubble_sort(l):
    cnt = 0
    for i in range(0, len(l) - 1):
        flag = True
        for j in range(len(l)-1, i, -1):
            if l[j-1] > l[j]:
                l = swap(l, j-1, j)
                cnt += 1
                flag = False
        if flag:
            break

    return l, cnt

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

    sl, cnt = bubble_sort(l)
    print(' '.join(map(str, sl)))
    print(cnt)

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

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

for i, n in enumerate(A):
    if i == N - 1:
        print(n)
    else:
        print(n, end=' ')

print(cnt)

Solution (Python):
def bubbleSort(A, N): 
    A = list(map(int, A.split()))
    flag = 1 
    count = 0  
    while flag:
        flag = 0
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
                count += 1
    print(" ".join(map(str,A)))
    print(count)
 
N = int(input())
A = input()
bubbleSort(A, N)

Solution (Python):
def BS(A, N):
    A = list(map(int, A.split()))
    flag = 1
    count = 0 
    while flag:
        flag = 0
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                unk = A[j]
                A[j] = A[j-1]
                A[j-1] = unk
                flag = 1
                count = count +1
    print(" ".join(map(str,A)))
    print(count)
 
N = int(input())
A = input()
BS(A, N)

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


def bubble_sort(ary, verbose=True):
    flag = True
    count = 0

    while flag:
        flag = False
        i = 0

        for j in range(N - 1, i, -1):

            if ary[j] < ary[j - 1]:
                ary[j], ary[j - 1] = ary[j - 1], ary[j]

                flag = True
                count += 1

        i += 1

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

    if verbose:
        print(count)


bubble_sort(ary)