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:
def bubbleSort(lst,n):
    flag = True
    count = 0
    while flag:
        flag = False
        for i in range(n-1,0,-1):
            if lst[i] < lst[i - 1]:
                lst[i],lst[i-1] = lst[i-1],lst[i]
                count += 1
                flag = True
    return lst,count


num = int(input())
lst = [int(i) for i in input().split()]

lst = bubbleSort(lst,num)
print(*lst[0])
print(lst[1])

Submission status: Accepted

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

c = 0
a = 0

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

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

Submission status: Accepted

Solution code:
def bubbleSort(A):
    steps = 0
    flag = 1
    completed_idx = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1] > A[i]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
        completed_idx += 1

    for e in A[:-1]:
        print(e, end=' ')
    print(A[-1])
    print(steps)

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

Submission status: Accepted

Solution code:
def bubbleSort(A, N): # N 個の要素を含む 0-オリジンの配列 A
    A = list(map(int, A.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] を交換
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
                count += 1
    print(" ".join(map(str,A)))
    print(count)

N = int(input())
A = input()
bubbleSort(A, N)

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

Submission status: Accepted

Solution code:
def bubble_sort(l):
    num = 0
    for index in range(len(l)):
        for low in range(len(l)-1, index, -1):
            if l[low] < l[low-1]:
                l[low], l[low-1] = l[low-1], l[low]
                num += 1

    return (l, num)
if __name__ == '__main__':
    n = int(input())
    l = list(map(int,input().split()))
    lsorted, num = bubble_sort(l)
    print(" ".join(list(map(str,lsorted))))
    print(num)

Submission status: Accepted

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

ans = 0

for i in range(x):
    for j in range(x-1):
        if li[j] > li[j+1]:
            li[j], li[j+1] = li[j+1], li[j]
            ans += 1

print(*li)
print(ans)

Submission status: Accepted

Solution code:
#! python3
# bubble_sort.py

# 泡が上がっていくようにソーティングされる

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

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

print(' '.join([str(x) for x in A]))
print(exc_num)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

flag = True
count = 0

while flag:
    flag = False
    for i in range(1, N)[::-1]:
        if A[i] < A[i-1]:
            A[i], A[i-1] = A[i-1], A[i]
            flag = True
            count += 1
print(' '.join(map(str, A)))
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(ls):
    flag=True
    i=0
    ch=0
    while flag:
        flag = False
        for j in reversed(range(i+1,len(ls))):
            if ls[j] < ls[j-1]:
                ls[j],ls[j-1] = ls[j-1],ls[j]
                flag = True
                ch += 1
        i += 1
    return ch

if __name__ == "__main__":
    ls=[]
    n=int(input())
    ls = list(map(int,input().split()))
    ch=bubble_sort(ls)
    for i in range(len(ls)):
        if i == len(ls)-1:
            print(ls[i])
        else:
            print(ls[i],end=" ")
    print(ch)

Submission status: Accepted

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

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

Submission status: Accepted

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

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
 
print(" ".join(map(str, a)))
print(counter)

Submission status: Accepted

Solution code:

def BubleSort(l):
    cnt = 0

    for idx in range(len(l), 0, -1):
        for jdx in range(1, idx):
            if l[jdx] < l[jdx-1]:
                l[jdx-1], l[jdx] = l[jdx], l[jdx-1]
                cnt += 1

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

def main():
    _ = input()
    in_list = list(map(int, input().split(' ')))
    BubleSort(in_list)

main()

Submission status: Accepted

Solution code:
def bubble_sort(A):
    flag = True
    count = 0
    while flag:
        flag = 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]
                flag = True
                count += 1
    return A, count

if __name__ == '__main__':
    n = int(input())
    A = [int(x) for x in input().split()]
    result_A, count = bubble_sort(A)
    print(*result_A)
    print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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):
    if i == N - 1:
        print("{0}".format(A[i]))
    else:
        print("{0} ".format(A[i]), end="")

print(count)

Submission status: Accepted

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

n = int(input())
a = list(map(int,input().split(' ')))
sa , c = bubbleSort(a, n)
str = " ".join([str(i) for i in sa])
print("{}\n{}".format(str,c))

Submission status: Accepted

Solution code:
def bubble_sort(args):
    count = 0
    result = args[:]
    l = len(result) - 1
    for i in range(0, l):
        for j in range(l, i, -1):
            if (result[j] < result[j-1]):
                result[j], result[j-1] = result[j-1], result[j]
                count += 1
    return result, count

if __name__ == '__main__':
    n = int(input())
    args = list(map(int, input().split()))[:n]
    r, c = bubble_sort(args)
    print(' '.join(map(str, r)))
    print(c)

Submission status: Accepted

Solution code:
def bubble_sort(l):
    cnt = 0
    i = 0
    while i <=  len(l) - 1:
        for j in range(len(l) - 1, i, -1):
            if l[j-1] > l[j]: # Swap
                tmp = l[j]
                l[j] = l[j-1]
                l[j-1] = tmp
                cnt += 1
        i += 1
    return l, cnt

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

    sl, cnt = bubble_sort(l)
    print(' '.join(map(str, sl)))
    print(cnt)

Submission status: Accepted

Solution code:
def swap(l, i, j):
    tmp = l[i]
    l[i] = l[j]
    l[j] = tmp
    return l

def bubble_sort(l):
    cnt = 0
    for i in range(0, len(l) - 1):
        flag = True
        for j in range(len(l)-1, i, -1):
            if l[j-1] > l[j]:
                l = swap(l, j-1, j)
                cnt += 1
                flag = False
        if flag:
            break

    return l, cnt

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

    sl, cnt = bubble_sort(l)
    print(' '.join(map(str, sl)))
    print(cnt)

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(n) for n in input().split()]
flag = 1
cnt = 0
while flag:
    flag = 0
    for i in range(N-1, 0, -1):
        if A[i] < A[i-1]:
            w = A[i-1]; A[i-1] = A[i]; A[i] = w
            cnt += 1
            flag = 1

for i, n in enumerate(A):
    if i == N - 1:
        print(n)
    else:
        print(n, end=' ')

print(cnt)

Submission status: Accepted

Solution code:
def bubbleSort(A, N): # N 個の要素を含む 0-オリジンの配列 A
    A = list(map(int, A.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] を交換
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
                count += 1
    print(" ".join(map(str,A)))
    print(count)
 
N = int(input())
A = input()
bubbleSort(A, N)

Submission status: Accepted

Solution code:
def BS(A, N):
    A = list(map(int, A.split()))
    flag = 1
    count = 0 
    while flag:
        flag = 0
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                unk = A[j]
                A[j] = A[j-1]
                A[j-1] = unk
                flag = 1
                count = count +1
    print(" ".join(map(str,A)))
    print(count)
 
N = int(input())
A = input()
BS(A, N)

Submission status: Accepted

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


def bubble_sort(ary, verbose=True):
    flag = True
    count = 0

    while flag:
        flag = False
        i = 0

        for j in range(N - 1, i, -1):

            if ary[j] < ary[j - 1]:
                ary[j], ary[j - 1] = ary[j - 1], ary[j]

                flag = True
                count += 1

        i += 1

    print(' '.join([str(_) for _ in ary]))

    if verbose:
        print(count)


bubble_sort(ary)

Submission status: Accepted

Solution code:
# ALDS_2_A.
# バブルソート。
from math import sqrt, floor

def intinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def show(a):
    # 配列の中身を出力する。
    _str = ""
    for i in range(len(a) - 1):
        _str += str(a[i]) + " "
    _str += str(a[len(a) - 1])
    print(_str)

def bubble_sort(a):
    # aをバブルソートする(交換回数をreturnする)。
    count = 0
    for i in range(1, len(a)):
        k = i - 1
        while k >= 0:
            # a[i]を前の数と交換していき、前の方が小さかったら止める。
            if a[k] > a[k + 1]:
                a[k], a[k + 1] = a[k + 1], a[k]
                count += 1
            else: break
            k -= 1
    return count

def main():
    N = int(input())
    A = intinput()
    count = bubble_sort(A)
    show(A); print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#coding:utf-8
n = int(input())
A = list(map(int, input().split()))

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

B = " ".join([str(num) for num in A])
print(B)
print(times)

Submission status: Accepted

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

n = int(input())
x = input().split()
y=[]
for i in range(n):
    y += [int(x[i])]
    
count = bubbleSort(y,n)
for j in range(n-1):
    print(y[j],end=' ')
print(y[n-1])
print(count)

Submission status: Accepted

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

n = int(input())
x = input().split()
y=[]
for i in range(n):
    y += [int(x[i])]
    
count = bubbleSort(y,n)
for j in range(n-1):
    print(y[j],end=' ')
print(y[n-1])
print(count)

Submission status: Accepted

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


def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x, end=' ')
    print(a[-1])


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

counter = bubble_sort(a, n)
print_list_split_whitespace(a)
print(counter)

Submission status: Accepted

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

flag = True
judge = True
count=0

while flag:
    for i in range(n-1,0,-1):
        if a[i] < a[i - 1]:
            a[i],a[i - 1] = a[i - 1], a[i] #swap
            count+=1
    if sorted(a) == a: flag = False

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

Submission status: Accepted

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


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

    print(' '.join([str(_) for _ in ary]))

    if verbose:
        print(count)


bubble_sort(ary)

Submission status: Accepted

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


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

    print(' '.join([str(_) for _ in ary]))

    if verbose:
        print(count)


bubble_sort(ary)

Submission status: Accepted

Solution code:
# リストの宣言
A = []

# 入力の最初の行に、数列の長さを表す整数 N が与えられます。
# 2 行目に、N 個の整数が空白区切りで与えられます。
LENGTH =int(input())
A = input().split()

# 変数
N = 0
M = LENGTH - 1
CHANGE = 0

# BubbleSort(A)
#  for i = 0 to A.length-1
while N <= LENGTH -1 :
    M = LENGTH - 1
#    print ('N=',N)
#      for j = A.length-1 downto i+1
    while M >= N + 1:
#       print  ('-M=',M)
#          if A[j] < A[j-1]
        if int(A[M]) < int(A[M-1]) :
            tmp = A[M-1]
            A[M-1] = A[M]
            A[M] = tmp
            CHANGE += 1
        M -= 1
    N += 1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys

if __name__ == '__main__':
    N = int(raw_input())
    l = map(int, raw_input().split())
    cnt = 0
    for i in range(0, 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]
                cnt += 1
    print ' '.join(map(str, l))
    print cnt

Submission status: Accepted

Solution code:
def bubble_sort(a):
    res = 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]
                res += 1
    return res

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def print_list():
    for i,e in enumerate(A):
        if i == N - 1:
            print(e)
        else:
            print(e, end=" ")

N = int(input())
A = [int(e) for e in input().split()]
flag = 1; cnt = 0
while flag == 1:
    flag = 0
    for i in range(N-1, 0, -1):
        if A[i] < A[i-1]:
            w = A[i]; A[i] = A[i-1]; A[i-1] = w
            cnt += 1
            flag = 1
print_list()
print(cnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from sys import stdin
def stdinput():
    return stdin.readline().strip()

def main():
    n = int(stdinput())
    A = list(map(int, stdinput().split()))

    done = True
    count = 0
    while done:
        done = 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]
                done = True
                count += 1
    
    print(' '.join(map(str, A)))
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

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

count = 0
for i in range(n):
	for j in range(n - 1, i, -1):
		if arr[j] < arr[j-1]:
			temp = arr[j]
			arr[j] = arr[j-1]
			arr[j-1] = temp
			count += 1

print_arr(arr)
print(count)

Submission status: Accepted

Solution code:
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()
	
def bubble_sort(arr):
	cnt = 0
	n = len(arr)
	for i in range(n):
		for j in range(n - 1, i, -1):
			if arr[j - 1] > arr[j]:
				temp = arr[j - 1]
				arr[j - 1] = arr[j]
				arr[j] = temp
				cnt += 1
	return cnt	
	
n = int(input())
arr = list(map(int, input().split()))

cnt = bubble_sort(arr)
print_arr(arr)
print(cnt)

Submission status: Accepted

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

swap_cnt = 0
swap_flg = True
i = 0
while swap_flg:
    swap_flg = False
    for j in range(N - 1, i, -1):
        if (arr[j] < arr[j - 1]):
            arr[j], arr[j - 1] = arr[j - 1], arr[j]
            swap_cnt += 1
            swap_flg = True
    i += 1

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

Submission status: Accepted

Solution code:
times = int(input())
numbers = list(map(int, input().split()))
count = 0

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

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

Submission status: Accepted

Solution code:
def bubble_sort(l):
    flag = 1
    count = 0
    while flag == 1:
        flag = 0
        j = len(l) - 1
        while(0 < j):
            if l[j] < l[j-1]:
                swap = l[j]
                l[j] = l[j-1]
                l[j-1] = swap
                count = count + 1
                flag = 1
            j = j - 1
    return l,count


n = int(input())

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

if len(l) != n:
    print("Error")
    exit()

l,count = bubble_sort(l)

for i in range(len(l)):
    if i == (len(l) - 1):
        print(l[i])
    else:
        print(l[i],end='')
        print(" ",end='')
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

swap_cnt = 0
swap_flg = True
i = 0
while swap_flg:
    swap_flg = False
    for j in range(N - 1, i, -1):
        if (arr[j] < arr[j - 1]):
            arr[j], arr[j - 1] = arr[j - 1], arr[j]
            swap_cnt += 1
            swap_flg = True
    i += 1

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

Submission status: Accepted

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

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
            ans += 1
for i in range(n):
    if i:
        print(" ", end = "")
    print(a[i], end = "")
print()
print(ans)

Submission status: Accepted

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

def run():
    n = int(input())
    a = list(map(int, input().split()))
    c, a = bubbleSort(a,n)
    print(' '.join([str(i) for i in a]))
    print(c)

if __name__ == '__main__':
    run()

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    cnt = 0
    for i in range(N):
        for j in range(N-1, i, -1):
            if j > 0 and A[j-1] > A[j]:
                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().split()))

if __name__ == '__main__':
    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(" ".join(map(str, A)))
    print(cnt)

Submission status: Accepted

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

if __name__ == '__main__':
    flg = True
    cnt = 0
    while flg:
        for i in range(N):
            flg = 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
                    flg = True
    print(" ".join(map(str, A)))
    print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(int, input().split(" ")))
Exchange_Number_of_times = 0
flag = True
while flag:
    flag = False
    for j in range(N - 1, 0, -1):
        f = j - 1
        if S[j] < S[f]:
            S[j], S[f] = S[f], S[j]
            flag = True
            Exchange_Number_of_times += 1
print(' '.join(map(str,S)))
print(Exchange_Number_of_times)

Submission status: Accepted

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

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

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

    bubbleSort(A, N)

main()

Submission status: Accepted

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

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

resultArray, cnt = bubbleSort(array)

print(*resultArray)
print(cnt)

Submission status: Accepted

Solution code:
A=int(input())
B=[int(i) for i in input().split()]
flag=True
s=0
while flag:
    flag=False
    for j in range(1,A):
        if B[j]<B[j-1]:
            B[j],B[j-1]=B[j-1],B[j]
            flag=True
            s+=1
for i in range(A):
    if i!=A-1:
        print(B[i],end=" ")
    else:
        print(B[i])
print(s)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
flag = 1
p = 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
            p += 1
            flag = 1
for i in range(N) :
    if i == N-1 :
        print(A[i])
    else :
        print(A[i],end=" ")
print(p)

Submission status: Accepted

Solution code:
n = int(input())
nlist = list(map(int, input().split()))
c= 0
for i in range(n):
    for k in range(i+1,n):
        j = n - 1 -k + i + 1
        if nlist[j] < nlist[j-1]:
            m = nlist[j]
            nlist[j] = nlist[j-1]
            nlist[j-1] = m
            c += 1
for i in range(n):
    if (i == n-1):
        print('{}'.format(nlist[i]), end = '')
    else : 
        print('{}'.format(nlist[i]), end = ' ')
print()
print(c)

Submission status: Accepted

Solution code:
n = int(input())
nlist = list(map(int, input().split()))
def BubbleSort(C, n):
    c = 0
    for i in range(n-1):
        for w in range(i+1, n):
            j = n-w+i
            if (C[j] < C[j-1]):
                a = C[j]
                C[j] = C[j-1]
                C[j-1] = a
                c += 1
    C.append(c)
BubbleSort(nlist, n)
for i in range(n):
    if (i == n-1):
        print('{}'.format(nlist[i]), end = '')
    else : 
        print('{}'.format(nlist[i]), end = ' ')
print()
print(nlist[-1])

Submission status: Accepted

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

c = 0
flag = 1
while flag:
    flag = 0
    for j in range(1,N):
        if a[j] < a[j-1]:
            v = a[j]
            a[j] = a[j-1]
            a[j-1] = v
            c +=1
            flag =1
print(" ".join([str(n) for n in a]))
print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

def bubbleSort(A, N):
    """N 個の要素を含む 0-オリジンの配列 A"""
    nswap = 0
    flag = True # 逆の隣接要素が存在する
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                swap(A, j, j-1) # A[j]とA[j-1]を交換
                nswap += 1
                flag = True
    return nswap

N = int(input())
A = [int(t) for t in input().split()]
if len(A) != N: raise

nswap = bubbleSort(A, N)
print(' '.join(map(str, A)))
print(nswap)

Submission status: Accepted

Solution code:
input()
arr = list(map(int, input().split()))
flag = False
count = 0
while not flag:
    flag = True
    for i in range(len(arr)-1):
        if arr[i] > arr[i+1]:
            count += 1
            flag = False
            t = arr[i+1]
            arr[i+1] = arr[i]
            arr[i] = t  
print(" ".join(map(str, arr)))
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:
n = input()
x = map(int, raw_input().split())
c = 0
for i in range(n):
    for j in range(i+1,n)[::-1]:
        if x[j] < x[j-1]:
            x[j],x[j-1] = x[j-1], x[j]
            c += 1
for e in x:
    print e,
print
print c

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin
stdin.readline()
data = [int(s) for s in stdin.readline().split()]
times = 0
for i in range(len(data)):
    for j in range(len(data)-1, i, -1):
        if data[j] < data[j-1]:
            data[j], data[j-1] = data[j-1], data[j]
            times += 1
print(*data)
print(times)

Submission status: Accepted

Solution code:
#!/usr/bin/python
i = input()
n = map(int, raw_input().split())
cnt=0

for i in xrange(len(n)):
    for j in xrange(len(n)-1, i, -1):
        if n[j] < n[j-1]:
            n[j], n[j-1] = n[j-1], n[j]
            cnt+=1
print ' '.join(map(str,n))
print cnt

Submission status: Accepted

Solution code:
n = input()
a = map(int, raw_input().split())
count = 0
for i in xrange(n):
	r = range(i+1, n)
	r.reverse()
	for j in r:
		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:
# -*- 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