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] 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



Sample input:
5
5 3 2 4 1


Sample output:
1 2 3 4 5
8

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
def bubblesort(N, A):
    """Do BubbleSort"""
    m = 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]
                m += 1
                A[i] = tmp
                flag = True
    b = list(map(str, A))
    print(" ".join(b))
    print(m)

A = []
N = int(input())
s = input()
A = list(map(int, s.split()))
bubblesort(N, A)

Submission status: Accepted

Solution code:
n = int(input())
m = list(map(int, input().split()))

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

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

Submission status: Accepted

Solution code:
#coding:UTF-8
def BS(N,A):
    count=0
    flag=True
    while flag==True:
        flag=False
        for i in range(1,N):
            if A[N-i]<A[N-i-1]:
                swap=A[N-i]
                A[N-i]=A[N-i-1]
                A[N-i-1]=swap
                flag=True
                count+=1
    for i in range(len(A)):
        A[i]=str(A[i])
    print(" ".join(A))
    print(count)

if __name__=="__main__":
    N=int(input())
    a=input()
    A=a.split(" ")
    for i in range(len(A)):
        A[i]=int(A[i])
    BS(N,A)

Submission status: Accepted

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

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

print(' '.join([str(A[i]) for i in range(0, N)]))
print(counter)

Submission status: Accepted

Solution code:
N = int(raw_input())
nums = map(int, raw_input().split(" "))

count = 0
for i in range(0, len(nums)):
    for j in range(len(nums)-1, i, -1):
        if nums[j-1] > nums[j]:
            temp = nums[j-1]
            nums[j-1] = nums[j]
            nums[j] = temp
            count += 1

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
f = 1 
c = 0

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# encoding: utf-8


class Solution:
    """
    @param prices: Given an integer array
    @return: Maximum profit
    """

    @staticmethod
    def bubble_sort():
        # write your code here
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

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


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# encoding: utf-8


class Solution:

    @staticmethod
    def bubble_sort():
        # write your code here
        array_length = int(input())
        array = [int(x) for x in input().split()]

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


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
#-*- coding:utf-8 -*-

N = int(raw_input())

A = map(int,raw_input().split())
counter = 0 
for i in range(len(A)):
    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]
            counter += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
A = map(int, raw_input().split())
ans = 0
flag = True
while flag:
    flag = False
    for j in xrange(N-1, 0, -1):
        if A[j] < A[j-1]:
            ans += 1
            A[j], A[j-1] = A[j-1], A[j]
            flag = True
print ' '.join(map(str, A))
print ans

Submission status: Accepted

Solution code:
n = input()
lst = map(int, raw_input().split())
flag = True
cnt=0
while flag:
    flag = False
    for j in range(n-1,0,-1):
        if lst[j] < lst[j-1]:
            tmp=lst[j-1]
            lst[j-1] = lst[j]
            lst[j] = tmp
            flag = True
            cnt+=1

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

Submission status: Accepted

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_A&lang=jp
#??????????????????
#?????????????´?????¢????????????£????????????????????????????????¨???????¢?????????¨
#?????°??¢??°???????¨???????????¢????????????????

def bubble_sort(target_list):
    list_length = len(target_list)
    
    flag = True
    change_count = 0
    top_index = 1
    while flag:
        flag = False
        for i in range(top_index, list_length)[::-1]:
            if target_list[i] < target_list[i - 1]:
                tmp = target_list[i]
                target_list[i] = target_list[i - 1]
                target_list[i - 1] = tmp
                change_count += 1
                flag = True
        top_index += 1
        
    return change_count

def main():
    n_list = int(input())
    target_list = [int(n) for n in input().split()]
    cc = bubble_sort(target_list)
    print(*target_list)
    print(cc)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

def print_array(A, N):
    for i in xrange(N - 1):
        print str(A[i]),

    print A[N - 1]


def bubbleSort(A, N):
    swap_num = 0
    flag = 1
    while flag == 1:
        flag = 0
        for j in xrange(N - 1, 0, -1):
            if A[j] < A[j - 1]:
                swap_num += 1

                temp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = temp
                flag = 1

    return swap_num


def main():
    N = int(raw_input())
    A = []

    for num in raw_input().split():
        A.append(int(num))

    temp = bubbleSort(A, N)
    print_array(A, N)

    print temp


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def bubble_sort(numbers, n):
    """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)

    Returns:
        sorted list
    """
    flag = True
    counter = 0
    while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if numbers[index] < numbers[index - 1]:
                numbers[index], numbers[index - 1] = numbers[index - 1], numbers[index]
                flag = True
                counter += 1
    return numbers, counter


n = int(raw_input())
numbers = [int(x) for x in raw_input().split()]
numbers, swapped_numbers = bubble_sort(numbers, n)

print(" ".join([str(x) for x in numbers]))
print(swapped_numbers)

Submission status: Accepted

Solution code:
N=input()
A=map(int,raw_input().split())
num=0
flag=1
while flag:
	flag=0
	for j in reversed(range(N-1)):
		if A[j+1]<A[j]:
			A[j+1],A[j]=A[j],A[j+1]
			flag=1
			num+=1

for m in range(N-1):
	print str(A[m]),
print A[N-1]
print num

Submission status: Accepted

Solution code:
def bubble_sort(data, n):
    """ ??°???data??????????????????????????????????????§???????????????????????????n?????°????????????
    ?????????????????????????????°??¨????????????????????????????????????
    """
    swap_count = 0
    flag = True
    i = 0
    while flag is True:
        flag = False
        for j in range(n-1, i, -1):
            if data[j] < data[j-1]:
                temp = data[j]
                data[j] = data[j-1]
                data[j-1] = temp
                swap_count += 1
                flag = True
        i == 1
    return swap_count, data


if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = [int(x) for x in input().split(' ')]
    # data = [5, 3, 2, 4, 1]

    # ???????????????????????????
    count, data = bubble_sort(data, num)

    # ???????????????
    print('{0}\n{1}'.format(' '.join(map(str, data)), count))

Submission status: Accepted

Solution code:
#! /usr/bin/env python
#-*- coding: utf-8 -*-

''' ?????????????????? '''

# O(n^2)


def bubble_sort(A, N):
    swap_num = 0  # ?????????????????°

    for i in range(N - 1):
        for j in range(N - 1, i, -1):
            if A[j] < A[j - 1]:
                # swap
                temp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = temp
                swap_num += 1

    return (A, swap_num)


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    # A = [5, 2, 4, 6, 1, 3]
    # N = len(A)
    array_sorted, swap_num = bubble_sort(A, N)
    print(' '.join(map(str, array_sorted)))
    print(swap_num)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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


if __name__ == '__main__':
    n = int(input())
    a = [int(n) for n in input().split()]
    ans = bubble_sort(n, a)
    print(' '.join(map(str, ans[0])))
    print(ans[1])

Submission status: Accepted

Solution code:
def buble_sort(seq):
    l = len(seq)
    cnt = 0
    for i in range(l):
        for j in range(i+1, l)[::-1]:
            if seq[j] < seq[j-1]:
                seq[j], seq[j-1] = seq[j-1], seq[j]
                cnt += 1
    return seq, cnt


n = int(input())
a = list(map(int, input().split()))
sorted_a, num_swap = buble_sort(a)
print(' '.join(map(str, sorted_a)))
print(num_swap)

Submission status: Accepted

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

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

print(" ".join(map(str, A)))
print(num_of_swp)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

def show_list(list):
    i = 0;
    while(i < len(list)-1):
        print(list[i], end=" ")
        i = i + 1
    print(list[i])

num = int(input())
num_list = [int(x) for i, x in enumerate(input().split()) if i < num]
num_list, swap_count = bubble_sort(num_list)
show_list(num_list)
print(swap_count)

Submission status: Accepted

Solution code:

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

def bubbleSort(A, N):
    flag = 1
    exchange_counter = 0
    while flag:
        flag = 0
        for i in range(1, N):
            if A[i] < A[i-1]:
                A[i], A[i-1] = A[i-1], A[i]
                flag = 1
                exchange_counter += 1
    print(" ".join(map(str, A)))
    print(exchange_counter)

bubbleSort(num_array, num_length)

Submission status: Accepted

Solution code:
def bubble_sort(array):
	N = len(array)
	count = 0
	
	for i in range(N - 1):
		flag = 0
		
		for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		if flag == 0:
			break
	
	return array, count
	
n = int(input())
array = [int(i) for i in input().split()]
result = bubble_sort(array)
print(*result[0])
print(result[1])

Submission status: Accepted

Solution code:
_, vs, c = input(), list(map(int, input().split())), 0
for i in range(len(vs)):
    for j in range(len(vs)-1, i, -1):
        if vs[j] < vs[j-1]:
            vs[j], vs[j-1] = vs[j-1], vs[j]
            c += 1
print(' '.join(map(str, vs)))
print(c)

Submission status: Accepted

Solution code:
def bubble_sort(r, n):
    flag = True  # ??£??\????´?????????¨????????°
    count = 0
    while flag:
        flag = False
        for i in range(n-1, 0, -1):
            if r[i-1] > r[i]:
                r[i-1],r[i] = r[i],r[i-1]
                count += 1
                flag = True

    print(*r)
    print(count)

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

Submission status: Accepted

Solution code:

def BubbleSort(A, N):
    cnt = 0 # swap???????????£????????°????¨???????????????°
    flag = 1 # 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]
                cnt += 1
                flag = 1

    # swap???????????£????????°?????????
    return cnt


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

cnt = BubbleSort(A, N)

print(*A)
print(cnt)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    swap_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
                swap_count += 1
    return swap_count

n = int(input())
A = list(map(int, input().split()))
count = bubbleSort(A,n)
print(*A)
print(count)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
def bubbleSort(A,N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in range(N-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
N = int(input())
A = input()
A = A.split()
for i in range(len(A)):
    A[i] = int(A[i])
    
A,count = bubbleSort(A,N)
for i in range(len(A)-1):
    print(A[i],end = ' ')
print(A[len(A)-1])
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(target):
    global cnt
    flag = True
    while flag:
        flag = False
        for i in range(len(target)-1, 0, -1):
            if target[i] < target[i-1]:
                target[i-1], target[i] = target[i], target[i-1]
                flag = True
                cnt += 1
    return target

N = int(input())
A = [int(s) for s in input().split()]
cnt = 0
print(' '.join(str(s) for s in bubble_sort(A)))
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def bubble_sort(seq, l):
    cnt = 0

    for max_size in range(l, 0, -1):
        for left in range(1, max_size):
            if seq[left-1] > seq[left]:
                seq[left-1], seq[left] = seq[left], seq[left-1]
                cnt += 1

    print(' '.join([str(i) for i in seq]))
    print(cnt)


def to_int(v):
    return int(v)


def to_seq(v):
    return [int(c) for c in v.split()]


if __name__ == '__main__':
    l = to_int(input())
    seq = to_seq(input())

    bubble_sort(seq, l)

Submission status: Accepted

Solution code:
def bubble_sort(A):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, 0, -1):
            if A[i - 1] > A[i]:
                A[i - 1], A[i] = A[i], A[i - 1]
                flag = 1
                count += 1
    return count


def show(A):
    for i in range(len(A) - 1):
        print(A[i], end=" ")
    print(A[len(A) - 1])


n = int(input())
line = input()
A = list(map(int, line.split()))
count = bubble_sort(A)
show(A)
print(count)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    flag = True
    count = 0
    while flag:
        flag = False
        for i in range(N-1,0,-1):
            if A[i] < A[i-1]:
                count += 1
                A[i], A[i-1] = A[i-1], A[i]
                flag = True
    print(" ".join(map(str,A)))
    print(count)


if __name__ == "__main__":
    import os
    import sys
    if len(sys.argv) > 1:
        if sys.argv[1] == "-d":
            fd = os.open("input.txt", os.O_RDONLY)
            os.dup2(fd, sys.stdin.fileno())
            main()
    else:
        main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_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]:
                temp = A[i]
                A[i] = A[i-1]
                A[i-1] = temp
                flag = 1
                count += 1
    return count

count = bubbleSort(A, N)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n = int(input())

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

def bubble(list):
    flag = 1
    count = 0
    while flag == 1:
        flag = 0
        for i in range(len(list)-1):
            if list[len(list)-1-i] < list[len(list)-1-i-1]:
                x = list[len(list)-i-1]
                list[len(list)-1-i] = list[len(list)-1-i-1]
                list[len(list)-1-i-1] = x
                flag = 1
                count += 1
    return [list,count]

answer = bubble(list)
for i in range(len(answer[0])):
    print(str(answer[0][i]) + ' ',end='') if i != len(answer[0])-1 else print(answer[0][i])
print(answer[1])

Submission status: Accepted

Solution code:
def bubbleSort():
    N = int(input())
    lst = list(map(int, input().split()))
    
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
    print(*lst)
    print(count)

bubbleSort()

Submission status: Accepted

Solution code:
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 swap
N = int(input())
A = list(map(int,input().split()))
swap=bubbleSort(A,N)
print(" ".join(map(str,A)))
print (swap)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
def bubblesort(a):
    count = 0
    swap = True
    while swap:
        swap = False
        for j in range(1,len(a))[::-1]:
            if a[j] < a[j-1]:
                ret = a[j]
                a[j] = a[j-1]
                a[j-1] = ret
                count += 1
                swap = True           
    print(" ".join(map(str,a)))
    print(count)
    
n = int(input())
a = list(map(int,input().split()))
bubblesort(a)

Submission status: Accepted

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

flag = 1
counter = 0
while flag:
	flag = 0
	for i in reversed(range(1,N)):
		if A[i] < A[i-1]:
			A[i], A[i-1] = A[i-1], A[i]
			counter += 1
			flag = 1
print(" ".join(map(str, A)))
print(counter)

Submission status: Accepted

Solution code:
N = int(input())

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

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

print((" ").join((str(a) for a in A)))
print(num_c)

Submission status: Accepted

Solution code:
def bubbleSort(n,A):
    flag = True
    cnt = 0
    while flag:
        flag = False
        for j in range(n-1, 0, -1):
            if A[j-1] > A[j]:
                A[j-1],A[j] = A[j],A[j-1]
                cnt += 1
                flag = True
    print(*A)
    print(cnt)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
frag = 1
i = 0
count = 0
while frag:
    frag = 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
        frag = 1
        
    i += 1

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

Submission status: Accepted

Solution code:
data_num = int(input())
nums = list(map(int, input().split(" ")))
cnt = 0
for i in range(data_num):
    for j in range(data_num-1, i,-1):
        if nums[j]<nums[j-1]:
            nums[j], nums[j-1] = nums[j-1], nums[j]
            cnt = cnt + 1

print(" ".join(map(str,nums)) + "\n" + str(cnt))

Submission status: Accepted

Solution code:
n = int(input())
sequence = [int(x) for x in input().split(' ')]

def pretty_sequence(sequence):
    n = len(sequence)
    for i in range(n-1):
        print(format(sequence[i]) + ' ', end='')
    print(sequence[n - 1])

def bubleSort(n, sequence):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if sequence[i] < sequence[i-1]:
                tmp_val = sequence[i]
                sequence[i] = sequence[i-1]
                sequence[i-1] = tmp_val
                count += 1
                flag = 1
    return sequence, count

sequence, count = bubleSort(n, sequence)
pretty_sequence(sequence)
print(count)