Problem:


Bubble Sort

Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

BubbleSort(A)
1 for i = 0 to A.length-1
2     for j = A.length-1 downto i+1
3         if A[j] N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by spaces characters.

Output

The output consists of 2 lines. 

In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

In the second line, please print the number of swap operations.

Constraints

1 ≤ N ≤ 100

Sample Input 1

5
5 3 2 4 1

Sample Output 1

1 2 3 4 5
8

Sample Input 2

6
5 2 4 6 1 3

Sample Output 2

1 2 3 4 5 6
9



Sample input:
5
5 3 2 4 1


Sample output:
1 2 3 4 5
8

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
i=int(raw_input())
n=map(int, raw_input().split())
ans=0
chk=10
#サンプルリスト
#n=[2,5,3,1,7,9,10,4,6,8]
#n=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#リストの長さ、要素の個数分の繰り返し回数設定
#リストの長さ、要素の個数分の繰り返し回数設定
for m in range(len(n)):
    for j in range(len(n)-1, m, -1):
        if n[j]<n[j-1]:
            n[j],n[j-1]=n[j-1],n[j]
            ans+=1
#        print ans
    if ans==chk: break
    chk=ans
print ' '.join(map(str,n))
print ans

Submission status: Accepted

Solution code:
n = int(raw_input())
elem = map(int,raw_input().split())

count = 0
for i in range(0,n):
    for j in range(n-1,-1+i,-1):
        if elem[j] < elem[j-1] and j>0:
            count += 1
            tmp = elem[j-1]
            elem[j-1] = elem[j]
            elem[j] = tmp
print " ".join(map(str,elem))
print count

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubble(n, a):
    count = 0
    for i in range(n-1):
        for j in range(n-1,i,-1):
            if a[j-1] > a[j]:
                tmp = a[j]
                a[j] = a[j-1]
                a[j-1] = tmp
                count += 1
    return count, a
    
n = int(raw_input())
a = map(int, raw_input().split())

n, a = bubble(n, a)
print " ".join(map(str, a))
print n

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    import sys
    N = int(sys.stdin.readline().strip())
    A = [int(e) for e in sys.stdin.readline().strip().split()]
    cnt = 0
    for i in range(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
    print " ".join(map(str, A))
    print cnt

Submission status: Accepted

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

Submission status: Accepted

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

def bubblesort(A, number):
    flag = True
    count, change = 0, 0
    while flag:
        flag = False
        for i in range(1, number-count):
            if A[-i-1] > A[-i]:
                A[-i-1], A[-i] = A[-i], A[-i-1]
                flag = True
                change += 1
        count += 1
    return print(*A), print(change)

bubblesort(A, N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#バブルソート
number=eval(input())
swap=0
arr=list(map(int, input().split(' ')))
for i in range(0,number-1):
    j=number-1
    while j>i:
        if arr[j]<arr[j-1]:
            arr[j],arr[j-1]=arr[j-1],arr[j]
            swap+=1
        j-=1

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

Submission status: Accepted

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

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

for k in range(n-1):
    print(str(A[k])+" ",end = "")
print(str(A[n-1]))
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

    return A, count

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

    answer_list, answer_count = bubbleSort(A, N)

    print(*answer_list)
    print(answer_count)

Submission status: Accepted

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

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(i):
		if A[j] > A[i]:
			ans += 1
A.sort()
s = str(A[0])
for i in range(1, N):
	s += " " + str(A[i])
print(s)
print(ans)

Submission status: Accepted

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

def bubbleSort(a:list, n:int):
    count = 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 #交換が一度でも発生したらflag立てる
                count += 1
    return count, a

count, a = bubbleSort(a, n)
print(" ".join(map(str, a)))
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(*A)
print(cnt)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
cnt = 0
#bubbleSort(A, N)
flag = 1
while flag:
    flag = 0
    for i in range(N - 1, 0, -1):
        if A[i] < A[i - 1]:
            a = A[i]
            b = A[i - 1]
            A[i] = b
            A[i - 1] = a
            flag = 1
            cnt += 1
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

Solution code:


def bubble_sort(arr):
    count = 0
    for j in range(len(arr)):
        for i in range(len(arr)-1, j, -1):
            if arr[i] < arr[i-1]:
                arr[i], arr[i-1] = arr[i-1], arr[i]
                count += 1
    return count, arr

nums = input()
# nums = 1
# arr = list(map(int, '100'.split()))
arr = list(map(int, input().split()))

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def bubble_sort(a, n):
    flag = 1
    switch = 0
    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
                switch += 1
    print(' '.join([str(i) for i in a]))
    print(switch)


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def bubbleSort(A):
    flag = 1
    count = 0
    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
                count += 1

    return [count,A]

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

answer = bubbleSort(lst)

print(" ".join(map(str,answer[1])))
print(answer[0])

Submission status: Accepted

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

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

for i in range(N):
    if i==N-1:
        print(S[i])
    else:
        print(S[i],end=' ')
print(count)

Submission status: Accepted

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

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

print(' '.join(str(x) for x in L))
print(cnt)

Submission status: Accepted

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

cnt = 0


# bubbleSort

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

print(" ".join([str(a) for a in A]))
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

flag = 1
sorted_index = 0
num_of_sorting = 0
while flag:
  flag = 0
  for i in range(N-1, sorted_index, -1):  # N-1から1まで降順 
    if A[i] < A[i-1]:
      temp = A[i]
      A[i] = A[i-1]
      A[i-1] = temp
      flag = 1
      num_of_sorting += 1
  sorted_index += 1

print(' '.join(map(str, A)))
print(f"{num_of_sorting}")

Submission status: Accepted

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

frag = True
count = 0
while frag:
    frag = False
    for i in reversed(range(1, n)):
        if nums[i] < nums[i-1]:
            nums[i], nums[i-1] = nums[i-1], nums[i]
            count += 1
            frag = True
print(' '.join(map(str, nums)))
print(count)

Submission status: Accepted

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


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


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

flag = True   # Continue while loop if flag == True
i = 0         # Last index of unsorted rows
count = 0     # Counts of swapping

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

print(str(A).replace('[', '').replace(']', '').replace(',', ''))
print(count)

Submission status: Accepted

Solution code:
import sys

def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('')
        
def main():
    N = int(input())
    data = list(map(int, input().split()))
    counter = 0
    
    for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j] < data[j-1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
                counter += 1
            j -= 1
        
    print_data(data)
    print(counter)
    
            
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(*lst)
print(count)

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    numbers=[]
    number = int(input())
    numbers=list(map(int,input().split()))
    sort_finished=0
    swap_count=0
    for idx in range(number-1):
        for sort_check_idx in reversed(range(number)[sort_finished+1:]):
            if numbers[sort_check_idx] < numbers[sort_check_idx-1]:
                numbers[sort_check_idx], numbers[sort_check_idx-1] = numbers[sort_check_idx-1], numbers[sort_check_idx]
                swap_count+=1
        sort_finished += 1
    print(*numbers)
    print(swap_count)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt = 0
for i in range(n-1):
    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)

Submission status: Accepted

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

n = int(input())
A = list(map(int, input().split()))
c = bubbleSort(A, n)
for i in range(n):
    if i == n - 1:
        print(A[i])
    else:
        print(A[i], end=' ')
print(c)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(input())
nums = list(map(lambda x: int(x), stdin.readline().split(' ')))

count = 0
for i in range(N-1):
    for j in reversed(range(i,N-1)):
        if nums[j] > nums[j+1]:
            nums[j], nums[j+1] = nums[j+1], nums[j]
            count += 1

output = ""
for i in nums:
    output += str(i) + ' '
print(output[:-1])
print(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:
length = int(input())
num_list = list(map(int, input().split()))

change_count = 0

for i in range(length):
    flag = 0
    for k in range(length-1,i,-1):
        if num_list[k-1] > num_list[k]:
            num_list[k-1], num_list[k] = num_list[k], num_list[k-1]
            change_count += 1
            flag = 1
    if flag == 0:
        break

print(" ".join(map(str,num_list)))
print(change_count)

Submission status: Accepted

Solution code:
# ALDS1_2_A.py
def bubbleSort(A: list, N: int):
    cnt = 0
    isEnd = False
    while not isEnd:
        isEnd = True
        for i in range(1, N):
            if A[i] < A[i-1]:
                tmp = A[i]
                A[i] = A[i-1]
                A[i-1] = tmp
                isEnd = False
                cnt += 1
    return A, cnt


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
t = list(map(int, input().split(' ')))
x, y = 0, 0
while True:
    x = 0
    for i in range(n-1):
        if t[i] > t[i+1]:
            t[i],t[i+1] = t[i+1],t[i]
            y = y + 1
        else:
            x = x + 1
    if x == n-1:
        break
print(' '.join(map(str,t)))
print(y)

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(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

Solution code:
try:
    N = int(input())
    A = list(map(int,input().split()))
except ValueError:
    print('数値以外が入力されました')

times_swap =0
for i in range(len(A)):
    for j in range(len(A)-1,i,-1):  
            # reversed(A)で配列の要素を逆順で取り出せる 
            #　配列はスライスでも逆順にできる 
            # list[start:end:stop] 
            # 逆順　list[::-1] （後ろ向きに1個ずつ止まる）
            
        if A[j] < A[j-1]:
            times_swap += 1
            tmp = A[j]
            A[j] = A[j-1]
            A[j-1] = tmp

print(*A)
print(str(times_swap))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

flag = True
n = len(A)
counter = 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
            flag = True
            counter += 1

print(" ".join(str(x) for x in A))
print(counter)

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

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    cnt = bubble_sort(A, N)
    print(*A)
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# def bubbleSort(A):
#     flag = 1
#     counter = 0
#     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
#                 counter += 1
#     return A, counter

def bubbleSort(A):
    flag = 1
    counter = 0
    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
                counter += 1
    return A, counter
    
N = int(input())
A = list(map(int, input().split()))

ans = bubbleSort(A)
print(' '.join(str(num) for num in ans[0]))
print(ans[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubble_sort(A:list) -> list:
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in range(len(A)-1, 0, -1):
            if A[i] < A[i-1]:
                A[i], A[i-1] = A[i-1], A[i]
                count += 1
                flag = 1
    print(" ".join(list(map(str, A))))
    return count

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

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                A[j] , A[j-1] = A[j-1], A[j]
                count += 1
                flag = 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:
def bubble_sort(array):
    i_n_srt = 0
    counter = 0
    flg = True
    while flg:
        flg = False
        for j in range(len(array) - 1, i_n_srt, -1):
            # print(f"i_Not_sort: {i_n_srt}, j: {j}")
            if array[j] < array[j - 1]:
                array[j], array[j - 1] = array[j - 1], array[j]
                # print(array, "\n")
                counter += 1
                flg = True
        i_n_srt += 1
    return counter, array        

N = int(input())
A = list(map(int, input().split()))
# print(f'array(start): {A}')
ans, array_new = bubble_sort(A)
print(" ".join(map(str, array_new)))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(n):
    for j in range(n-1, i, -1):
        if l[j] < l[j-1]:
            l[j], l[j-1] = l[j-1], l[j]
            count += 1
print(" ".join(map(str, l)))
print(count)

Submission status: Accepted

Solution code:
import math

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

c=0
for i in range(len(A)):
	for j in range(len(A)-1, i, -1):
            if A[j] < A[j-1]:
		A[j], A[j-1] = A[j-1], A[j]
      	      	c+=1
print ' '.join(map(str, A))
print c

Submission status: Accepted

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

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

    return cnt

res = bubbleSort(a, n)

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

Submission status: Accepted

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

counter = 0
for i in range(1,N):
    for j in reversed(range(i, N)):
        if A[j] < A[j - 1]:
            temp = A[j]
            A[j] = A[j - 1]
            A[j - 1] = temp
            counter = counter + 1

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

Submission status: Accepted

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


def bubble_sort(A):
    flag = 1  # non-sorted element exist on side
    counter = 0
    while flag:
        flag = 0
        for i in range(1, len(A))[::-1]:
            if A[i] < A[i - 1]:
                A[i], A[i - 1] = A[i - 1], A[i]
                flag = 1
                counter += 1
    print(" ".join([str(a) for a in A]))
    print(counter)


def main():
    _ = input()
    A = [int(i) for i in input().strip().split()]
    bubble_sort(A)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(*aa)
print(swaps)

Submission status: Accepted

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

reversals = 0
need_to_sort = True
while need_to_sort:
    need_to_sort = False
    for index in range(len(arr) -1, 0, -1):
        if arr[index] < arr[index-1]:
            arr[index], arr[index-1] = arr[index-1], arr[index]
            need_to_sort = True
            reversals += 1
print(" ".join(map(str, arr)), reversals, sep="\n")

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
count = 0
flag = 1
while flag:
    flag = 0
    for i in 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
print(*A,sep=' ')
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(A):
    cnt = 0
    for i in range(0, len(A) - 1):
        for j in range(len(A) - 1, i, -1):
            if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
    print(' '.join(map(str,A)))
    print(cnt)


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

Submission status: Accepted

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

flag = 1
i = 0
cnt = 0

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

Submission status: Accepted

Solution code:
def BubbleSort1(A):
    ans=0
    for i in range(len(A)):
        for j in range(len(A)-1,i,-1):
            if A[j-1]>A[j]:
                A[j],A[j-1]=A[j-1],A[j]
                ans+=1
    
    return ans

    
cnt=int(input())
a=[int(x) for x in input().split()]


ans=BubbleSort1(a)

for i in range(len(a)):
    if i:
        print(" ",end="")
    print(a[i],end="")
print()
print(ans)

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

Submission status: Accepted

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

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

print(*A)
print(cnt)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def bubble_sort(A, N):
    exchange_cnt = 0
    flg = True
   
    while flg:
        flg = False
        for i in range(1, N)[::-1]:
            if A[i - 1] > A[i]:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
                exchange_cnt += 1
    print(" ".join([str(a) for a in A]))
    print(exchange_cnt)

N = int(input().rstrip())
A = [int(num) for num in input().rstrip().split(" ")]
bubble_sort(A, N)

Submission status: Accepted

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

                flag = 1
    print(*A)
    print(count)

N = int(input())
nums = list(map(int, input().split()))
bubbleSort(nums, N)

Submission status: Accepted

Solution code:
# Bubble Sort
N = int(input())
A = list(map(int, input().split()))
swapCnt = 0
for i in range(len(A)-1):
    for j in range(len(A)-1, i, -1):
        if A[j] < A[j-1]:
            A[j], A[j - 1] = A[j - 1], A[j]
            swapCnt += 1
print(' '.join(map(str, A)))
print(swapCnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def bubbleSort(a, n):
    flag = 1
    cnt = 0
    i = 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
                cnt += 1
        i += 1
    print(' '.join(map(str, a)))
    print(cnt)

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

bubbleSort(a, n)

Submission status: Accepted

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

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

ans=str(num_list[0])
for i in range(1,len(num_list)):
    ans+=" "+str(num_list[i])
print(ans)
print(count)

Submission status: Accepted

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

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

print(*A)
print(count)