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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#ALDS1_2_A

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

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

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

Submission status: Accepted

Solution code:
array_to_str = lambda A: ' '.join([str(a) for a in A])
input_number = lambda: int(input())
input_num_array = lambda: [int(a) for a in input().split()]

def bubble_sort(A, N):
  flag = 1
  exchange_num = 0
  while flag:
    flag = 0
    for j in range(N)[:0:-1]:
      if A[j] < A[j-1]:
        exchange_num += 1
        A[j],A[j-1] = A[j-1], A[j]
        flag = 1
  print(array_to_str(A))
  print(exchange_num)
  
N = input_number()
A = input_num_array()
bubble_sort(A, N)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
i = 0

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

print(str(li).replace(',', '')[1:-1])
print(i)

Submission status: Accepted

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

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

Submission status: Accepted

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

L = A[:]
sorted_l = []
un_sorted_l = L[:]
counter = 0
while un_sorted_l:
    n = len(un_sorted_l)
    for i in reversed(range(1, n)):
        if un_sorted_l[i-1] <= un_sorted_l[i]:
            continue
        else:
            counter += 1
            un_sorted_l[i-1], un_sorted_l[i] = un_sorted_l[i], un_sorted_l[i-1]
    sorted_l.append(un_sorted_l[0])
    un_sorted_l = un_sorted_l[1:]


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

Submission status: Accepted

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

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

print(*A)
print(cnt)

Submission status: Accepted

Solution code:
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]:
      c += 1
      a[j],a[j-1] = a[j-1],a[j]
print(' '.join(map(str,a)))
print(c)

Submission status: Accepted

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

swap_count = 0
for i in range(0, n):
    for j in reversed(range(i + 1, n)):
        if a_list[j - 1] > a_list[j]:
            temp = a_list[j]
            a_list[j] = a_list[j - 1]
            a_list[j - 1] = temp
            swap_count = swap_count + 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#疑似コード読み違えた
N=int(input())
L=input().split( )
A=[int(L[i]) for i in range(N)]
c=0
j=N-1
flag=1
while flag:
    flag=0
    for j in range(N-1):
        if A[j+1]<A[j]:
            temp=A[j]
            A[j]=A[j+1]
            A[j+1]=temp
            c+=1
            flag=1
for j in range(N):
    if j<N-1:
        print(A[j],end=" ")
    else:
        print(A[j])
print(c)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3 
# coding=utf_8

n = int(input())
adata = input().split()
data = list(map(int, adata))
count = 0

flag = True
while flag:
    flag = False
    for i in range(len(data)-1,0,-1):
        if data[i] < data[i-1]:
            tmp = data[i]
            data[i] = data[i-1]
            data[i-1] = tmp
            count +=1
            flag = True
ans = ""
for s in data:
    if ans != "":
        ans = ans+ " "
    ans = ans + str(s)



print(ans)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
flg=True
counter=0
while flg:
    flg=False
    for j in reversed(range(1,n)):
        if l[j]<l[j-1]:
            l[j],l[j-1]=l[j-1],l[j]
            flg=True
            counter+=1
for i in range(n):
    if i!=n-1:
        print(l[i],end=' ')
    else:
        print(l[i])
print(counter)

Submission status: Accepted

Solution code:
n=int(input())
da=list(map(int,input().split()))
chco=0
flag=1
while flag:
    flag=0
    for i in range(n-1,0,-1):
        if da[i]<da[i-1]:
            da[i],da[i-1]=da[i-1],da[i]
            flag=1
            chco+=1
for i in range(n):
    if i==n-1:
        print(da[i])
    else:
        print(da[i],end=' ')
print(chco)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(lambda x:int(x),input().split()))
flag=True
number=0
while flag:
    flag=False
    for j in range(n-1,0,-1):
        if a[j]<a[j-1]:
            a[j],a[j-1]=a[j-1],a[j]
            number=number+1
            flag=True

for k in range(n-1):
    print(a[k],end=" ")
print(a[n-1])
print(number)

Submission status: Accepted

Solution code:
def bublleSort(A):
    cnt = 0

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

    return A, cnt


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

A_sorted, cnt = bublleSort(A)

print(*A_sorted)
print(cnt)

Submission status: Accepted

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

N = int(input())
a = list(map(int, input().split()))
ans, count = bubbleSort(a, N)
print(' '.join(map(str, a)))
print(count)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	n = int(input())
	num = [int(i) for i in input().split()]
	
	# bubble sort
	flag = 1
	count = 0
	while flag != 0:
		flag = 0
		rev = list(range(1,n))
		rev.reverse()
		for j in rev:
			if num[j] < num[j-1]:
				tmp = num[j]
				num[j] = num[j-1]
				num[j-1] = tmp
				flag = 1
				count += 1
	for i in range(n):
		if i != n-1:
			print("%d" % (num[i]),end=" ")
		else:
			print("%d" % (num[i]))
	print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
flag = 1
counter = 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]
            counter += 1
            flag = 1
print(' '.join(map(str, A)))
print(counter)

Submission status: Accepted

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

num = 0
for i in range(1, N):
  while A[i] < A[i-1] and i-1 >= 0:
    temp = A[i-1]
    A[i-1] = A[i] 
    A[i] = temp
    i -= 1
    num += 1
 
print(*A)
print(num)

Submission status: Accepted

Solution code:
num = int(input())
line = list(map(int, input().split()))
count = 0
for i in range(1,num):
    for j in range(i):
        if line[i-j] < line[i-j-1]:
            a = line[i-j]
            line[i-j] = line[i-j-1]
            line[i-j-1] = a
            count+= 1
        else:
            break
line_str = [str(k) for k in line]
print(' '.join(line_str))
print(count)

Submission status: Accepted

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

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

string = ''
for s_val in a[:-1]:
    string += str(s_val) + ' '
string += str(a[-1])
print(string)
print(count)

Submission status: Accepted

Solution code:
#ALDS1_2_A
n = int(input())
a = [int(i) for i in input().split()]
cnt = 0
for ni in range(n):
  for ii in range(n-1,ni,-1):
    if a[ii] < a[ii-1]:
      a[ii], a[ii-1] = a[ii-1], a[ii]
      cnt += 1
print(" ".join([str(i) for i in a]))
print(cnt)

Submission status: Accepted

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

def bubbleSort(A, N):
    counter = 0
    i = 0
    flag = 1
    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
                counter += 1
        i += 1
    print(" ".join(map(str, A)), counter, sep="\n")
bubbleSort(A, N)

Submission status: Accepted

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

print(" ".join([str(i) for i in A]))
print(ans)

Submission status: Accepted

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

cnt = 0
for i in range(len(s)-1):
    for j in reversed(range(i+1,len(s))):
        if(s[j-1] > s[j]):
            v = s[j]
            s[j] = s[j-1]
            s[j-1] = v
            cnt += 1
            
print(" ".join(map(str,s)))
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().strip().split()))
rev = 0
counter = 0
while(rev >= 0):
    for i in range(1, N):
        if(a[i-1] > a[i]):
            a[i-1], a[i] = a[i], a[i-1]
            rev += 1
            counter += 1
    if(rev == 0):
        for j in range(0, N-1):
            print(a[j], end=" ")
        print(a[N-1])
        print(counter)
        break
    rev = 0

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().strip().split()))
rev = 0
counter = 0
while(rev >= 0):
    for i in range(1, N):
        if(a[i-1] > a[i]):
            a[i-1], a[i] = a[i], a[i-1]
            rev += 1
            counter += 1
    if(rev == 0):
        for j in range(0, N-1):
            print(a[j], end=" ")
        print(a[N-1])
        print(counter)
        break
    rev = 0

Submission status: Accepted

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

flag = 1
count = 0
while flag == 1:
  flag = 0
  for i in range(N - 1, 0, -1):
    if int(list[i]) < int(list[i - 1]):
      tmp = list[i]
      list[i] = list[i - 1]
      list[i - 1] = tmp
      count += 1
      flag = 1
print(' '.join(list))
print(count)

Submission status: Accepted

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

c = 0
flag = 1
while flag:
    flag=0
    for j in reversed(range(1, n)):
        if a[j] < a[j-1]:
            b = a[j]
            a[j] = a[j-1]
            a[j-1] = b
            flag = 1
            c += 1
print(*a)
print(c)

Submission status: Accepted

Solution code:
num = int(input())
x = list(map(int, input().split()))
n = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


def bubbleSort(A, N):
    change = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1, N)):
            #print(j, N)
            if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                flag = True
                change += 1
                #print(A,j)
    return A, change

result, change_n = bubbleSort(a, n)


print (" ".join(map(str,result)))
print(change_n)

Submission status: Accepted

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

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

count = 0
flag = 1

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

show(A)
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(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
				count += 1
				flag = 1

	return A, count

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

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

Submission status: Accepted

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

while flag:
    flag = False
    for i in range(n-1):
        if A[i] > A[i+1]:
            v = A[i]
            A[i] = A[i+1]
            A[i+1] = v
            flag = True
            count += 1
print(" ".join(map(str, A)))
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
t = 0

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

print(' '.join(map(str, a)))
print(t)

Submission status: Accepted

Solution code:
c = 0
N = int(input())

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

for i in range(N - 1):
    for j in reversed(range(i + 1, N)):
        if num[j - 1] > num[j]:
            p = num[j]
            num[j] = num[j - 1]
            num[j - 1] = p
            c += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubbleSort(A,N):
    flag = 1
    swapCount = 0
    while flag == 1 :
        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
                swapCount += 1
                flag = 1
    str = ""
    for i in range(N):
        str += "{} ".format(A[i])
    print(str[:len(str)-1])
    print(swapCount)


if __name__ == '__main__':
    N = int(input())
    A = [int(_) for _ in input().split()]

    bubbleSort(A,N)

Submission status: Accepted

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

while True:
    if f == 0 :
        break
    
    for i in range(f):
        if A[i] > A[i+1]:
            v = A[i]
            A[i] = A[i+1]
            A[i+1] = v
            change_sum += 1
    f += -1    

for i in range(n):
    if i == n-1:
        print(A[i])
    else:
        print(A[i],end=' ')

print(change_sum)

Submission status: Accepted

Solution code:
import sys
# import bisect
# import math
# import itertools
# import numpy as np
# import collections

"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    n = ip.I()
    a = ip.IL()
    flag = True
    ans = 0
    while flag:
        flag = False
        for j in r(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                flag = True
                ans += 1
    p(*a)
    p(ans)


main()

Submission status: Accepted

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

Submission status: Accepted

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


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

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
flag = True
cnt = 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]
            cnt += 1
            flag = True
print(' '.join([str(i) for i in a]))
print(cnt)

Submission status: Accepted

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

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

A, count = bubbleSort(A, N)
print(*A)
print(count)

Submission status: Accepted

Solution code:
def readInt():
  return int(input())


def readInts():
  return [int(i) for i in input().split()]


N = readInt()
AS = readInts()

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

print(*AS)
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
time = int(input())

tmp = input().split(" ")
nums = []
for i in tmp:
    nums.append(int(i))

flag = 1
count = 0
while flag:
    flag = 0
    for i in range(time-1,0,-1):
        #ここにはいってない
        if nums[i] < nums[i-1]:
            count += 1
            a = nums[i-1]
            nums[i-1] = nums[i]
            nums[i] = a
            flag = 1

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

Submission status: Accepted

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

print(*a)
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(A):
    global N
    flag = True
    i = 0 
    cnt = 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]
                cnt += 1
                flag = True
        i += 1
    return cnt

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
flag=1
c=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
            c+=1
print(*a)
print(c)

Submission status: Accepted

Solution code:
n = int(input())
*v, = map(int, input().split())

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

print(" ".join([str(_v) for _v in v]))
print(c)

Submission status: Accepted

Solution code:
n = int(input())
nums = [int(num) for num in input().split(" ")]
flag = 1
exchange_num = 0
result_string = ""        

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

for i in range(n):
    if result_string == "":
        result_string += str(nums[i])
    else:
        result_string += " "
        result_string += str(nums[i])

print(result_string)
print(exchange_num)

Submission status: Accepted

Solution code:
factor = int(input())
numbers = list(map(int, input().split()))

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


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

Submission status: Accepted

Solution code:
def swap(a, j):
    # list aのj番目の要素とj-1番目の要素を交換する関数
    k = a[j]
    a[j] = a[j-1]
    a[j-1] = k


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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(*cards)
print(cnt)
# #
# for j in range(10,1,-1):
#      print(j)

Submission status: Accepted

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

Submission status: Accepted

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