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:
import sys

input = sys.stdin.readline


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

    ans = 0
    at_least_once = True
    while at_least_once:
        at_least_once = False
        for i in range(N - 1):
            if A[i] > A[i + 1]:
                A[i], A[i + 1] = A[i + 1], A[i]
                at_least_once = True
                ans += 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin

def bubble_sort(A, N):
    flg = 1
    count = 0
    while flg:
        flg = 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]
                flg = 1
                count += 1
    return count
                
    
def main():
    read = stdin.readline
    N = int(read())
    data = list(map(int, read().split(" ")))
    count = bubble_sort(data, N)
    for i in range(N-1):
        print(data[i], end = " ")
    print(data[N-1])
    print(count)
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

def bubbleSort(lst,N):
    flag=1
    i=0
    cnt=0
    while flag:
        flag=0
        for j in range(N-1,i,-1):
            if lst[j]<lst[j-1]:
                lst[j],lst[j-1]=lst[j-1],lst[j]
                cnt+=1
                flag=1
        i+=1
    return lst,cnt
ans=bubbleSort(b,a)
print(" ".join(list(map(str,ans[0]))),ans[1],sep="\n")

Submission status: Accepted

Solution code:
#ALDS1_2_A:   Bubble Sort

def b_sort(A, n):
    flag = True
    res = 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
                res += 1
    return A, res

n = int(input())
l = list(map(int, input().split()))
res_l, res = b_sort(l,n)
print(*res_l)
print(res)

Submission status: Accepted

Solution code:
def bubbleSort(table):
    swap_count = 0
    for nAchieve in range(len(table) - 1):
        for index in range(len(table) - 1, nAchieve, -1):
            if table[index] < table[index - 1]:
                table[index], table[index - 1] = table[index - 1], table[index]
                swap_count += 1
    return swap_count


def conversion(table):
    return ' '.join(map(str, table))


element = int(input())
table = list(map(int, input().split()))
swap_count = bubbleSort(table)
table = conversion(table)

print('{}\n{}'.format(table, swap_count))

Submission status: Accepted

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


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

change = 1
for i in range(n):
    if not change:
        break
    change = 0
    for j in range(n-i-1):
        if x[j] > x[j+1]:
            x[j],x[j+1] = x[j+1],x[j]
            change = 1
            c += 1

print(*x)
print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

def bubble_sort(A, N):  #N個の要素を含む0-オリジンの配列 A
  flag = True  #逆の隣接要素が存在する
  exchange_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
        exchange_cnt += 1
  return A, exchange_cnt

n = int(stdin.readline().rstrip())
A = list(map(int, stdin.readline().split()))
ans = bubble_sort(A, n)
print(' '.join(map(str,ans[0])))
print(ans[1])

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int, input().split()))
cont = 0
for i in range(n):
  for j in reversed(range(i+1, n)):
    if lis[j-1] > lis[j]:
      lis[j-1], lis[j] = lis[j], lis[j-1]
      cont += 1
      
print(" ".join(map(str, lis)))
print(cont)

Submission status: Accepted

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

nums = []
for i in range(1, N):
    nums.append(i)
    
nums.sort(reverse=True)

flag = 1
cnt = 0
while flag:
    flag = 0
    for num in nums:
        if A[num] < A[num-1]:
            A[num-1], A[num] = A[num], A[num-1]
            flag = 1
            cnt += 1

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

Submission status: Accepted

Solution code:
from sys import stdin
N = int(input())
A = [int(x) for x in stdin.readline().split(' ')]

cnt = 0
for i in range(N-1):
    for j in range(0, N-1-i):
        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:
def bubble_sort(A):
    n = len(A)
    cnt = 0
    for i in range(n-1, 0, -1):
        for j in range(i):
            if A[j] > A[j+1]:
                cnt += 1
                A[j], A[j+1] = A[j+1], A[j]
    return cnt


N = int(input())
A = [int(x) for x in input().split()]
cnt = bubble_sort(A)
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

Solution code:
def printList(array):
    print(' '.join(map(str, array)))

def bubbleSort(A, N):
    needToContinue = True
    swapCount = 0
    while needToContinue:
        needToContinue = False
        for index in range(N - 1, 0, -1):
            if A[index] < A[index - 1]:
                A[index], A[index - 1] = A[index - 1], A[index]
                swapCount += 1
                needToContinue = True
    printList(A)
    print(str(swapCount))


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

Submission status: Accepted

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

frag = True
ex = 0

while frag:
    frag = False
    for j in range(n-1, 0, -1):
        if arr[j-1] > arr[j]:
            arr[j-1], arr[j] = arr[j], arr[j-1]
            frag = True
            ex += 1



def print_space(li):
    for i in range(len(li)-1):
        print(li[i], end=' ')
    else:
        print(li[-1])

        
print_space(arr)
print(ex)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    cnt = 0
    flg = True
    while flg:
        flg = False
        for i in range(N - 1):
            if A[i] > A[i + 1]:
                tmp = A[i]
                A[i] = A[i + 1]
                A[i + 1] = tmp
                flg = True
                cnt += 1
    return A, cnt


def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans, cnt = bubbleSort(A, N)
    print(*ans)
    print(cnt)


resolve()

Submission status: Accepted

Solution code:
def bubblesort(N,A):
    flag = 1
    counta = 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
                counta += 1
    print(" ".join(map(str,A)))
    print(counta)

bubblesort(int(input()),list(map(int,input().split())))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
if len(A) != N:
    raise ValueError('N個の整数を入力してください。')
elif N < 1 or N > 100:
    raise ValueError('1から100までの整数を入力してください')

frag = 1
count = 0

def print_num(A):
    for i in range(len(A)):
        if i != N-1:
            print(A[i], end=' ')
        else:
            print(A[i])
    
while frag:
    frag = 0
    for j in reversed(range(1, N)):
        if A[j]<A[j-1]:
            v = A[j]
            A[j] = A[j-1]
            A[j-1] = v
            frag = 1
            count += 1
            
print_num(A)
print(count)

Submission status: Accepted

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

cnt = 0


def bubble_sort(A, n):
    global cnt
    for _ in range(n):
        for i in range(n-1):
            if A[i] > A[i+1]:
                A[i], A[i+1] = A[i+1], A[i]
                cnt += 1


bubble_sort(A, n)
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
def bubblesort(A,N):
    count = 0
    for j in range(N):
        for i in range(N-1):
            if A[i] > A[i+1]:
                A[i],A[i+1] = A[i+1],A[i]
                count += 1

    return A,count

def bubblesort_re(A,N):
    count = 0
    #A[i] > A[i+1]の状態がある→交換する必要がある：flag = True
    #交換する必要が無いとbreak→値の無駄な比較をしない
    flag = True
    while flag:
        flag = False
        for i in range(N-1):
            if A[i] > A[i+1]:
                A[i],A[i+1] = A[i+1],A[i]
                count += 1
                flag = True

    return A,count

if __name__ == '__main__':
    N = int(input())
    A = [int(i) for i in input().split()]
    ans,count = bubblesort_re(A,N)
    for i in range(N):
        if i != N - 1:
            print(ans[i],end = " ")
        else:
            print(ans[i])
    print(count)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_A

from sys import stdin
input = lambda: stdin.readline().rstrip()

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

f = True
i = 0
cnt = 0
while f:
    f = False
    for j in reversed(range(1+i, N)):
        if A[j] < A[j-1]:
            w = A[j]
            A[j] = A[j-1]
            A[j-1] = w
            f = True
            cnt = cnt+1
    i = i+1

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

Submission status: Accepted

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

def bubble_sort(A,N):
    flag = True
    cnt  = 0
    while flag:
        flag = False
        for j in range(N-1,1-1,-1): #j=N-1 to 1
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1],A[j]
                cnt += 1
                flag = True
    return A, cnt
    
sorted_A, cnt = bubble_sort(A,N)
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
import functools

def BubbleSort(A):
    cnt = 0
    for i in range(len(A)):
        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
    return cnt
n = int(input())
A = list(map(int,input().split()))
cnt = BubbleSort(A)
print(*A)
print(cnt)

Submission status: Accepted

Solution code:

def buble_sort(N,a_count):
  flag = 1
  while(flag):
    flag = 0
    for i in range(1,len(N)):
      if N[i] < N[i-1]:
        tmp = N[i-1]
        N[i-1] = N[i]
        N[i] = tmp
        flag = 1
        a_count += 1
  return a_count


x = 0
N = int(input())
a = list(map(int,input().split()))
w = buble_sort(a,x) 

for  i in range(len(a)):
  if i == len(a)- 1:
    print(a[i],end = "")
  else:
    print(a[i],end = " ")
  
print()
print(w)

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(N-1):
    for j in range(N-1,i,-1):
        if A[j-1] > A[j]:
            A[j-1], A[j] = A[j], A[j-1]
            count += 1
print(*A); print(count)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
def bubbleSort(A,N):
    flg=1
    global ct
    ct = 0
    while flg:
        flg = 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]
                flg=1
                ct+=1

    return ' '.join(map(str,A))

print(bubbleSort(A,N))
print(ct)

Submission status: Accepted

Solution code:
size = int(input())
l = []

for i in map(int, input().split()):
    l.append(i)

sort_cnt = 0
reversed_cnt = 0

for j in range(size - 1,0,-1):
    for i in range(size - 1,sort_cnt,-1):
        tmp = l[i-1]
        if l[i] < l[i-1]:
            l[i-1] = l[i]
            l[i] = tmp
            reversed_cnt += 1
    sort_cnt += 1

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

Submission status: Accepted

Solution code:
from typing import List, Tuple


def bubble_sort(A: List[int]) -> Tuple[List[int], int]:
    flag: bool = True
    count: int = 0

    while flag == True:
        flag = False
        for j in range(len(A) - 1, 0, -1):
            if A[j - 1] > A[j]:
                A[j - 1], A[j] = A[j], A[j - 1]
                flag = True
                count += 1

    return A, count


def main():
    _ = int(input())
    A: List[int] = list(map(int, input().split()))

    lst, count = bubble_sort(A)
    print(*lst, sep=" ")
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    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(*A)
    print(cnt)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

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

bubbleSort(numlist,n)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(x) for x in input().split()]#リスト内包表記

def bubble_sort(r):
    flag=True
    i=0
    count=0
    while flag:
        flag=False
        for i in range(n-1,i,-1):#iは1でとまらないとi-1が-1となってしまう
            if r[i]<r[i-1]:
                r[i],r[i-1]=r[i-1],r[i]#swap
                count+=1
                flag=True#入れ替える条件があって初めてtrueになる
    print(*r)
    print(count)

bubble_sort(R)

Submission status: Accepted

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

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

print(*A)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input("")) #รับจำนวนข้อมูลในบรรทัดแรก
ns = list(map(int,input().split())) #รับข้อมูลในบรรทัดที่2 split และเก็บไว้ใน int 
c = 0
for i in range(n-1): #วน n-1 เพราะไม่นับรวมครั้งแรกที่ทำ
    for j in range(n-1-i): #วน loop 
        if ns[j] > ns[j+1]: #ถ้าข้อมูลในตำแหน่งที่ j > ข้อมูลตำแหน่งที่ j+1
            ns[j],ns[j+1]=ns[j+1],ns[j]
            #ทำการสลับตำแหน่ง j ไปที่ j+1 เหมือนฟองอากาศที่ลอยขึ้นไปเรื่อยๆ
            c+=1 # นับครั้งการ swap
print(" ".join(map(str,ns)))
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
statements = input().split()
count = 0

for j in range(num):
    for i in range(num-1):
        statements[i] = int(statements[i])
        statements[i+1] = int(statements[i+1])
        if statements[i] > statements[i+1]:
            temp = statements[i] 
            statements[i] = statements[i+1]
            statements[i+1] = temp
            count += 1

print(*statements)
print(count)

Submission status: Accepted

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

count = 0
for b in range(len(A)-1):
    for a in range(0,len(A)-1-b):
        if A[-a-1] < A[-a-2]:
            A[-a-1],A[-a-2] = A[-a-2],A[-a-1]
            count += 1

print(' '.join(list(map(str,A))))
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 j in range(1,N):
        if A[N-j] < A[N-j-1]:
            dummy = A[N-j]
            A[N-j] = A[N-j-1]
            A[N-j-1] = dummy
            flag = 1
            count += 1
for i in range(N-1):
    print(A[i],end=" ")
print(f"{A[N-1]}\n{count}")

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
kaisu = bubbleSort(A, N)
for i in range(N) :
    print(A[i], end="")
    if i != N-1 :
        print(" ", end="")
print()
print(kaisu)

Submission status: Accepted

Solution code:
# Bubble Sort

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]:
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp
            flag = 1
            cnt += 1

print(*a)
print(cnt)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    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(' '.join(map(str,A)))
    print(cnt)


x =int(input())
y = list(map(int,input().split(' ')))
bubbleSort(y,x)

Submission status: Accepted

Solution code:
import math
import itertools

def main():
  n = int(input())
  A = list(map(int, input().split()))
  len_A = len(A)
  times = 0

  for i in range(len_A):
    for j in reversed(range(i+1, len_A)):
      if A[j] < A[j-1]:
        tmp = A[j]
        A[j] = A[j-1]
        A[j-1] = tmp
        times += 1

  print(*A)
  print(times)

if __name__ == '__main__':
  main()

Submission status: Accepted

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


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

bubbleSort(A, N)

Submission status: Accepted

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

bubble_sort(ar,num)

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

n = int(input())
a = [ int(i) for i in input().split()]
b , count = bubblesort(a,n)
for i in range(len(b)):
    if i == len(b)-1:
        print(b[i])
    else:
        print(b[i],end=' ')
print(count)

Submission status: Accepted

Solution code:
n = int(input())
As = input().split()
A = [int(i) for i in As]
f = True
c = 0

while f:
    f = False
    for i in range(n-1):
        if A[i+1] < A[i]:
            w = A[i+1]
            A[i+1] = A[i]
            A[i] = w
            c += 1
            f = True

for i in range(n-1):
    print("{} ".format(A[i]), end="")
print(A[n-1])
print(c)

Submission status: Accepted

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

flag = True
j = 1
cnt = 0

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

result = [str(i) for i in A]
print(' '.join(result))
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
flg = True
cnt = 0
while flg:
	flg = False
	for i in range(len(a) - 1, 0, -1):
		if a[i] < a[i - 1]:
			a[i - 1], a[i] = a[i], a[i - 1]
			cnt = cnt +1
			flg = True

print(' '.join([str(n) for n in a]))
print(cnt)
# for i in range(0, len(a)):
#	print('{0} {1}'.format(i, a[i]))

# for i in range(0,10):
#	print('{} '.format(i), end='')
# print()

# for i in range(10,0, -1):
#	print('{} '.format(i), end='')

Submission status: Accepted

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

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


nums = [str(num) for num in nums]
print(" ".join(nums))
print(count)

Submission status: Accepted

Solution code:
num = int(input())
sort_list = list(map(int,input().split()))

cnt = 0
for i in range(num):
    for j in reversed(range(i+1,num)):
        if sort_list[j] < sort_list[j-1]:
            sort_list[j],sort_list[j-1] = sort_list[j-1],sort_list[j]
            cnt += 1

print(" ".join([str(i) for i in sort_list]))
print(cnt)

Submission status: Accepted

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

[print(A[x],end=' ') for x in range(N-1)];print(A[N-1]);print(cnt)

Submission status: Accepted

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

n = int(input())
l = list(map(int, input().split()))
c = bubbleSort(l, n)
print(" ".join(map(str, l)))
print(c)

Submission status: Accepted

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



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()))
count = 0
flag = 1
i = 0

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

s = [str(a) for a in A]
s = ' '.join(s)
print(s)
print(count)

Submission status: Accepted

Solution code:
# https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_1_A
import sys
from functools import reduce


def to_str(x, y):
    return str(x) + ' ' + str(y)


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

count = 0
flag = 1
while flag:
    flag = 0
    for j in range(n):
        if j <= n - 2 and A[j] > A[j + 1]:
            tmp = A[j + 1]
            A[j + 1] = A[j]
            A[j] = tmp
            count += 1
            flag = 1

print(reduce(to_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 j in range(n-1, 0, -1):
        if (a[j] < a[j-1]):
            (a[j-1], a[j]) = (a[j], a[j-1])
            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()))

swapcnt = 0

def bubbleSort(A):
  global N,swapcnt
  for i in range(N):
    for j in  range(N-1,i,-1):
      if A[j] < A[j-1]:
        x = A[j]
        A[j] = A[j-1]
        A[j-1] = x
        swapcnt += 1

bubbleSort(A)
strA = [str(a) for a in A]
print(" ".join(strA))
print(swapcnt)

Submission status: Accepted

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

change = 0
for i in range(N):
    for j in range(N-i-1):
        if A[j] > A[j+1]:
            A[j],A[j+1] = A[j+1],A[j]
            change += 1
print(" ".join(map(str,A)))
print(change)

Submission status: Accepted

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

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

print(*A)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = []
a = input().split()
a = list(map(int,a))
num = 0
i = 0
flag = 1
while flag == 1:
    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
            num = num + 1
    i = i + 1
print(*a)
print(num)

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,i,-1):
        if A[j] < A[j-1]:
            A[j],A[j-1] = A[j-1],A[j]
            count += 1
print(*A)
print(count)

Submission status: Accepted

Solution code:
def bubbleSort(sort_list):
    flag = True
    count = 0
    while flag:
        flag = False

        for j in range(len(sort_list) - 1, 0, -1):
            if sort_list[j] < sort_list[j - 1]:
                tmp = sort_list[j]
                sort_list[j] = sort_list[j - 1]
                sort_list[j - 1] = tmp
                count += 1
                flag = True

    print(' '.join([str(i) for i in sort_list]))
    print(count)


if __name__ == "__main__":
    N = int(input())
    sort_list = [int(i) for i in input().split()]

    bubbleSort(sort_list)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def trace():
    for i in range(len(R)):
        if i != len(R) - 1:
            print(R[i],end=' ')
        else:
            print(R[i])

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

trace()

print(count)

Submission status: Accepted

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

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

out_A, cnt_sort = bubbleSort(A,N)

out_A_str = list(map(str, out_A))
print(" ".join(out_A_str))
print(str(cnt_sort))

Submission status: Accepted

Solution code:
# Problem - バブルソート

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

# input
N = int(input())
a_list = list(map(int, input().split()))

# output
ans_list, ans_count = bubbleSort(a_list, N)
print(" ".join(list(map(str, ans_list))))
print(ans_count)

Submission status: Accepted

Solution code:
# Bubble Sort

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

def bubbleSort(A):
	n = len(A)
	cnt = 0 # 要素の交換回数
	flag = 1
	while flag:
		flag = 0
		for j in range(n - 1, 0, -1):
			if A[j] < A[j - 1]:
				A[j], A[j - 1] = A[j - 1], A[j]
				flag = 1
				cnt += 1
	return A, cnt

res, cnt = bubbleSort(A)
print(*res)
print(cnt)