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(raw_input())
num_list = map(int, raw_input().split())

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

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

Solution (Python):
numbers = []
n = int(raw_input())
s = raw_input()
count = 0

numbers = s.split(" ")
for i in range(int(n)):
	numbers[i] = int(numbers[i])

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

Solution (Python):
num = raw_input()
num_list = raw_input().split()
num_list = map(int, num_list)
count = 0 

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

count = bubble_sort(num_list, count)

num_list =  map(str, num_list)
print " ".join(num_list)
print count

Solution (Python):
import sys

lines = sys.stdin.readlines()

N = lines[0]
A = lines[1].strip().split(" ")

flag = 1

c = 0
i = 0
while flag == 1:
    flag = 0
    for j in reversed(range(i+1,len(A))):
        if int(A[j]) < int(A[j-1]):
            a = A[j]
            b = A[j-1]
            A[j] = b
            A[j-1] = a
            flag = 1
            c += 1
    i += 1

print " ".join(A)
print c

Solution (Python):
N = int(raw_input())
A = raw_input().split(" ")
count = 0
flag = 1

i = 0
while flag == 1:
    flag = 0
    for j in reversed(range(i+1, N)):
        if int(A[j]) < int(A[j-1]):
            temp = A[j]
            A[j] = A[j-1]
            A[j-1] = temp
            count += 1
            flag = 1
    i += 1
    
print " ".join(A)
print count

Solution (Python):
def input_data():
    num = raw_input()
    array = []
    array = num.strip().split(" ")
    array = map(int, array)
    return array


def main():
    n = raw_input()
    count = 0
    array = input_data()
    flag = True
    i = 0
    while flag:
        flag = False
        for j in reversed(xrange(i + 1, int(n))):
            if array[j] < array[j - 1]:
                v = array[j]
                array[j] = array[j - 1]
                array[j - 1] = v
                flag = True
                count += 1
        i += 1
    array = map(str, array)
    print " ".join(array)
    print str(count)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def bubbleSort(x_list, y):
  a = 0
  flag = 1
  while flag:
    flag = 0
    for i in range(1, y):
      j = y - i
      if x_list[j] < x_list[j - 1]:
        x_list[j], x_list[j - 1] = x_list[j - 1], x_list[j]
        flag = 1
        a += 1
  return a

y = sys.stdin.readline()
y = int(y)

x = sys.stdin.readline()
x_list = x.split(" ")

for i in range(y):
  x_list[i] = int(x_list[i])

a = bubbleSort(x_list, y)

for k in range(0, y):
  print x_list[k],
print
print a

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

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

flag = 1

cnt = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

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

numbers = list(map(int, input().split(" ")))

flag = 1

cnt = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

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

numbers = list(map(int, input().split(" ")))

flag = 1

cnt = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

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

numbers = list(map(int, input().split(" ")))

cnt = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

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

numbers = list(map(int, input().split(" ")))

cnt = 0

flag = 1

si = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

Solution (Python):
def bubble_sort(List):
	cnt=0
	l=len(List)
	for i in range(l):
		for j in range(l-1,i,-1):
			if List[j]<List[j-1]:
				temp=List[j]
				List[j]=List[j-1]
				List[j-1]=temp
				cnt+=1
	return cnt
def main():
	N=input()
	N_List=map(int,raw_input().split())
	cnt=bubble_sort(N_List)
	for k in range(len(N_List)):
		N_List[k]=str(N_List[k])
	print(" ".join(N_List))
	print(cnt)

if __name__=='__main__':
	main()

Solution (Python):




def print_array(a):
    print(" ".join(map(str, a)))


def bubblesort(a, n):
    swap = 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])
                swap += 1
                flag = True
    return (a, swap)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    (b, swap) = bubblesort(a, n)
    print_array(b)
    print(swap)

if __name__ == "__main__":
    main()

Solution (Python):


def bubble_sort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in xrange(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 A, count

def main():
    N = input()
    A = map(int, raw_input().split())
    A, count = bubble_sort(A, N)
    print " ".join(map(str,A))
    print count

main()

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

length = int(raw_input())
nums = map(int, raw_input().split())
ans = bubbleSort(nums, length)
print " ".join(map(str, ans[0]))
print ans[1]

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

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

print ' '.join([str(v) for v in a])
print nswap

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

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


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

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

bubble_sort(A, N)

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

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

N = int(input())
A = [int(i) for i in input().split()]
count = 0
bubbleSort(A, N)
print(*A)
print(count)

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

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

print(*a)
print(c)

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

    return a, c

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

    as_l, c  = bubbleSort(a_l, n)
    print(' '.join(map(str, as_l)))
    print(c)

Solution (Python):
def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in xrange(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
    return A, count

def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = bubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count

if __name__ == "__main__":
    main()

Solution (Python):
def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in xrange(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
    return A, count

def newbubbleSort(A, N):
    flag = 1
    count = 0
    i = 0
    while flag:
        flag = 0
        for j in xrange(N-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


def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = newbubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count

if __name__ == "__main__":
    main()

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

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

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

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

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

bubbleSort(A, N)

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

def bubble_sort(raw_list):
    n_list = len(raw_list)
    n_swap = 0
    sorted_i = 1
    flag = True
    while flag:
        flag = False
        for j in reversed(range(sorted_i, n_list)):
            if raw_list[j] < raw_list[j-1]:
                raw_list[j-1], raw_list[j] = raw_list[j], raw_list[j-1]
                n_swap += 1
                flag = True
        sorted_i += 1
    print(' '.join([str(i) for i in raw_list]))
    print(n_swap)
        
bubble_sort(data)

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

def bubble_sort(raw_list):
    n_list = len(raw_list)
    n_swap = 0
    sorted_i = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(sorted_i + 1, n_list)):
            if raw_list[j] < raw_list[j-1]:
                raw_list[j-1], raw_list[j] = raw_list[j], raw_list[j-1]
                n_swap += 1
                flag = True
        sorted_i += 1
    print(' '.join([str(i) for i in raw_list]))
    print(n_swap)
        
bubble_sort(data)

Solution (Python):
def trace(a,n) :
    li=[]
    for i in xrange(n):
        li.append(str(a[i]))
    print(" ".join(li))
    
def bubblesort(a,n):
    flag=1
    ct=0
    while flag:
        flag=0
        if n==0:
            break
        else :
            for i in xrange(n-1,0,-1):
                if a[i]<a[i-1]:
                    keep=a[i-1]
                    a[i-1]=a[i]
                    a[i]=keep
                    ct+=1
                    flag=1
    return ct
n=input()
a=map(int,raw_input().split())
ct=bubblesort(a,n)
trace(a,n)
print(ct)

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

Solution (Python):
N = int(raw_input())
A = map(int, raw_input().split())
i = 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]
            A[j] = A[j-1]
            A[j-1] = A_j
            flag = 1
            i += 1
print " ".join(map(str, A))
print i

Solution (Python):
N = int(raw_input())
A = map(int, raw_input().split())
i = 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]
            A[j] = A[j-1]
            A[j-1] = A_j
            flag = 1
            i += 1
print " ".join(map(str, A))
print i

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

def show(a, n):
    for i in range(n):
        if i == n-1:
            print "%d" % a[i]
        else:
            print "%d" % a[i],

n = input()
a = map(int, raw_input().split())
c = bubbleSort(a, n)
show(a, n)
print "%d" % c

Solution (Python):


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

c = 0

for i in range(a - 1):
    for j in range(a-1, i,-1):
        if N[j] < N[j - 1]:
            c+=1
            tmp = N[j]
            N[j] = N[j - 1]
            N[j - 1] = tmp

for i in range(a - 1):
    print(N[i],end=' ')
print(N[a-1]);
print(c);

Solution (Python):
def bubbleSort(R, N):
	flag = True
	cnt = 0
	while flag == True:
		flag = False
		for j in range(N - 1, 0, -1):
			if R[j] < R[j - 1]:
				tmp = R[j]
				R[j] = R[j - 1]
				R[j - 1] = tmp
				cnt = cnt + 1
				flag = 1
	print(" ".join(map(str, R)))
	return cnt

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

Solution (Python):


N = int(raw_input())
A = map(int, raw_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-1], A[j] = A[j], A[j-1]
            count += 1
            flag = 1
print " ".join(map(str, A))
print count

Solution (Python):
import math

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

flag = 1
count = 0

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

Solution (Python):
n = int(input())
a = list(map(int, 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]
            count += 1
            flag = True

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

Solution (Python):
data_num = int(input())
data = [int(n) for n in input().split(' ')]
data_len = len(data)

flag = True
i = 0
changed = 0

while flag:
    flag = False
    for j in range(data_len-1, i, -1):
        if data[j] < data[j-1]:
            data[j], data[j-1] = data[j-1], data[j]
            changed += 1
            flag = True
    i += 1
print(' '.join(str(i) for i in data))

print(changed)

Solution (Python):
def show_list(a):
    for x in a:
        print x,
    print

def bubble_sort(a,n):
    flag = True
    count = 0
    while flag:
        flag = False
        for j in reversed(xrange(1,n)):
            if a[j] < a[j-1]:
                tmp = a[j]
                a[j] = a[j-1]
                a[j-1] = tmp
                count+=1
                flag = True
    show_list(a)
    print count
def main():
    n = input()
    a = map(int,raw_input().split())
    bubble_sort(a,n)

if __name__ == '__main__':
    main()

Solution (Python):
def bubble_sort(array):
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True

def merge_count(array):
    if len(array) <= 1:
        return array, 0
    mid = len(array) // 2
    left, cl = merge_count(array[:mid])
    right, cr = merge_count(array[mid:])
    count = cl + cr
    left = list(reversed(left))
    right = list(reversed(right))
    for i in range(len(array)):
        if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
    return array, count

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    a, b = merge_count(A)
    print(*a)
    print(b)

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


N = input()
A = map(int, raw_input().split())
c = bubbleSort(A, N)
for i in A: print i,
print 
print c

Solution (Python):



def pnt(s):
 for i in xrange(len(s)):
  print s[i],
 print

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

sw=0
flag=True
while flag:
 flag=False
 for j in xrange(n-1,0,-1):
  if a[j]<a[j-1]:
   a[j],a[j-1]=a[j-1],a[j]
   flag=True
   sw+=1

pnt(a)
print sw

Solution (Python):
N = int(input())
a = [int(x) for x in input().split()]
def bubbleSort(A, N):
	flag = 1
	count = 0
	while flag:
		flag = 0
		for j in reversed(range(1,N)):
			if A[j] < A[j-1]:
				tmp = A[j]
				A[j] = A[j-1]
				A[j-1] = tmp
				flag = 1
				count += 1
	return A,count
ans,c = bubbleSort(a,N)
print(*ans)
print(c)

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

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

Solution (Python):

n = int(input())
num_list = input().split()

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

    result = ' '.join(num_list)
    print(result)
    print(count)

bubbleSort(num_list, n)

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

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

    result = ' '.join(num_list)
    print(result)
    print(count)

bubbleSort(num_list, n)

Solution (Python):
count = 0
list1 = []

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

for i in range(len(list1)):
  for n in range(len(list1) - i - 1):
    if(list1[n] > list1[n + 1]):
      tmp = list1[n]
      list1[n] = list1[n + 1]
      list1[n + 1] = tmp
      count += 1

print(" ".join(str(x) for x in list1))
print(count)

Solution (Python):


x = int(input())
y = list(map(int,input().split()))
z = 0

flag = 1
while flag:
	flag = 0
	i = x - 1
	while i > 0:
		if y[i] < y[i - 1]:
			y[i], y[i - 1] = y[i - 1], y[i]
			flag = 1
			z += 1
		i -= 1

print(" ".join(list(map(str,y))))
print(z)

Solution (Python):

sample_input = list(range(3))
sample_input[0] = '''5
5 3 2 4 1'''
sample_input[1] = '''6
5 2 4 6 1 3'''
sample_input[2] = '''1
100'''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        

def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp

num_of_data = int(input())
list_of_data = list(map(int, input().split()))

swap_count = 0
flag = True
while flag:
    flag = False
    n = num_of_data - 1
    while n >= 1:
        if list_of_data[n] < list_of_data[n-1]:
            swap_list_item(list_of_data, n, n-1)
            flag = True
            swap_count += 1
        n -= 1

result = ''
for number in list_of_data:
    result += str(number) + ' '
print(result.strip())
print(swap_count)

Solution (Python):



def bubble_sort(a, N):
    change = 0
    for i in xrange(N - 1):
        for j in xrange(N - 1, i, -1):
            if a[j - 1] > a[j]:
                temp = a[j - 1]
                a[j - 1] = a[j]
                a[j] = temp
                change += 1
    return (a, change)


if __name__ == "__main__":
    N = input()
    a = map(int, raw_input().split())
    result, change = bubble_sort(a, N)
    print ' '.join(map(str, result))
    print change

Solution (Python):
n = int(input())
A = input().split(' ')
count = 0

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

print(" ".join(A))
print(count)

Solution (Python):
n = int(input())
A = input().split(' ')
count = 0

flag = True
while flag:
    flag = False
    i = 0

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

    i += 1


print(" ".join(A))
print(count)

Solution (Python):
def main():
  element_num = int(input())
  elements = [int(s) for s in input().split()]
  swap_count = 0

  for i in range(1, element_num):
    for j in reversed(range(i, element_num)):
      if elements[j-1] > elements[j]:
        elements[j-1], elements[j] = elements[j], elements[j-1]
        swap_count += 1

  print(' '.join(list(map(str, elements))))
  print(swap_count)
  return 0

if __name__ == '__main__':
  main()

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

def bubbleSort(A, N):
    count = 0
    flag = 1
    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
    return A, count

A, count = bubbleSort(A, N)
print(' '.join([str(i) for i in A]))
print(count)

Solution (Python):
def bubble_sort(array):
    isEnd = False
    count_swap = 0
    while isEnd is False:
        isEnd = True
        for j in reversed(range(1,len(array))):
            if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
    return count_swap

def print_array(array):
     print(str(array)[1:-1].replace(', ', ' '))

def main():
    N = int(input())
    array = [int(s) for s in input().split(' ')]
    count = bubble_sort(array)
    print_array(array)
    print(count)

if __name__ == '__main__':
    main()

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]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
    return count


def main():
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    count = bubbleSort(a, n)
    print(' '.join([str(x) for x in a]))
    print(count)

if __name__ == '__main__':
    main()

Solution (Python):

n = int(input())
A = list(map(int, input().split()))
c = 0
for i in range(n-1):
    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
print(" ".join(map(str, A)))
print(c)

Solution (Python):
length = int(input())
targ = [int(n) for n in input().split(' ')]
swap = 0
for l in range(length):
    for m in range(l):
        if targ[l - m] < targ[l - m - 1]:
            disp = targ[l-m -1]
            targ[l-m-1] = targ[l-m]
            targ[l-m] = disp
            swap += 1
print(" ".join([str(n) for n in targ]))
print(swap)

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

cnt = 0

for i in range(0, 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]
            cnt = cnt + 1

print(' '.join([str(item) for item in A]))
print(cnt)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split(" ")]
swop = 0
for i, a in enumerate(A):
    j = N - 1
    while j >= i + 1:
        if A[j] < A[j - 1]:
            tmp = A[j]
            A[j] = A[j - 1]
            A[j - 1] = tmp
            swop += 1
        j -= 1
print(" ".join([str(a) for a in A]))
print(swop)

Solution (Python):


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


input_num = int(input())
num_list = [int(i) for i in input().split()]

bubble_list, swap_num = bubbleSort(num_list)
for i in range(len(num_list)):
    if i > 0:
        print(" ", end="")
    print(num_list[i], end="")
print()
print(swap_num)

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
cnt = 0

flag = True

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

print(*li)
print(cnt)

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

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

def swap(a, b):
    t = a
    a = b
    b = t
    return [a, b]

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

l, count = bubble_sort(A)
print(' '.join(str(x) for x in l))
print(count)

Solution (Python):
import sys

def bubble_sort(n, data):
    flag = True
    i = 0
    cnt = 0

    while flag:
        flag = False
        
        for j in range(n-1, i, -1):
            if data[j] < data[j-1]:
                data[j], data[j-1] = data[j-1], data[j]
                cnt += 1
                flag = True

        i += 1

    print(' '.join([str(i) for i in data]))
    print(cnt)
        
def main():
    n = int(sys.stdin.readline().strip())
    data = [int(i) for i in sys.stdin.readline().strip().split(' ')]
    bubble_sort(n, data)
    
if __name__ == '__main__':
    main()

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

counter = 0
while True:
  ischanged = False
  for i in xrange(1, N):
    if A[N-i] < A[N-i-1]:
      tmp = A[N-i]
      A[N-i] = A[N-i-1]
      A[N-i-1] = tmp
      counter += 1
      ischanged = True
  if ischanged == False:
    break
print " ".join(map(str, A))
print counter

Solution (Python):
length = int(input())
eles = [int(l) for l in input().split()]

times = 0
for i in range(length):
    flag = 1
    while flag > 0:
        flag = 0
        for j in range(length-1, 0, -1):
            if eles[j] < eles[j - 1]:
                _temp = eles[j]
                eles[j] = eles[j-1]
                eles[j-1] = _temp
                flag = 1
                times += 1

print(*eles)
print(times)

Solution (Python):
length = int(input())
eles = [int(l) for l in input().split()]

times = 0
for i in range(length):
    flag = 1
    while flag > 0:
        flag = 0
        for j in range(length-1, i, -1):
            if eles[j] < eles[j - 1]:
                _temp = eles[j]
                eles[j] = eles[j-1]
                eles[j-1] = _temp
                flag = 1
                times += 1

print(*eles)
print(times)