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 print_list(r):
    for i in range(len(r)):
        if i != len(r)-1:
            print (r[i], end=' ')
        else:
            print(r[i])

n=int(input())
r=list(map(int,input().split()))
flag = 1
change_num = 0
while flag:
    flag = 0
    for j in range(n-1, 0, -1):
        if r[j] < r[j-1]:
            v = r[j]
            r[j] = r[j-1]
            r[j-1] = v
            flag = 1
            change_num += 1
print_list(r)
print(change_num)

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


def bubbleSort(list_a, n):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for j in list(range(1,n))[::-1]:
            if list_a[j] < list_a[j-1]:
                (list_a[j], list_a[j-1]) = (list_a[j-1], list_a[j])
                count += 1
                flag = 1
    return list_a,count
list_n,count = bubbleSort(list_n, n)
print(" ".join(str(number) for number in list_n))
print(count)

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

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

ns, sa = BubbleSort(a)
print(' '.join(map(str, sa)))
print(ns)

Solution (Python):

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

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

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

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

    ans = 0

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

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

main()

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

if n <= 0 or 1000 <= n:
  exit()
list1 = list(map(int, input().split()))

for i in range(n-1, 0, -1):
  j = i
  while (j < n) and (list1[j] < list1[j-1]):
    tmp = list1[j-1]
    list1[j-1] =  list1[j]
    list1[j] = tmp
    j += 1
    a += 1
print(' '.join(map(str, list1)))
print(a)

Solution (Python):
N = int(input())
Numbers = list(map(int, input().split()))
Flag = True
count = 0
while Flag:
    Flag = False
    for j in range(N - 1, 0, -1):
        if Numbers[j] < Numbers[j - 1]:
            Numbers[j], Numbers[j - 1] = Numbers[j - 1], Numbers[j]
            count += 1
            Flag = True
print(' '.join(list(map(str, Numbers))))
print(count)

Solution (Python):


n=int(input())
*l,=map(int,input().split())
flag=0

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

Solution (Python):
n = int(input())
a = [int(_) for _ in input().split()]
neighbor = True
change_count = 0
while neighbor:
    neighbor = False
    for i in range(n-1, 0, -1):
        if a[i] < a[i-1]:
            change_count += 1
            a_tmp = a[i]
            a[i] = a[i-1]
            a[i-1] = a_tmp
            neighbor = True
for i in range(n-1):
    print(a[i], end=" ")
print(a[n-1])
print(change_count)

Solution (Python):



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


def insertion(A, N): 
    flag = 1 
    i = 0 
    counter = 0
    while flag == 1:
        flag = 0
        if i != N:
            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
                    flag = 1
                    counter += 1
            i += 1
        map_list = map(str, A)
        a = ' '.join(map_list)
    print(a)
    print(counter)

insertion(A, N)

Solution (Python):
def main():
    input()
    times = 0
    sequence = list(map(int, input().split()))
    for i in range(len(sequence)):
        for j in range(len(sequence)-1, i, -1):
            if sequence[j-1] > sequence[j]:
                tmp = sequence[j]
                sequence[j] = sequence[j-1]
                sequence[j-1] = tmp
                times += 1
    print(' '.join(map(str, sequence)))
    print(times)


if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
n = 0
dat = []

swap = 0


def input_file():
    global n, A
    f = open('input', mode='r')
    n = int(f.readline())
    *A, = map(int, f.readline().split(' '))


def input_cmd():
    global n, A
    n = int(input())
    *A, = map(int, input().split(' '))


input_cmd()



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








print(*A)
print(ans)

Solution (Python):
def b_sort(n, l):
    cn = 0
    for i in range(n):
        for j in range(n-1, i, -1):
            if l[j] < l[j-1]:
                l[j], l[j-1] = l[j-1], l[j]
                cn += 1

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


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

Solution (Python):
cnt = int(input())
numbers = list(map(int, input().split(' ')))


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

    return A, swap_cnt


numbers, swap_cnt = bubblesort(numbers, cnt)
print(' '.join(map(str, numbers)))
print(swap_cnt)

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

_, array = input(), input().split()

result, count = bubble_sort(array)

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

Solution (Python):
def BubbleSort(A):
    n = len(A)
    ans = 0
    for i in range(n):
        for j in range(n-1, i, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                ans += 1
    print(' '.join([str(i) for i in A]))
    print(ans)

_ = input()
BubbleSort([int(i) for i in input().split()])

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

r_exists = True
s_count = 0

while r_exists == True:

    r_exists = False

    i = N - 1

    while i >= 1:

        if A[i] < A[i - 1]:
            
            A[i], A[i - 1] = A[i - 1], A[i]

            s_count += 1

            r_exists = True
        
        i -= 1

s = ''

for x in A:

    s = s + str(x) + ' '

print(s.rstrip())
print(s_count)

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

while flag:
    flag = 0
    for j in range(N-1, 0, -1):
        if A[j - 1] > A[j]:
            A[j - 1], A[j] = A[j], A[j - 1]
            swap_counter += 1
            flag = 1
A_str = [str(a) for a in A]
result = ' '.join(A_str)
print(result)
print(swap_counter)

Solution (Python):
def bubble_sort(nums, n):
    c = 0

    loop = True
    while loop:
        loop = False

        for j in range(n-1, 0, -1):
            if nums[j] < nums[j-1]:
                nums[j], nums[j-1] = nums[j-1], nums[j]
                loop = True
                c += 1

    return (c, nums)

def main():
    n = int(input())
    nums = [int(x) for x in input().split()]

    c, nums = bubble_sort(nums, n)

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

if __name__ == '__main__':
    main()

Solution (Python):


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


flag = 1
i = 0  
count = 0
while flag:
    flag = 0

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

print(*A)
print(count)

Solution (Python):
N = int(input())
*A, = list(map(int, input().split()))
flg = 1
cnt = 0
while flg:
    flg = 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
            flg = 1
            cnt += 1
print(*A)
print(cnt)

Solution (Python):
def bubble_sort(lis, len):
    flag = 1
    times = 0
    while flag:
        flag = 0
        for i in range(len - 1, 0, -1):
            if lis[i] < lis[i - 1]:
                ele = lis[i]
                lis[i] = lis[i - 1]
                lis[i - 1] = ele
                times += 1
                flag = 1
    return times


inputs = []
while True:
    try:
        inputs.append(input())
    except(EOFError):
        break

ln = int(inputs[0])
li = list(map(int, inputs[1].split()))

t = bubble_sort(li, ln)

print(*li)
print(t)

Solution (Python):
def bubbles(N):
    count = 0
    for i in range(len(N)):
        for j in range(len(N)-1, i, -1):
            if N[j] < N[j-1]:
                N[j], N[j-1] = N[j-1], N[j]
                count += 1
    c = 1
    for i in N:
        print(i, end='')
        if c < len(N):
            print(' ', end='')
            c += 1
    print('')
    return count
 
n = int(input())
numbers = list(map(int, input().split()))
 
print(bubbles(numbers))

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

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

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

Solution (Python):
n = int(input())
s = input()
s = s.split(" ")
for i in range(n):
    s[i] = int(s[i])
flag = 1
count = 0
while flag:
    flag = 0
    for i in range(1,n):
        if s[i] < s[i-1]:
            v = s[i]
            s[i] = s[i-1]
            s[i-1] = v
            flag = 1
            count += 1
for i in range(n-1):
    print(s[i],end = " ")
print(s[n-1])
print(count)

Solution (Python):

def bubble_sort(A,N):
    count = 0
    flag = 1 
    while flag:
        flag = 0
        for j in range(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
                count += 1
    return count
N = int(input())
A = [int(i) for i in input().split(' ')]
c = bubble_sort(A,N)
print(' '.join(list(map(str, A))))
print(c)