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())
l = list(map(int, input().split(" ")))
f = 0
for _ in l:
    for j in range(n - 1, 0, -1):
        if l[j] < l[j - 1]:
            l[j], l[j - 1] = l[j - 1], l[j]
            f += 1
print(*l)
print(f)

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

count = 0
loop = True
while loop:
    loop = 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]
            loop = True
            count += 1

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

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 reversed(range(1, N)):
        if A[j] < A[j-1]:
            cnt += 1
            tmp = A[j]
            A[j] = A[j-1]
            A[j-1] = tmp
            flag = 1
print(' '.join([str(i) for i in A]))
print(cnt)

Submission status: Accepted

Solution code:
def BubbleSort(N ,A): # バブルソート
   count = 0
   for i in range(N): # 未ソート部分列は一つずつ減っていく
      for j in range(N-1, i, -1): # 一番後ろ(配列的にはN-1番目)から未ソート部分列の先頭(i+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())) # 「1 2 3…」のような配列入力
ans, count = BubbleSort(N, A)
print(*ans)
print(count)

Submission status: Accepted

Solution code:
import sys

def bubble_sort(size, numbers):
    swap_count = 0

    for i in range(size):
        sorted = True
        for j in range(size - 1, i, -1):
            if numbers[j] < numbers[j - 1]:
                sorted = False
                swap_count += 1
                numbers[j], numbers[j - 1] = numbers[j - 1], numbers[j]
        if sorted:
            break

    return swap_count

def main(stdin=None, stdout=None):
    if stdin is None:
        stdin = sys.stdin
    if stdout is None:
        stdout = sys.stdout

    size = int(stdin.readline().strip())
    numbers = [int(x) for x in stdin.readline().strip().split(" ")]

    swap_count = bubble_sort(size, numbers)
    stdout.write(" ".join([str(x) for x in numbers]) + "\n")
    stdout.write(str(swap_count) + "\n")

main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
numSwap = 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]
            numSwap += 1
        # print('i = '+str(i)+': '+':::j = '+str(j)+': '+ str(a))
a = [str(s) for s in a]
print(' '.join(a))
print(numSwap)

Submission status: Accepted

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


def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in reversed(range(1, N)):
            if A[i] < A[i - 1]:
                x = A[i]
                y = A[i - 1]
                A[i] = y
                A[i - 1] = x
                flag = 1

                count += 1

    print(*A)
    print(count)

main()

Submission status: Accepted

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

def bublesort(b,a):
    count =0
    for j in range(len(b)-1):
        for i in range(len(b)-1,0,-1):
            if b[i-1]>b[i]:
                b[i-1],b[i]=b[i],b[i-1]
                count +=1
    return count,b

A,B =bublesort(b,a)
print(*B)
print(A)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
flag=1
count=0
while flag==1:
    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
            count+=1
for i in range(n-1):
    print(A[i],end=" ")
print(A[n-1])
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(N-1,0,-1):
        if A[i] < A[i-1]:
          tmp = A[i]
          A[i] = A[i-1]
          A[i-1] = tmp
          flag = 1
          count += 1
print(' '.join([str(a) for a in A])) 
print(count)

Submission status: Accepted

Solution code:
# 5
# 5 3 2 4 1

# j = 0
# 

n = int(input())
arr = [ int(i) for i in input().split(" ")]

ans = 0

for i in range(len(arr) - 1):
  j = len(arr)-1
  for _ in range(len(arr)-i-1):
    if arr[j-1] > arr[j]:
      b = max(arr[j], arr[j-1])
      s = min(arr[j], arr[j-1])
      arr[j] = b
      arr[j-1] = s
      ans += 1
    j = j - 1
    
print(" ".join([str(i) for i in arr]))
print(ans)

Submission status: Accepted

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

cnt = 0
flag = 1
while flag == 1:
    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(" ".join([str(s) for s in A]))
print(cnt)

Submission status: Accepted

Solution code:
#初期data
input_number = int(input())
input_list = list(map(int, input().split()))
exchange_count = 0

#アルゴリズム
for i in range(input_number):
    for j in range(input_number-1, i, -1):
        if input_list[j] < input_list[j-1]:
            input_list[j], input_list[j-1] = input_list[j-1], input_list[j]
            exchange_count += 1
    
#処理の実行
print(" ".join(map(str, input_list)))
print(exchange_count)

Submission status: Accepted

Solution code:
def BubbleSort(seq):
    count = 0

    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(*seq)
    print(count)

def main():
    n = input()
    seq = [int(a) for a in input().split()]
    BubbleSort(seq)

main()

Submission status: Accepted

Solution code:
# Algorithms and Data Structures 1
# Bubble Sort
# Name: Ryuya Asada
# ID: s1260064


def BubbleSort(A: list):
    number_of_swaps = 0
    flag = 1
    while flag:
        flag = 0
        for j in range(len(A)-1, 0, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
                number_of_swaps += 1
    print(*A, sep=" ")
    print(number_of_swaps)


def main():
    length = int(input())
    A = [int(e) for e in input().split(" ")]
    BubbleSort(A)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def babble_sort(n, data):
    __count = 0
    for i in range(n-1):
        for j in range(n-i-1):
            if data[j] > data[j+1]:
                tmp = data[j+1]
                data[j+1] = data[j]
                data[j] = tmp
                __count += 1
    return data, __count

def show(data, count):
    for i in range(len(data)):
        if i != (len(data)-1):
            print(data[i], end = " ")
        else:
            print(data[i])
    print(count)

if "__main__" == __name__:
    n = int(input())
    data = list(map(int, input().rsplit()))
    data, count = babble_sort(n, data)
    show(data, count)

Submission status: Accepted

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

def bubleSort(A, N):
    flag = 1
    exchange = 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]
                exchange += 1
                flag = 1
    show(A)
    print(exchange)

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

bubleSort(A, N)

Submission status: Accepted

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

    flag = True
    ans = 0
    i = 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]
                ans += 1
                flag = True
        i += 1
    #N-1からiの範囲で全てif文が満たされない時(並べ替えが完了した時)，flag=Falseとなり処理が終了する

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

bubble_sort()

Submission status: Accepted

Solution code:
def printData(data) :
    for i, value in enumerate(data) :
        if i > 0 :
            print(" ", end = "")
        print(value, end = "")
    print()
    
def bubbleSort(data) :
    flag = 1
    count = 0
    for i in range(len(data)):
        if not flag :
            break
        flag = 0
        for j in reversed(range(i + 1, len(data))):
            if data[j] < data[j - 1] :
                buf = data[j]
                data[j] = data[j - 1]
                data[j - 1] = buf
                flag = 1
                count += 1
    
    printData(data)
    print(count)
    
    
input()
data = [int(char) for char in input().split()]
bubbleSort(data)

Submission status: Accepted

Solution code:
#Bubble sort

n = int(input())

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

count = 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
            count += 1

print(*A)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

flag = 1
cnt = 0

for _ in range(n-1):
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if a[i] < a[i-1]:
                a[i], a[i-1] = a[i-1], a[i]
                flag = 1
                cnt += 1
print(" ".join(map(str, a)))
print(cnt)

Submission status: Accepted

Solution code:
def BubbleSort(A, N):
    j = 0
    flag = 1
    k = 0
    while flag:

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

                A[i-1], A[i] = A[i], A[i-1]
        
                # a = A[i-1]
        
                # A[i-1] = A[i]
        
                # A[i] = a
        
                flag = 1
        
                j += 1
        # k += 1
    
    print(' '.join([str(x) for x in A]))
    print(j)

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

BubbleSort(A, N)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    A = list(map(int, input().split()))

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

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

Submission status: Accepted

Solution code:
def main():
	length = int(input())
	target = list(map(int,input().split()))
	bubbleSort(target)

def bubbleSort(target):
	flag = True
	count = 0
	while flag:
		flag = False
		for i in range(len(target)-1,0,-1):
			if target[i] < target[i-1]:
				tmp = target[i]
				target[i] = target[i-1]
				target[i-1] = tmp
				flag = True
				count += 1


	print(*target,sep=' ')
	print(count)


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
def bubblesort(a,n):
    count = 0
    for i in range(n):
        for j in reversed(range(i+1,n)):
            if a[j] < a[j-1]:
                a[j-1],a[j] = a[j],a[j-1]
                count += 1
    print(" ".join(map(str,a)))
    print(count)
N = int(input())
A = [int(s) for s in input().split()]
bubblesort(A,N)

Submission status: Accepted

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


def bubble_sort(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]
                count += 1
                flag = 1
    return A, count

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

Submission status: Accepted

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


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

a, count = bubble_sort(a, n)

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

Submission status: Accepted

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

ans = 0
f = True
while f:
    f = False
    for i in range(N - 1, 0, -1):
        if A[i-1] > A[i]:
            A[i], A[i-1] = A[i-1], A[i]
            ans += 1
            f = True
s = ''
for a in A:
    s += str(a) + ' '
print(s.rstrip())
print(ans)

Submission status: Accepted

Solution code:
#2_A
def bubble_sort(a,n):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(n-1):
            j = n - 1 - i
            if a[j] < a[j-1]:
                v = a[j]
                a[j] = a[j-1]
                a[j-1] = v
                flag = 1
                count += 1
    return a, count

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

arr, count = bubble_sort(a, n)
res = str(arr[0])
for i in range(n-1):
    res += " " + str(arr[i+1])

print(res)
print(count)

Submission status: Accepted

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

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

ans1, ans2 = Bubble_Sort(A, N)
print(' '.join(map(str, ans1)))
print(ans2)

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


def bubble_sort(lists, n):
    flag = 1
    x = 0
    while flag:
        flag = 0
        for j in range(n - 1, 0, -1):
            if lists[j] < lists[j - 1]:
                lists[j], lists[j - 1] = lists[j - 1], lists[j]
                x += 1
                flag = 1

    return lists, x


def main():
    N = int(input())
    lists = list(map(int, input().split(' ')))
    lists_answer, x = bubble_sort(lists, N)
    print(' '.join(map(str, lists_answer)))
    print(x)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

flag = 1 
trade = 0
while flag:
    flag = 0
    for j in range(1, n):
        if a[j-1] > a[j]:
            a[j], a[j-1] = a[j-1], a[j]
            flag = 1
            trade += 1
print(' '.join(list(map(str, a))))
print(trade)

Submission status: Accepted

Solution code:
N = int(input())
lst = list(map(int,input().split()))
cnt = 0
for i in range(N):
    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
print(" ".join(list(map(str, lst))))
print(cnt)

Submission status: Accepted

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

def bs(a, n):
  cnt=0
  f = True
  while f:
    f = False
    for i in range(n-1):
      v = a[i]
      if v>a[i+1]:
        a[i] = a[i+1]
        a[i+1] = v
        cnt+=1
        f = True
  return a,cnt

b,c = bs(a, n)

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

print()
print(c)

Submission status: Accepted

Solution code:
def bubble_sort(N, A):
    num = 0
    for k in range(0, N-1):
        for i in range(N-1, k, -1):
            if A[i] < A[i-1]:
                A[i], A[i-1] = A[i-1], A[i]
                num += 1
    return [num, *A]

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    ans = bubble_sort(N, A)
    print(*ans[1:])
    print(ans[0])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
'''
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
'''


def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(N - 1):
            if A[i] > A[i + 1]:
                tmp = A[i]
                A[i] = A[i + 1]
                A[i + 1] = tmp
                flag = 1
                count += 1
    print(' '.join(list(map(str, A))))
    print(count)


bubbleSort(A, N)

Submission status: Accepted

Solution code:
from sys import stdin
#stdin = open("input.txt")

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

if __name__ == "__main__":
    N = int(stdin.readline())
    A = [int(item) for item in stdin.readline().split()]
    cnt = bubbleSort(A, N)
    print(*A)
    print(cnt)

Submission status: Accepted

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


def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    count = bubble_sort(a, n)
    print(*a)
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
j = 0
flag = True
while flag:
    flag = 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]
            flag = True
            ans += 1
    j += 1
else:
    print(*a)
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = [x for x in list(map(int, input().split()))]

flag = True
count = 0
while flag:
  flag = False
  for i in reversed(range(1, len(arr))):
    if arr[i] < arr[i - 1]:
      tmp = arr[i]
      arr[i] = arr[i - 1]
      arr[i - 1] = tmp
      flag = True
      count += 1

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

Submission status: Accepted

Solution code:
import re
n=int(input())
a=list(map(int,input().split()))
change_num=0
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]
			change_num+=1
			flag=True
print(re.sub("[\[\]\,]","",str(a)))
print(change_num)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

def bubblesort(a,n):
    exc = 0
    flag = True
    while flag:
        flag = False
        for j in range(n-1,0,-1):
            if a[j] < a[j-1]:
                exc += 1
                tmp = a[j]
                a[j] = a[j-1]
                a[j-1] = tmp
                flag = True
    return exc

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

exc = bubblesort(a,n)
print(*a)
print(exc)

Submission status: Accepted

Solution code:
def b_sort(A):
    flg = True
    swap = 0
    while flg:
        flg = False
        for j in range(len(A) - 1,0,-1):
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1],A[j]
                swap += 1
                flg = True
    return swap

if __name__=='__main__':
    N = int(input())
    A = list(map(int,input().split()))
    swap = b_sort(A)
    print(*A)
    print(swap)

Submission status: Accepted

Solution code:
# ALDS1_2_A

N = int(input())
A = [int(i) for i in input().split()]
c = 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
            c += 1
print(" ".join(map(str, A)))
print(c)

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]:
            x = a[j]
            a[j] = a[j-1]
            a[j-1] = x
            flag = True
            count = count + 1

print(*a)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def print_list(a):
    out = ''
    for ai in a:
        out += str(ai) + ' '
    else:
        out = out[:-1]
    print(out)

def bubble_sort(a, n):
    flag = True
    cnt = 0
    while flag:
        flag = 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
                flag = True
    
    print_list(a)
    print(str(cnt))
    return a



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

a = bubble_sort(a, n)

Submission status: Accepted

Solution code:
def BubbleSort(A, N):
    cnt = 0  # 隣接要素の交換回数
    t = 0
    for i in range(N):   # iは未ソートの部分列の先頭のインデックス
        t = cnt
        for j in range(N-1, i, -1):     # 隣接要素を比較するのは未ソート部分列の末尾から先頭まで
            if A[j-1] > A[j]:           # 隣接要素を比較
                A[j-1], A[j] = A[j], A[j-1]     # 大小関係が逆の場合、交換
                cnt += 1        # 交換回数をカウント
        if t == cnt:
            return A, cnt

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

ans, cnt = BubbleSort(A,N)
print(*ans)    # リストの中身を値で取り出す
print(cnt)

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(n-1,0,-1):
    if A[j] < A[j-1]:
      A[j], A[j-1]=A[j-1],A[j]
      flag=1
      count += 1
a=map(str,A)
print(" ".join(a))
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
working_list = list(map(int, input().split()))
exchange_count = 0  # 交換回数、つまり、列の乱れの程度を示す反転数あるいは転倒数を記録する。
i = 0
flag = True
while flag:
    flag = False
    for j in range(N-1, i, -1):
        if working_list[j] < working_list[j-1]:
            working_list[j-1], working_list[j] = working_list[j], working_list[j-1]
            flag = True
            exchange_count += 1
    i += 1

print(*working_list)
print(exchange_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]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
                cnt +=1
    print(*A)
    print(cnt)

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

bubbleSort(ls,len(ls))

Submission status: Accepted

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

def BubbleSort(A, N):
    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)

main()

Submission status: Accepted

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

b = [str(i) for i in a]
print(" ".join(b))
print(count)

Submission status: Accepted

Solution code:
input()
A = [int(i) for i in input().split()]
flag = 1
n = 0
while flag:
    flag = 0
    for j in range(len(A) - 1, 0, -1):
        # print("debug: j = {0}".format(j))
        if A[j] < A[j - 1]:
            A[j], A[j - 1] = A[j - 1], A[j]
            flag = 1
            n += 1
print(" ".join(map(str, A)))
print(n)

Submission status: Accepted

Solution code:
#バブルソート

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

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

result = ''
for l in list:
    result += str(l) + ' '

print(result[:-1])
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(A, N):
    flag = True
    swap_count = 0
    while flag:
        flag = False
        for j in range(1, N)[::-1]:
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                swap_count += 1
                flag = True
    return A, swap_count
N = int(input())
A = list(map(int, input().split(' ')))
sorted_A, swap_count = bubble_sort(A, N)
print(" ".join(map(str, sorted_A)))
print(swap_count)

Submission status: Accepted

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

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

result,count=bubblesort(A,n)
print(" ".join([str(i) for i in result]))
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def show (a,n):
  for i in range(n):
    if i > 0:
      print("",end=" ")
    print(a[i],end="")
  print("")

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-1],a[j] = a[j],a[j-1]
        flag = 1
        count += 1
  show(a,n)
  print(count)

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

bubbleSort(a,n)

Submission status: Accepted

Solution code:
def BubbleSort(A, N):
    if N == 1:
        print(str(A[0]) + "\n" + str(0))
    else:
        count = 0
        for i in range(N): #このiは使わない
            for j in range(N - i - 1):
                if A[N - j - 1] < A[N - j - 2]: #わかりにくくなってしまった
                    count += 1
                    A[N - j - 1], A[N - j - 2] = A[N - j - 2], A[N - j - 1] 
        str_A = map(str, A)
        print(" ".join(str_A) +  "\n" + str(count))

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

BubbleSort(a, n)

Submission status: Accepted

Solution code:
# def Swap(A,B):
#     swp = A
#     A = B
#     B = swp
#     return A,B

def BubbleSort(A):
    count = 0
    for i in range(len(A)):
        for j in range(i+1,len(A))[::-1]:
            if(A[j-1] > A[j]):
                swp = A[j-1]
                A[j-1] = A[j]
                A[j] = swp
                count += 1
                # print(A)
                # print(f"count:{count}")
                # print(f"i:{i},j:{j}")
    
    return A,count

def test(a):
    return a

def outputList(A):
    if(len(A) == 1):
        print(A[0])
    else:
        for i in range(len(A)-1):
            print(f"{A[i]} ",end="")
        print(A[-1])
        

n = int(input())
numbers = list(map(int,input().split()))
ans,count = BubbleSort(numbers)
# ans = BubbleSort(numbers)
outputList(ans)
print(count)

Submission status: Accepted

Solution code:

def ALDS1_2_A():
    """
    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

    ＜問題＞
    数列 A を読み込み、バブルソートで昇順に並び変え出力するプログラムを作成してください。
    また、バブルソートで行われた要素の交換回数も報告してください。

    ＜入力＞
    入力の最初の行に、数列の長さを表す整数 N が与えられます。２行目に、N 個の整数が空白区切りで与えられます。

    ＜出力＞
    出力は 2 行からなります。１行目に整列された数列を 1 行に出力してください。数列の連続する要素は１つの空白で区切って出力してください。
    2 行目に交換回数を出力してください。

    ＜制約＞
    1 ≤ N ≤ 100
    0 ≤ A の要素 ≤ 100
    """
    N = int(input())
    A = [int(e) for e in input().split(" ")]
    flag = 1    # A[j] < A[j-1] の要素が存在するかのflag変数
    switch_cnt = 0
    idx_not_sorted = 0
    while flag:
        flag = 0
        for j in range(N-1, idx_not_sorted, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                switch_cnt += 1
                flag = 1
        idx_not_sorted += 1
    print(" ".join(map(str, A)))
    print(switch_cnt)


def main():
    ALDS1_2_A()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
flag=True
cnt=0
for i in range(n):
	if not flag:
		break
	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
print(*a)
print(cnt)

Submission status: Accepted

Solution code:
def bubble_sort(a, n):

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

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


def main():
    n = int(input())
    a = list(map(int, input().split(' ')))
    bubble_sort(a, n)


if __name__ == "__main__":
    main()