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:
N, *A = map(int, open(0).read().split())
F = True
ans = 0
while F:
    F = False
    for i in range(N-1):
        if A[i] > A[i+1]:
            A[i], A[i+1] = A[i+1], A[i]
            F = True
            ans += 1
print(' '.join(map(str, A)))
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

def bubbleSort(A, N):
    flag = 1
    cnt = 0;
    while flag:
        flag = 0;
        for i in reversed(range(1, N)):
            if A[i-1] > A[i]:
                A[i-1], A[i] = A[i] , A[i-1]
                flag = 1;
                cnt += 1
    showResult(A, cnt)

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

N = int(input())
A = list(map(int, input().split())) 
#A = [1,5,4,3,2]
#N = len(A)

bubbleSort(A, N)

Submission status: Accepted

Solution code:
flag = 0

N = int(input())

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

for i in range(N-1):
  for j in range(N-1):
    if l[N-1-j] < l[N-2-j]:
      a = l[N-1-j]
      b = l[N-2-j]
      l[N-2-j] = a
      l[N-1-j] = b
      flag += 1

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

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    i = 0
    count = 0
    while i<N:
        for j in range(0, N-1-i):
            if A[N-j-2]>A[N-j-1]:    
                (A[N-j-1], A[N-j-2]) = (A[N-j-2], A[N-j-1])
                count += 1
        i += 1
    print(" ".join(map(str, A)))
    print(count)


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()))
cnt = 0
i = 0
flag = 1
while flag:
    flag = 0
    for j in range(n-1,i,-1):
        if A[j] < A[j-1]:
            buff = A[j-1]
            A[j-1]=A[j]
            A[j] = buff
            flag = 1
            cnt += 1
    i += 1
for k in range(n):
    if k != n-1:
        print(A[k],"",end="")
    else:
        print(A[k])
print(cnt)

Submission status: Accepted

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

count = 0
flag = 1
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]
            count += 1
            flag = 1
print(*A)
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(a):
    cnt = 0
    size = len(a)
    for i in range(size):
        for j in range(size-1, i, -1):
            if a[j] < a[j-1]:
                a[j], a[j-1] = a[j-1], a[j]
                cnt += 1

    return (a,cnt)

_ = input()
l = [ int(s) for s in input().split() ]
(nl,swap) =  bubble_sort(l)

print(*nl)
print(swap)

Submission status: Accepted

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

flag=1
count=0

while flag==1:
  flag=0
  for i in range(n-1,0,-1):
    if A[i-1] > A[i]:
      v =A[i]
      A[i] = A[i-1]
      A[i-1] =v
      flag=1
      count+=1
print(*A, sep=' ')
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

class BitSum:
    def __init__(self, n):
        self.n = n + 3
        self.table = [0] * (self.n + 1)

    def update(self, i, x):
        i += 1
        while i <= self.n:
            self.table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    bit=BitSum(105)
    swap_n=0
    for i,a in enumerate(aa):
        swap_n+=i-bit.sum(a)
        bit.update(a,1)
    aa.sort()
    print(*aa)
    print(swap_n)

main()

Submission status: Accepted

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

Submission status: Accepted

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

def bubbleSort(A, N):
    flag = 1
    cnt = 0
    
    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
                cnt += flag
            
    print(*A)
    print(cnt)
    
bubbleSort(A, n)

Submission status: Accepted

Solution code:
def bubbleSort(l):
    k = 1
    count = 0
    for j in range(len(l)-1):
        for i in range(len(l)-k):
            if l[i] > l[i+1]:
                a = l[i]
                l[i] = l[i+1]
                l[i+1] = a
                count += 1
        k += 1

    ans_l = map(str,l)
    return ans_l,count

n = int(input())
l = list(map(int,input().split()))
ans_l,ans_count = bubbleSort(l)
print(' '.join(ans_l))
print(ans_count)

Submission status: Accepted

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

flag = True
cnt=0

for j in range(n):
  flag = True
  while flag:
    flag = False
    i = n #5
    #print(ary) #5 3 2 4 1
    while i > 1:
      i -= 1
      if(ary[i-1] > ary[i]):
        ary[i], ary[i-1] = ary[i-1], ary[i]
        cnt += 1
print(' '.join(map(str,ary)))
print(cnt)

Submission status: Accepted

Solution code:
def bubble_sort(arr, n):
    count = 0
    for j in range(n - 1):
        for i in reversed(range(j, n - 1)):
            if arr[i] > arr[i + 1]:
                arr[i], arr[i + 1] = arr[i + 1], arr[i]
                count += 1
    return arr, count


def main():
    n = int(input())
    arr = list(map(int, input().split()))
    # n = 5
    # arr = [5, 3, 2, 4, 1]
    sorted_arr, count = bubble_sort(arr, n)
    print(" ".join(map(str, sorted_arr)))
    print(count)
    # print(sorted_arr)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

flg = True
cnt = 0
i = 0
while flg:
    flg = False
    #末尾(N-1)から先頭(i)に向けてソートする
    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
            flg = True
    #一番小さい数字が確定するごとにiをカウントアップ
    i += 1
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
def bubble_sort(A):
    cnt = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        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]]
                cnt += 1
                flag = 1
        i += 1
    print(*A)
    print(cnt)
    return A

bubble_sort(A)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
def flag(A):
    j = 0
    for i in range(len(A)-1):
        if A[i] > A[i+1]:
            j = 1
    return j != 0 
n = 0
while flag(A):
    for i in range(N-1):
        if A[i] > A[i+1]:
            A_i = A[i]
            A[i] = A[i+1]
            A[i+1] = A_i
            n += 1
print(*A)
print(n)

Submission status: Accepted

Solution code:
def bubbleSort(numlist):
    n = len(numlist)
    swaptime = 0
    for i in range(n):
        for j in range(n - 1, i, -1):
            if numlist[j] < numlist[j - 1]:
                swaptime += 1
                numlist[j], numlist[j - 1] = numlist[j - 1], numlist[j]
    return swaptime

def print_numlist(numlist):
    if len(numlist) == 1:
        print(numlist[0])
    else:
        for i in range(0, len(numlist) - 1):
            print(numlist[i], end = ' ')
        print(numlist[-1])

if __name__ == '__main__':
    n = int(input())
    numlist = [int(i) for i in input().replace('\n', '').split(' ')]
    swapnum =  bubbleSort(numlist)

    print_numlist(numlist)
    print(swapnum)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int, input().split()))
l_res=sorted(l)
cnt=0
for i in reversed(range(n)):
    for j in range(i):
        if l[j]>l[j+1]:
            l[j],l[j+1]=l[j+1],l[j]
            cnt+=1
print(*l_res)
print(cnt)

Submission status: Accepted

Solution code:
1# bubbleSort(A, N) // N 個の要素を含む 0-オリジンの配列 A
2#   flag = 1 // 逆の隣接要素が存在する
3#   while flag
4#     flag = 0
5#     for j が N-1 から 1 まで
6#       if A[j] < A[j-1]
7#         A[j] と A[j-1] を交換
8#         flag = 1

n = int(input())
nums = list(map(int, input().split()))
count = 0
flag = True
while flag:
    flag = 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]
            count += 1
            flag = True

print(*nums)
print(count)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

while flag:
    flag = False
    i = 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]
            counter += 1
            flag = True
    i += 1
A = [str(i) for i in A]
print(' '.join(A))
print(counter)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
change = 0
for i in range(n):
	u = n-1
	while u > i:
		if a[u-1] > a[u]:
			a[u-1], a[u] = a[u], a[u-1]
			change += 1
		u -= 1
print(*a)
print(change)

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 range(n-1, 0, -1):
    if A[j] < A[j-1]:
      A[j], A[j-1] = A[j-1], A[j]
      flag = 1
      counter += 1
print(*A, sep=" ")
print(counter)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# 諸々の入力
N=int(input())
A=list(map(int,input().split()))

flag=1 # flagはbool型みたいなもの
count=0 # 反転数

# バブルソート
while flag==1:
    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]
            flag=1
            count+=1

# 答えと反転数の出力
ans=list(map(str,A))
print(' '.join(ans))
print(count)

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


N = int(input())

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

swap_count = 0

for i in range(len(table)-1):
    for k in range(len(table)-1,i,-1):
        if table[k] < table[k-1]:
            table[k],table[k-1] = table[k-1],table[k]
            swap_count += 1

print(' '.join(map(str,table)))
print("%d"%(swap_count))

Submission status: Accepted

Solution code:
def main():
    n = int(raw_input().strip())
    data = map(int, raw_input().split())
    change = bubble_sort(n, data)
    print " ".join([str(x) for x in data])
    print change

def bubble_sort(n, data):
    change = 0
    for i in xrange(n):
        for j in xrange(n - 1, i, -1):
            if data[j] < data[j - 1]:
                change += 1
                data[j], data[j - 1] = data[j - 1], data[j]
    return change

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def bubbleSort(a, n):
    number = 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
                number += 1
    return number


n = int(input())
ls = list(map(int, input().split()))
num = bubbleSort(ls, n)
for i in range(len(ls)):
    if i:
        print(" ", end="")
    print(ls[i], end="")
print()
print(num)

Submission status: Accepted

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

for i in range(N-1):
  flag = False
  for j in range(N-2, i-1, -1):
    if A[j] > A[j+1]:
      t = A[j]
      A[j] = A[j+1]
      A[j+1] = t
      flag = True
      r += 1

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

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 reversed(range(1,n)):
      if a[j] < a[j-1]:
        temp = a[j]
        a[j] = a[j-1]
        a[j-1] = temp
        flag = 1
        count += 1
  return count

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 = 1
c = 0
while flag:
    flag = 0
    for i in range(1,n):
        j = n-i
        if a[j] < a[j-1]:
            a[j],a[j-1] = a[j-1],a[j]
            c += 1
            flag = 1

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

Submission status: Accepted

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

for i in range(len(a)):
    for j in reversed(range(1,n)):
        if a[j]<a[j-1]:
            a[j-1],a[j]=a[j],a[j-1]
            s+=1
for i in range(n):
    if i==0:
        print(a[i],end="")
    else:
        print("{: }".format(a[i]),end="")
print()
print(s)

Submission status: Accepted

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

def bubble_sort(a):
    global cnt
    for i in range(len(a)):
        l = 0
        r = 1

        while r < len(a):
            if a[l] > a[r]:
                a[l], a[r] = a[r], a[l]
                cnt += 1
            l += 1
            r += 1


    # for i in range(len(a)):
    #     for j in range(i + 1, len(a)):
    #         if a[i] > a[j]:
    #             a[i], a[j] = a[j], a[i]

bubble_sort(a)
print(*a)
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

def bubbleSort(N):
    Flag=True
    count=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]
                Flag=True
                count+=1
    print(*A)
    print(count)
bubbleSort(N)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
ans=0
flag=True
while flag:
    flag=False
    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=True
            ans+=1
print(*a)
print(ans)

Submission status: Accepted

Solution code:
def func(n, A):
    flag = True
    count = 0
    i = 0
    while (flag):
        flag = False
        for j in reversed(range(i+1, n)):
            if (A[j] < A[j-1]):
                A[j], A[j-1] = A[j-1], A[j]
                flag = True
                count += 1
        i += 1
    print (" ".join(map(str, A)))
    print (count)

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

Submission status: Accepted

Solution code:
#バブルソート
def print_output(_list):
    for index in range(len(_list)):
        if index == len(_list) - 1:
            print(_list[index])
        else:
            print(_list[index],end=' ')

target_length = int(input())
target_list = list(map(int,input().split()))

bubble_count = 0
for index in range(1, target_length):
    for compare_index in reversed(range(index)):
        if target_list[compare_index + 1] < target_list[compare_index]:
            target_list[compare_index + 1] ,target_list[compare_index] = target_list[compare_index] ,target_list[compare_index + 1]
            bubble_count += 1
        else:            
            break
print_output(target_list)
print(bubble_count)

Submission status: Accepted

Solution code:
import sys
from sys import stdin

cnt = 0

def BubbleSort(L):
    global cnt
    for i in range(len(L)):
        for j in range(len(L)-1, i, -1):
            if L[j-1] > L[j]:
                L[j-1], L[j] = L[j], L[j-1]
                cnt += 1
    
n = int(input())
NUM_LIST = list(map(int, input().split()))

BubbleSort(NUM_LIST)
STR = list(map(str, NUM_LIST))
print(" ".join(STR))
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
	print(*A)
	print(count)


if __name__ == "__main__":
	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()))
cnt = 0
for i in range(len(A)-1):
    for j in range(len(A)-1, i, -1):
        if A[j] < A[j-1]:
            tmp = A[j]
            A[j] = A[j-1]
            A[j-1] = tmp
            cnt += 1
print(" ".join(map(str, A)))
print(cnt)

Submission status: Accepted

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

N = int(input())
A = list(map(int,input().split()))
A,cnt = bubbleSort(A,N)
print(*A)
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def BubbleSort(A):
    count = 0
    for i in range(len(A)):
        for j in reversed(range(len(A) - 1, 0, -1)):
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                count += 1
    return A, count

N = int(input())
A = list(map(int, input().split()))
bubble_sort, count = BubbleSort(A)
print(*bubble_sort)
print(count)

Submission status: Accepted

Solution code:

def babble_sort(A, N):
    cnt = 0
    flag = 1
    while flag:
      flag = 0
      i = 0
      for j in reversed(range(i + 1, N)):
        if A[j] < A[j-1]:
          c = A[j]
          A[j] = A[j -1]
          A[j-1] = c
          cnt += 1
          flag = 1
      i += 1
    for b in range(N):
      print(A[b], end=" \n"[b + 1 == N])
    print(cnt)


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

babble_sort(A, N)

Submission status: Accepted

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


def bubbleSort(li: list, n: int):
    count = 0
    for i in range(n-1):
        for j in reversed(range(i+1, n)):
            if li[j-1] > li[j]:
                li[j-1], li[j] = li[j], li[j-1]
                count += 1
    print(*li)
    print(count)


bubbleSort(A, N)

Submission status: Accepted

Solution code:
def bubble_sort(nums, n):
    count = 0
    flag = True
    while flag:
        flag = False
        for i in reversed(range(1, n)):
            if nums[i] < nums[i-1]:
                tmp = nums[i]
                nums[i] = nums[i-1]
                nums[i-1] = tmp
                count += 1
                flag = True

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

if __name__=="__main__":
    n = int(input())
    nums = list(map(int, input().split()))
    bubble_sort(nums, n)

Submission status: Accepted

Solution code:
n,*a = map(int,open(0).read().split())

ans = 0 

def BublbleSort(a,m):
    global ans
    flag = 1
    while flag:
        flag = 0
        for j in range(m-1,0,-1):
            if a[j] < a[j-1]:
                a[j],a[j-1]=a[j-1],a[j]
                ans += 1
                flag = 1
BublbleSort(a,n)
  
            
print(*a)

print(ans)

Submission status: Accepted

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

def BubbleSort(seq):
    count = 0
    for i in range(N):
        for j in range(1, N):
            if seq[j] < seq[j-1]:
                swp = seq[j]
                seq[j], seq[j-1] = seq[j-1], swp
                count += 1

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

BubbleSort(seq)

Submission status: Accepted

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

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

for _ in range(N):
    if _ == N-1:
        print(A[_], sep=' ',end='')
    else:
        print(A[_], sep='',end=' ')
print()
print(count)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    P = list(map(int, input().split()))
    bubbleSort(N, P)


def bubbleSort(N, P):
    change_count = 0
    i = 0
    while True:
        for j in range(N - 1, i, -1):
            if P[j] < P[j - 1]:
                tmp = P[j]
                P[j] = P[j - 1]
                P[j - 1] = tmp
                change_count += 1
        i += 1
        # print("i:{}".format(i))
        if i == N:
            break

    print(' '.join(map(str, P)))
    print(change_count)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0

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

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

Submission status: Accepted

Solution code:
nums_count = int(input())
nums = list(map(int, input().split()))
swap_count = 0

for i in range(nums_count):
    for j in range(nums_count-1, i, -1):
        if nums[j-1] > nums[j]:
            nums[j-1], nums[j] = nums[j], nums[j-1]
            swap_count += 1

for num in nums[:-1]:
    print(num, end=' ')
print(nums[-1])

print(swap_count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def pintarr(arr):
    arr = [str(c) for c in arr]
    print(' '.join(arr))


def bub_sort(it, key):
    count = 0
    for start_ind in range(len(it)):
        for target_ind in range(len(it)-1, start_ind, -1):
            if it[target_ind] < it[target_ind-1]:
                it[target_ind], it[target_ind-1] = it[target_ind-1], it[target_ind],
                count += 1
    pintarr(it)
    print(count)


if __name__ == '__main__':

    t = int(input())
    a = input()
    a = [int(c) for c in a.split(' ')]
    bub_sort(a, t)

Submission status: Accepted

Solution code:
#バブルソート
n=int(input())
a=[int(i) for i in input().split()]

f=1 #隣接した要素が昇順になっていないものが存在する
j=0
while f:
    f=0
    for i in range(n-1):
        if a[n-i-1]<a[n-i-2]:
            a[n-i-1],a[n-i-2]=a[n-i-2],a[n-i-1]
            f=1
            j+=1
print(" ".join(list(map(str,a))))
print(j)

Submission status: Accepted

Solution code:
def bb(A,n):
    cnt=0
    for k in range(n):
        for i in range(n-1,k,-1):
            if A[i] < A[i-1]:
                tmp=A[i]
                A[i]=A[i-1]
                A[i-1]=tmp
                cnt=cnt+1
    return A,cnt

n=int(input())
A=list(map(int,input().split()))
A,cnt=bb(A,n)
As=[str(i)for i in A]
As = ' '.join(As)
print(As)
print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

frag = 1
point = 0
while frag == 1:
    frag = 0
    for i in range(n-1,0,-1):
        if li[i] < li[i-1]:
            li[i],li[i-1] = li[i-1],li[i]
            frag = 1
            point += 1
print(' '.join(map(str, li)))
print(point)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=list(map(int,input().split()))
swap=0

def bubble_sort(A,N,swap):
    flag=True
    while flag:
        flag=False
        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
                swap+=1
                flag=True
    return A,swap

Rst,swap=bubble_sort(s,n,swap)

print(Rst[0],end='',sep='')
for i in range(1,n):
    print(" ",Rst[i],end='',sep='')
print()
print(swap)

Submission status: Accepted

Solution code:
def bubble_sort(a, n):
    # N個の要素を含む0-オリジナルの配列A
    cnt = 0
    flag = True
    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]
                cnt += 1
                flag = True
    print(" ".join([str(x) for x in a]))
    print(cnt)


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#bubble sort

N=int(input())
A=list(map(int,input().split()))
counter=0
flag=True
string=""
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]
            counter+=1
            flag=True

A_str=map(str,A)
string=" ".join(A_str)
print(string)
print(counter)

Submission status: Accepted

Solution code:
N = int(input())

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

flag = 1
cnt = 0

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

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

Submission status: Accepted

Solution code:
input()
A = [int(v) for v in input().split(' ')]

swap_count = 0
def swap(A, i, j):
    t = A[i]
    A[i] = A[j]
    A[j] = t

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

print(" ".join([str(v) for v in A]))
print(swap_count)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]

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

Submission status: Accepted

Solution code:
#coding: utf-8

swap = 0
n = int(input())
a = list(map(int, input().split()))
for i in range(n - 1):
    for j in range(n - 1 - i):
        if a[j] > a[j + 1]:
            temp = a[j]
            a[j] = a[j + 1]
            a[j + 1] = temp
            swap += 1
print(' '.join([str(i) for i in a]))
print(swap)

Submission status: Accepted

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


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

Submission status: Accepted

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

flag = 1
count = 0
while flag:
    flag = 0
    for i in range(n-1, 0, -1):
        if A[i-1] > A[i]:
            A[i-1], A[i] = A[i], A[i-1]
            flag = 1
            count += 1
print(*A)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
box=list(map(int,input().split()))
count=0
for i in range(n):
    p=n-1
    while p!=i:
        if box[p]<box[p-1]:
            box[p],box[p-1]=box[p-1],box[p]
            count +=1
        p -=1
print(*box)
print(count)

Submission status: Accepted

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

count = 0
dec = 0

is_reverse = True
while is_reverse:
    is_reverse = False
    
    for i in range(n-1-dec):
        if l[i] > l[i+1]:
            l[i] , l[i+1] = l[i+1], l[i]
            is_reverse = True
            count += 1
    
    dec += 1

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

Submission status: Accepted

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

main(int(input()))

Submission status: Accepted

Solution code:
# coding: utf-8

def bubbleSort(A, N):
    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]
                c += 1
                flag = 1
    print(' '.join(map(str, A)))
    print(c)


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

Submission status: Accepted

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

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

print(' '.join([str(x) for x in a]))
print(count)

Submission status: Accepted

Solution code:
def show(A):
    for i in range(N):
        if i<N-1:
            print(A[i],end=' ')
        else:
            print(A[i])

N=int(input())
A=list(map(int,input().split()))
count=0
flag=True
while flag:
    flag=False
    for j in reversed(range(1,N)):
        if A[j-1]>A[j]:
            A[j-1],A[j]=A[j],A[j-1]
            count+=1
            flag=True
show(A)
print(count)

Submission status: Accepted

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

flg = True
ans = 0

while flg:
    flg = False
    for i in range(N-1):
        if A[i] > A[i+1]:
            A[i], A[i+1] = A[i+1], A[i]
            flg = True
            ans += 1
    
print(' '.join(list(map(str, A))))
print(ans)