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

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

        else:
            print(nums[i])

def bubble_sort(nums,n):
    count=0
    flag=1
    while flag:
        flag=0
        for j in range(n-1,0,-1):
            
            if nums[j-1]>nums[j]:
                flag=1
                nums[j-1],nums[j]=nums[j],nums[j-1]
                count+=1
            

    return count,nums

count,nums=bubble_sort(nums,n)
show(nums)
print(count)

Submission status: Accepted

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

counter=0
for E in range(0,N):
    for i in range(N-1,E,-1):
        if S[i]<S[i-1]:
            this_value=S[i]
            S[i]=S[i-1]
            S[i-1]=this_value
            counter+=1
        else:pass

print(*S)
print(counter)

Submission status: Accepted

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

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

print(*A)
print(cnt)

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(n-1,0,-1):
        if a[j] < a[j-1]:
            cnt += 1
            a[j],a[j-1] = a[j-1],a[j]
            flag = 1
print(*a)
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubble_sort(data, n):
    end_of_line = 0
    change_num = 0

    while end_of_line < n-1:
        for i in reversed(range(end_of_line,n)):
            if data[i-1] > data[i]:
                v = data[i]
                data[i] = data[i-1]
                data[i-1] = v
                change_num += 1
                break
            if end_of_line >= i-1:
                end_of_line += 1
                break
    
    return data, change_num


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

sorted_data, chage_num = bubble_sort(data,num)

print(' '.join(map(str,sorted_data)))
print("%d"%chage_num)

Submission status: Accepted

Solution code:
def bubble(A,N):
    flag = 1
    c = 0
    while flag:
        flag = 0
        for j in range(1,N)[::-1]:
            if A[j]<A[j-1]:
                t = A[j]
                A[j]=A[j-1]
                A[j-1]=t
                c+=1
                flag = 1
    
    L = [str(a) for a in A]
    print(" ".join(L))
    print(c)

if __name__=="__main__":
    n=int(input())
    l=[int(x) for x in input().split()]
    bubble(l,n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def BubbleSort(A,N):
    count = 0
    flag = 1
    while flag == 1:
        flag = 0
        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
                flag = 1
    L=[str(ai) for ai in A]
    L=" ".join(L)
    print(L)
    return count
    
n = int(input())
a = list(map(int,input().split()))
count = BubbleSort(a,n)
print(count)

Submission status: Accepted

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

f = 1
c = 0
while f:
    f = 0
    for i in range(1, n)[::-1]:
        if A[i - 1] > A[i]:
            A[i - 1], A[i] = A[i], A[i - 1]
            c += 1
            f = True

print(*A)
print(c)

Submission status: Accepted

Solution code:
def bubblesort(a, n):
    n_exchange = 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]
                n_exchange += 1
                flag = True

    return a, n_exchange

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

a_sort, n_exchange = bubblesort(a, n)

print(' '.join([str(x) for x in a_sort]))
print(n_exchange)

Submission status: Accepted

Solution code:
def BubbleSort(l):
  countSwap = 0
  for i in range(len(l)-1):
    for j in reversed(range(i+1, len(l))):
      if l[j] < l[j-1]:
        temp = l[j]
        l[j] = l[j-1]
        l[j-1] = temp
        countSwap += 1
  print(" ".join(map(str, l)))
  print(countSwap)

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

Submission status: Accepted

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

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


def p(N):
    for i in range(len(N)):
        if i == len(N) - 1:
            print(N[i])
        else:
            print(N[i], end=" ")


flag = True
ans = 0

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

p(A)
print(ans)

Submission status: Accepted

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

flag = 1
i = 0
count = 0
while flag==1:
    flag = 0
    for j in range(i+1,len(number))[::-1]:
        if number[j] < number[j-1]:
            temp  = number[j]
            number[j] = number[j-1]
            number[j-1] = temp
            flag = 1
            count += 1
    i += 1


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

Submission status: Accepted

Solution code:
# -*- coding:utf8 -*-

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

Submission status: Accepted

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

swap=0

flag = 1
while flag:
    flag = 0
    for i in reversed(list(range(1,len(lst)))):
        if lst[i-1]>lst[i]:
            lst[i-1],lst[i] = lst[i],lst[i-1]
            flag = 1
            swap+=1

print(' '.join(map(str,lst)))
print(swap)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(_) for _ 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]:
            t = A[j]
            A[j] = A[j-1]
            A[j-1] = t
            flag = 1
            count += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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


flag = 1
i = 0
count = 0

while flag:
    flag = 0
    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 = 1
            count += 1
    i += 1
for k in range(len(a)):
    if k == n - 1:
        print(a[k])
    else:
        print(a[k], end=' ')
print(count)

Submission status: Accepted

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


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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
flag = True
swap = 0
while flag:
    flag = False
    for i in range(N-1,0,-1):
        if A[i-1] > A[i]:
            A[i-1], A[i] = A[i], A[i-1]
            swap += 1
            flag = True
            # break
print(" ".join(map(str,A)))
print(swap)

Submission status: Accepted

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

n = int(input())
lst = list(map(int, input().split()))
cnt = bubble_sort(lst)
print(*lst)
print(cnt)

Submission status: Accepted

Solution code:
# バブルソート

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


def bubbleSort(A, N):
    isExist = True
    cnt = 0
    while isExist:
        isExist = False
        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]
                isExist = True
                cnt += 1
    print(' '.join([str(i) for i in A]))
    print(cnt)


bubbleSort(A, N)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
#Bubble sort
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] #swap by Python3
            cnt +=1
            flag = 1
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

flag = 1
perm = 0
while flag == 1:
    flag = 0
    for j in range(N-1, 0, -1):
        if A[j] < A[j-1]:
            temp = A[j]
            temp2 = A[j-1]
            A[j-1] = temp
            A[j] = temp2
            perm += 1
            flag = 1
print(" ".join(list(map(str, A))))
print(perm)

Submission status: Accepted

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


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

cnt = bubbleSort(L,n)

for item in range(len(L)):
	if item == len(L)-1:
		print(L[item])
	else:
		print(L[item],end=" ")

print(cnt)

Submission status: Accepted

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

  return count

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

Submission status: Accepted

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

def b_s(li):
    num = 0
    for index in range(len(li)):
        for j in range(len(li) - 1, index, -1):
            if li[j] < li[j-1]:
                li[j], li[j-1] = li[j-1], li[j]
                num += 1
    return li, num

li, num = b_s(li)
print(*li)
print(num)

Submission status: Accepted

Solution code:
def bubble_sort(A, N):
    flag = True
    cnt = 0
    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(*A, sep=' ')
    print(cnt)


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int,input().split()))
flag = 1
count = 0
while flag:
    flag = 0
    for i in range(n-1,0,-1):
        if lst[i] < lst[i-1]:
            lst[i],lst[i-1] = lst[i-1],lst[i]
            flag = 1
            count = count + 1
tmp_lst = map(str,lst)
print(' '.join(tmp_lst))
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 range(1,n):
        if A[j] < A[j-1]:
            tmp=A[j]
            A[j]=A[j-1]
            A[j-1]=tmp
            flag=1
            counter+=1
for k in range(len(A)):
    if k != len(A)-1:
        print(A[k],end=' ')
    else:
        print(A[k])
print(counter)

Submission status: Accepted

Solution code:
# ALDS1_2_A: Bubble Sort
#N = 5
#A = [int(x) for x in "5 3 2 4 1".split()]

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


def bubbleSort(A, N):
    flag = True
    i = 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
                i += 1
                #print(A)
    print(" ".join(map(str, A)))
    print(i)
    
bubbleSort(A, N)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    cnt = 0
    flag = 1
    
    while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j] < A[j-1]:
                A = A[:j-1] + A[j-1:j+1][::-1] + A[j+1:]
                cnt += 1
                flag = 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()))
flag = 1
cnt = 0
while flag:
  flag = 0
  for i in range(len(a)):
    if i > 0 and a[i] < a[i-1]:
      a[i],a[i-1] = a[i-1],a[i]
      flag = 1
      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()))
flag=True
count=0
while(flag):
    flag=0
    for i in range(N-1):
        if(A[N-1-i]<A[N-2-i]):
            v=A[N-1-i]
            A[N-1-i]=A[N-2-i]
            A[N-2-i]=v
            count+=1
            flag=True
s=" ".join(map(str,A))
print(s)
print(count)

Submission status: Accepted

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


    i += 1
for i in range(N):print(A[i],end=' \n'[i+1==N])
print(count)

Submission status: Accepted

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


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

for i in range(0, N-1):
    print(A[i], '', end='')
print(A[N-1])
print(cntswap)

Submission status: Accepted

Solution code:

def bubble(a):
	ct=0
	for i in range(len(a)-1):
		for j in range(len(a)-1,i,-1):
			if a[j]<a[j-1]:
				t=a[j]
				a[j]=a[j-1]
				a[j-1]=t
				ct+=1
	return a,ct
n=int(input())
table=list(map(int,input().split()))
b,c=bubble(table)
print(*b)
print(c)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

swap_count = 0

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
flag = False
cnt = 0
for i in range(n-1):
    if flag: break
    flag = True
    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 = False
print(" ".join(map(str,a)))
print(cnt)

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

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

if len(A) != N:
  print("[ERROR] Number of Input integers is invalid")
  sys.exit()

swap_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]
      swap_count += 1

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

Submission status: Accepted

Solution code:

# バブルソート
n = int(input())
A = list(map(int, input().split()))

#%%
def bubbleSort(A, N):
    flag = True
    i = 0 # 未ソートのindex
    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 A, cnt

ans, c = bubbleSort(A[:], n)
print(' '.join(map(str, ans)))
print(c)

Submission status: Accepted

Solution code:
N  = int(input())

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

swap_count = 0
for i in range(N-1):
    for k in range(N-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(swap_count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def bubble_sort(n, a):
  flag = True
  count = 0
  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]
        flag = True
        count += 1
  print(*a)
  print(count)

bubble_sort(int(input()), [int(i) for i in input().split()])

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    flag = True
    c = 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]
                c += 1
                flag = True
    return A,c
num = int(input())
li1 = [int(e) for e in input().split()]
li2 = bubbleSort(li1,num)
print(*li2[0],sep=" ")
print(li2[1])

Submission status: Accepted

Solution code:
n = int(input())
numL = [0 for i in range(n)]

x = input()
for i in range(n):
    numL[i] = int(x.split()[i])

flag = 1
ctr = 0
while flag:
    flag -= 1
    for i in range(n-1,0,-1):
        if numL[i] < numL[i-1]:
            ctr += 1
            tmp = numL[i]
            numL[i] = numL[i-1]
            numL[i-1] = tmp
            flag = 1
print(*numL)
print(ctr)

Submission status: Accepted

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

  return count, A

n = int(raw_input())
a = map(int, raw_input().split())
count, a = bubble(n,a)

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

Submission status: Accepted

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

c = 0 #交換回数
flag = 1
while flag:
    flag = 0
    j = N-1
    while j >= 1:
        if A[j] < A[j-1]:
            c +=1 
            v = A[j]
            A[j] = A[j-1]
            A[j-1] = v
            flag = 1
        j -=1

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

Submission status: Accepted

Solution code:
def bubble_sort(A, N):
    flag = 1
    cnt = 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
                cnt += 1
    print(' '.join(list(map(str, A))))
    print(cnt)
                

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def Bubble_Sort(n, L):
    flag = True
    j = 0
    count = 0
    while flag:
        flag = False
        for i in range(n-1, j, -1):
            if L[i] < L[i - 1]:
                L[i], L[i - 1] = L[i - 1], L[i]
                count += 1
                flag = True
    return count

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

s = Bubble_Sort(n, L)
print(*L)
print(s)

Submission status: Accepted

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

n=0
for i in range(N):
    for j in range(N-1,i,-1):
        v=L[j]
        u=L[j-1]
        
        if v < u:
            L[j]=u
            L[j-1]=v
            n+=1
            
print(' '.join(map(str,L)))
print(n)

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

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

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

def bubble_sort(A, N):
    num_exchange = 0
    FLAG = 1
    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]
                num_exchange += 1
                FLAG = 1
    show(A)
    print(num_exchange)

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

Submission status: Accepted

Solution code:
from typing import MutableSequence


def bubble_sort(a_list: MutableSequence, n:int) -> int:
    """バブルソート"""
    sw_cnt = 0
    for i in range(n - 1):
        for j in range(n -1, i, -1):
            if a_list[j - 1] > a_list[j]:
                a_list[j - 1], a_list[j] = a_list[j], a_list[j - 1]
                sw_cnt += 1

    print_list = [str(i) for i in a_list]
    print(' '.join(print_list))
    print(sw_cnt)

    
if __name__ == "__main__":
    n = int(input())
    input_list = [int(x) for x in input().split()]

    bubble_sort(input_list, n)

Submission status: Accepted

Solution code:
def print_list(A):
    print(" ".join(map(str, A)))

def swap(A, i, j):
    temp = A[i]
    A[i] = A[j]
    A[j] = temp

def bubble(A):
    N = len(A)
    sorted = False
    count = 0
    for i in range(N):
        if (sorted):
            break
        sorted = True
        for j in reversed(range(i + 1, N)):
            if A[j] < A[j - 1]:
                swap(A, j, j - 1)
                count += 1
            sorted = False
    print_list(A)
    print(count)
    

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

Submission status: Accepted

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


def bubbleSort(a, n):
    flag = 1
    ans = 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
                ans += 1

    print(*a)
    print(ans)


bubbleSort(l, n)

Submission status: Accepted

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

num_swap = 0
flag = 1

while flag:
    flag = 0
    for j in reversed(range(N - 1)):
        if li[j + 1] < li[j]:
            temp = li[j + 1]
            li[j + 1] = li[j]
            li[j] = temp
            flag = 1
            num_swap += 1

print(*li)
print(num_swap)

Submission status: Accepted

Solution code:
def main(N, A):
    count = 0
    flag = True
    i = 0
    while flag:
        flag = False
        for j in list(reversed(list(range(i + 1, N)))):
            a = A[j-1]
            b = A[j]
            if a > b:
                A[j-1] = b
                A[j] = a
                count += 1
                flag = True
        i += 1
    print(' '.join(map(str, A)))
    print(count)
    return (A, count)


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
swap = 0
while True:
    _continue = False
    for i in range(N - 1):
        if A[i] > A[i + 1]:
            swap += 1
            _continue = True
            A[i], A[i + 1] = A[i + 1], A[i]
    if not _continue:
        break
print(*A)
print(swap)

Submission status: Accepted

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

def bubble(a:list, n:int) -> None:
    if not isinstance(a, list):
        return None

    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)

bubble(a, n)

Submission status: Accepted

Solution code:
import sys
import math
from typing import Tuple
input = sys.stdin.readline

def  print_vec (A: list):
    print (" ".join(map (str, A)))

def swap (A, i, j):
    temp = A [i]
    A [i] = A [j]
    A [j] = temp
 

def bubble_sort(A: list) -> Tuple[list, int]:
    if len (A) <= 1:
        return (A, 0)
    swap_cout = 0
    for i in range(len(A)):
        for j in range(len(A) - 1, i + 1 - 1, -1):
            if A[j] < A[j - 1]:
                swap_cout += 1
                swap (A, j, j - 1)
    return (A, swap_cout)



if __name__ == "__main__":
    n = int(input())
    res = 0
    vec = list (map (int, input ().split ()))
    result=  bubble_sort(vec)
    print_vec (result [0])
    print(result [1])

Submission status: Accepted

Solution code:
n = int(input())
A = [int(a) for a in input().split()]
cnt = 0
flg = True
while flg:
    flg = False
    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
            cnt += 1
            flg = True
A = [str(a) for a in A]
print(" ".join(A))
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

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

count = 0

print(*sorted(seq))

for i in range(len(seq)):
    for j in reversed(range(i + 1, len(seq))):
        if seq[j] < seq[j - 1]:
            seq[j], seq[j - 1] = seq[j - 1], seq[j]
            count += 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
data = list(map(int, input().split()))
count = 0

for i in range(1, N):
    for j in range(N-i):
        if data[N-1-j] < data[N-1-j-1]:
            data[N-1-j], data[N-1-j-1] = data[N-1-j-1], data[N-1-j]
            count += 1
            
print("%d" %data[0], end="")
for i in range(1, N):
    print(" %d" %data[i], end="")
print()
print(count)

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 j in range(n-1, 0, -1):#が N-1 から 1 まで:
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                cnt += 1
                flag = 1
    print(*A)
    print(cnt)
    
bubbleSort(a, n)

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


N = int(input())
A = [int(i) for i in input().split()]
cnt = bubbleSort(A, N)
for i in range(len(A)):
    if i:
        print(" ", end = "")
    print(A[i], end = "")
print()
print(cnt)

Submission status: Accepted

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

for j in range(N): 
    for i in range(N-1,j,-1): 
        v = A[i] 
        if A[i-1] > A[i]: 
            A[i] = A[i-1] 
            A[i-1] = v 
            cnt += 1
        else:
            continue

print(*A)
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
num_list = [int(item) for item in input().split()]

def bubbleSort(A, N):
    is_flag = 1
    cnt = 0

    while is_flag:
        is_flag = 0
        for i in range(N-1):
            if A[i] > A[i+1]:
                A[i], A[i+1] = A[i+1], A[i]
                is_flag = 1
                cnt += 1
    print(*A)
    print(cnt)

bubbleSort(num_list, N)

Submission status: Accepted

Solution code:
def bubble_sort(a, n):
    j = 0
    is_loop = True
    count = 0
    
    while is_loop:
        is_loop = False
        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
                is_loop = True
        j += 1
        
    return count


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

count = bubble_sort(A, N)

print(*A)
print(count)