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


def bubbleSort(list_a, n):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for j in list(range(1,n))[::-1]:
            if list_a[j] < list_a[j-1]:
                (list_a[j], list_a[j-1]) = (list_a[j-1], list_a[j])
                count += 1
                flag = 1
    return list_a,count
list_n,count = bubbleSort(list_n, n)
print(" ".join(str(number) for number in list_n))
print(count)

Submission status: Accepted

Solution code:
def BubbleSort(A):
    nswap = 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]
                nswap += 1
    return nswap, A

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

ns, sa = BubbleSort(a)
print(' '.join(map(str, sa)))
print(ns)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

    ans = 0

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

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

main()

Submission status: Accepted

Solution code:
n =  int(input())
a = 0

if n <= 0 or 1000 <= n:
  exit()
list1 = list(map(int, input().split()))
#i = 0の時はエラーが出る
for i in range(n-1, 0, -1):
  j = i
  while (j < n) and (list1[j] < list1[j-1]):
    tmp = list1[j-1]
    list1[j-1] =  list1[j]
    list1[j] = tmp
    j += 1
    a += 1
print(' '.join(map(str, list1)))
print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#60
#ALDS1_2_A
n=int(input())
*l,=map(int,input().split())
flag=0

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
neighbor = True
change_count = 0
while neighbor:
    neighbor = False
    for i in range(n-1, 0, -1):
        if a[i] < a[i-1]:
            change_count += 1
            a_tmp = a[i]
            a[i] = a[i-1]
            a[i-1] = a_tmp
            neighbor = True
for i in range(n-1):
    print(a[i], end=" ")
print(a[n-1])
print(change_count)

Submission status: Accepted

Solution code:
#! python3
# insertion.py - 標準入出力を行う

N = int(input())
l = input().split(' ')
A = [int(i) for i in l]
# print(A)

def insertion(A, N): # N個の要素を含む0-オリジンの配列A
    flag = 1 # 逆の隣接要素が存在する
    i = 0 # 未ソート部分列の先頭インデックス
    counter = 0
    while flag == 1:
        flag = 0
        if i != N:
            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
                    flag = 1
                    counter += 1
            i += 1
        map_list = map(str, A)
        a = ' '.join(map_list)
    print(a)
    print(counter)

insertion(A, N)

Submission status: Accepted

Solution code:
def main():
    input()
    times = 0
    sequence = list(map(int, input().split()))
    for i in range(len(sequence)):
        for j in range(len(sequence)-1, i, -1):
            if sequence[j-1] > sequence[j]:
                tmp = sequence[j]
                sequence[j] = sequence[j-1]
                sequence[j-1] = tmp
                times += 1
    print(' '.join(map(str, sequence)))
    print(times)


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

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

Submission status: Accepted

Solution code:
n = 0
dat = []

swap = 0


def input_file():
    global n, A
    f = open('input', mode='r')
    n = int(f.readline())
    *A, = map(int, f.readline().split(' '))


def input_cmd():
    global n, A
    n = int(input())
    *A, = map(int, input().split(' '))


input_cmd()

# input_file()

ans = 0
for _ 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

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

print(*A)
print(ans)

Submission status: Accepted

Solution code:
def b_sort(n, l):
    cn = 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]
                cn += 1

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


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

Submission status: Accepted

Solution code:
cnt = int(input())
numbers = list(map(int, input().split(' ')))


def bubblesort(A, N):
    swap_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]
                swap_cnt += 1
                # ループで一回でもswapしたらTrueとなる
                flag = True

    return A, swap_cnt


numbers, swap_cnt = bubblesort(numbers, cnt)
print(' '.join(map(str, numbers)))
print(swap_cnt)

Submission status: Accepted

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

_, array = input(), input().split()

result, count = bubble_sort(array)

print(' '.join(result))
print(count)

Submission status: Accepted

Solution code:
def BubbleSort(A):
    n = len(A)
    ans = 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]
                ans += 1
    print(' '.join([str(i) for i in A]))
    print(ans)

_ = input()
BubbleSort([int(i) for i in input().split()])

Submission status: Accepted

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

r_exists = True
s_count = 0

while r_exists == True:

    r_exists = False

    i = N - 1

    while i >= 1:

        if A[i] < A[i - 1]:
            
            A[i], A[i - 1] = A[i - 1], A[i]

            s_count += 1

            r_exists = True
        
        i -= 1

s = ''

for x in A:

    s = s + str(x) + ' '

print(s.rstrip())
print(s_count)

Submission status: Accepted

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

while flag:
    flag = 0
    for j in range(N-1, 0, -1):
        if A[j - 1] > A[j]:
            A[j - 1], A[j] = A[j], A[j - 1]
            swap_counter += 1
            flag = 1
A_str = [str(a) for a in A]
result = ' '.join(A_str)
print(result)
print(swap_counter)

Submission status: Accepted

Solution code:
def bubble_sort(nums, n):
    c = 0

    loop = True
    while loop:
        loop = False

        for j in range(n-1, 0, -1):
            if nums[j] < nums[j-1]:
                nums[j], nums[j-1] = nums[j-1], nums[j]
                loop = True
                c += 1

    return (c, nums)

def main():
    n = int(input())
    nums = [int(x) for x in input().split()]

    c, nums = bubble_sort(nums, n)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# Aはリストを表している
# Nは要素数を表している
N = int(input())
A = list(map(int, input().split()))

# バブルソートの処理
flag = 1
i = 0  # 未ソート部分列の先頭インデックス
count = 0
while flag:
    flag = 0

    # pythonで逆順にするのは組み込み関数reversedを使用する。またrange関数とも一緒に使うことができる。
    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
    i += 1

print(*A)
print(count)
# バブルソートは、挿入ソートと同様に、各計算ステップにおいて、配列は「ソート済みの部分列」と「未ソートの部分列」とに分けられる。
# ソート済み | 未ソート

# バブルソート
# リストにおいて隣り合う二つの要素の値を比較して条件に応じた交換を行う整列アルゴリズムである。
# その条件は値の大小関係である。
# このソートを実行すると値の大きいまたは小さい値が浮かび上がって見えることからバブルソートと呼ばれている。

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubble_sort(lis, len):
    flag = 1
    times = 0
    while flag:
        flag = 0
        for i in range(len - 1, 0, -1):
            if lis[i] < lis[i - 1]:
                ele = lis[i]
                lis[i] = lis[i - 1]
                lis[i - 1] = ele
                times += 1
                flag = 1
    return times


inputs = []
while True:
    try:
        inputs.append(input())
    except(EOFError):
        break

ln = int(inputs[0])
li = list(map(int, inputs[1].split()))

t = bubble_sort(li, ln)

print(*li)
print(t)

Submission status: Accepted

Solution code:
def bubbles(N):
    count = 0
    for i in range(len(N)):
        for j in range(len(N)-1, i, -1):
            if N[j] < N[j-1]:
                N[j], N[j-1] = N[j-1], N[j]
                count += 1
    c = 1
    for i in N:
        print(i, end='')
        if c < len(N):
            print(' ', end='')
            c += 1
    print('')
    return count
 
n = int(input())
numbers = list(map(int, input().split()))
 
print(bubbles(numbers))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s = s.split(" ")
for i in range(n):
    s[i] = int(s[i])
flag = 1
count = 0
while flag:
    flag = 0
    for i in range(1,n):
        if s[i] < s[i-1]:
            v = s[i]
            s[i] = s[i-1]
            s[i-1] = v
            flag = 1
            count += 1
for i in range(n-1):
    print(s[i],end = " ")
print(s[n-1])
print(count)

Submission status: Accepted

Solution code:
# Bubble sort
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]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag = 1
                count += 1
    return count
N = int(input())
A = [int(i) for i in input().split(' ')]
c = bubble_sort(A,N)
print(' '.join(list(map(str, A))))
print(c)

Submission status: Runtime Error

Solution code:
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

swap_count=0

for i in range(0,len(table-1)):
    for k in range(len(table)-1,i-1):
        if table[k-1]>table[k]:
            table[k-1],table[k]=table[k],table[k-1]
            swap_count+=1

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

Submission status: Runtime Error

Solution code:
if __name__ == '__main__'
    N = input()
    N = int(N)
    array = list(map(int,  input().split()))
    cnt = 0
    for i in range(N):
    # print(' '.join(map(str, array)))
        for j in range(N-1, i, -1):
            if array[j] < array[j-1]:
                array[j], array[j-1] = array[j-1], array[j]
                cnt += 1
    print(' '.join(map(str, array)))
    print("%d" % (cnt))

Submission status: Runtime Error

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

count = 0

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

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

Submission status: Runtime Error

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

c = 0
while flag:
    flag = False
    for j in range(n-1,1,-1):
        if(a[j] < a[j-1]):
            a[j],a[j-1] = a[j-1],a[j]
            flag = 1
            c += 1
print(*a)
print(c)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
c = 0
n = input()
a = map(int, input_raw().split())

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

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

Submission status: Runtime Error

Solution code:
def main():
	c = 0
	n = input()
	a = map(int, input_raw().split())

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

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

main()

Submission status: Runtime Error

Solution code:
def bubble_sort(A):
    cnt = 0
    for i in range(len(A) - 1):
        for j in reversed(range(i+1, len(A))):
            if A[j] < A[j - 1]:
                cnt += 1
                tmp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = tmp
    return cnt

cnt = 0
amount = int(input())
nl = list(int(input()) for i in range(amount))

cnt = bubble_sort(nl)

for i in range(amount - 1):
    print(nl[i], end=" ")
print(nl[amount - 1])
print(cnt)

Submission status: Runtime Error

Solution code:
def bubbleSort(list_, n):
    list_ = list_[:]
    count = 0
    for i in xrange(n):
        for j in xrange(n-1, i, -1):
            if list_[j] < list_[j - 1]:
                list_[j], list_[j - 1] = list_[j - 1], list_[j]
                count += 1

    return list_

n = input()
nums = raw_input().split()
sorted_, count = bubbleSort(nums, n)
print sorted
print count

Submission status: Runtime Error

Solution code:
def bubbleSort(list_, n):
    list_ = list_[:]
    count = 0
    for i in xrange(n):
        for j in xrange(n-1, i, -1):
            if list_[j] < list_[j - 1]:
                list_[j], list_[j - 1] = list_[j - 1], list_[j]
                count += 1

    return list_

n = input()
nums = map(int, raw_input().split())
sorted_, count = bubbleSort(nums, n)
print sorted
print count

Submission status: Runtime Error

Solution code:
num = int(raw_input())
line = raw_input()

A_str = line.strip().split(" ")
print " ".join(A_str)
A = map(int, item_str)

for i in range(1, num):
    v = A[i]
    j = i - 1
    for j in range(j, -1, -1):
        if A[j] > v:
            A[j+1] = A[j]
            j -= 1
        else:
            break
    A[j+1] = v
    A_str = map(str, A)
    print " ".join(A_str)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
A = list(map(int,s.split()))
flag = 1
cnt = 0

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

for k in range(0,len(A)-1):
    print(A[k],end=" ")
print(A[len(A)-1])
print (cnt))

Submission status: Runtime Error

Solution code:
N = int(raw_input())
num_list = map(int, raw_input().split())
c = 0
flag = 1
while flag:
    flag =0
    for j in range(N-1,0,-1):
        if num_list[j] < num_list[j-1]:
            a = num_list[j]
            num_list[j] = numlist[j-1]
            num_list[j-1] = a
            c += 1
            flag =1
print " ".join(map(str,num_list))
print c

Submission status: Runtime Error

Solution code:
N = int(raw_input())
num_list = map(int, raw_input().split())


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

Submission status: Runtime Error

Solution code:
N = int(raw_input())
num_list = map(int, raw_input().split())

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

Submission status: Runtime Error

Solution code:
N = int(raw_input())
num_list = map(int, raw_input().split())

c = 0
flag = 1
while flag==1:
    flag =0
    for j in range(N-1,0,-1):
        if num_list[j] < num_list[j-1]:
            a = num_list[j]
            num_list[j] = numlist[j-1]
            num_list[j-1] = a
            c += 1
            flag =1

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

Submission status: Runtime Error

Solution code:
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
    return A, count

n = int(input())
A = [int(input()) for i in range(n)]

ans, count =  (BubbleSort(A,n))
ans = list(map(str,ans))


print (" ".join(ans))
print (count)

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(input()) for i in range(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
    return A, count

ans, count =  (BubbleSort(A,n))
ans = list(map(str,ans))

print (" ".join(ans))
print (count)

Submission status: Runtime Error

Solution code:
def BubbleSort1(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]
                flag = 1
                count += 1
    return A, count

n = int(input())
A = [int(input()) for i in range(n)]


ans, count =  (BubbleSort1(A,n))
ans = list(map(str,ans))

print (" ".join(ans))
print (count)

Submission status: Runtime Error

Solution code:
def BubbleSort1(A,n):
    flag = 0
    count = 0
    while flag:
        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

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

ans, count =  (BubbleSort(A,n))
ans = list(map(str,ans))

print (" ".join(ans))
print (count)

Submission status: Runtime Error

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

n = int(input())
A = [int(input()) for i in range(n)]

ans, count =  (BubbleSort(A,n))
ans = list(map(str,ans))

print (" ".join(ans))
print (count)

Submission status: Runtime Error

Solution code:
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]
                print (A[j], A[j-1])
                count += 1
    return A, count

n = int(input())
A = [int(input()) for i in range(n)]

ans, count =  (BubbleSort(A,n))
ans = list(map(str,ans))

print (" ".join(ans))
print (count)

Submission status: Runtime Error

Solution code:
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]
                print (A[j], A[j-1])
                count += 1
    return A, count


n = int(input())
A = [int(input()) for i in range(n)]
count = 0
ans = 0
ans, count =  (BubbleSort(A,n))
ans = list(map(str,ans))

print (" ".join(ans))
print (count)

Submission status: Runtime Error

Solution code:
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]
                print (A[j], A[j-1])
                count += 1
    return A, count


n = int(input())
A = [int(input().strip()) for i in range(n)]
count = 0
ans = 0
ans, count =  (BubbleSort(A,n))
ans = list(map(str,ans))

print (" ".join(ans))
print (count)

Submission status: Runtime Error

Solution code:
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]
                print (A[j], A[j-1])
                count += 1
    return A, count


n = int(input())
A = [int(input().rstrip(" ")) for i in range(n)]
count = 0
ans = 0
ans, count =  (BubbleSort(A,n))
ans = list(map(str,ans))

print (" ".join(ans))
print (count)

Submission status: Runtime Error

Solution code:
count = 0

a = int(input())
a, b, c, d, e = map(int, input(). split())
list1 = [a ,b, c, d, e]

for i in range(len(list1)):
  for n in range(len(list1) - i - 1):
    if(list1[n] > list1[n + 1]):
      tmp = list1[n]
      list1[n] = list1[n + 1]
      list1[n + 1] = tmp
      count += 1

for i in range(len(list1)):
  print(list1[i], end = " ")
print("\n%d" % (count))

Submission status: Runtime Error

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


def bubble_sort(a, N):
    change = 0
    for i in xrange(N - 1):
        for j in xrange(N - 1, i, -1):
            if a[j - 1] < a[j]:
                temp = a[j - 1]
                a[j - 1] = a[j]
                a[j] = temp
                change += 1
    return (a, change)


if __name__ == "__main__":
    N = input()
    a = map(int, raw_input().split())
    # a = [5, 2, 4, 6, 1, 3]
    result, change = bubble_sort(a, N)
    print print ','.join(map(str, result))
    print change

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
f = 1 
c = 0
while f == 1:
    f = 0
    for j in range(n-1,0,-1):
	if A[j] < A[j-1]:
            inc = A[j]
	    A[j] = A[j-1] 
	    A[j-1] = inc
	    f = 1
	    c += 1
print(" ".join(map(str,A)))
print(c)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
f = 1 
c = 0
while f == 1:
    f = 0
    for j in range(n-1,0,-1):
        if A[j] < A[j-1]:
        inc = A[j]
        A[j] = A[j-1] 
        A[j-1] = inc
        f = 1
        c += 1
print(" ".join(map(str,A)))
print(c)

Submission status: Runtime Error

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


class Solution:
    """
    @param prices: Given an integer array
    @return: Maximum profit
    """

    @staticmethod
    def swap(a, b):
        a, b = b, a
        return a, b

    @staticmethod
    def bubble_sort(self):
        # write your code here
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

        flag = 1
        count = 0
        while flag:
            flag = 0
            for j in range(array_length - 1, 0, -1):
                if unsorted_array[j] < unsorted_array[j - 1]:
                    self.swap(unsorted_array[j], unsorted_array[j - 1])
                    flag = 1
                    count += 1
        print(" ".join(map(str, unsorted_array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Submission status: Runtime Error

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

''' ?????????????????? '''

# ?¨???????O(n^2)


def bubble_sort(A, N):
    swap_num = 0  # ?????????????????°

    for i in range(N - 1):
        for j in range(N - 1, i, -1):
            if A[j] < A[j - 1]:
                # swap
                temp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = temp
                swap_num += 1

    return (A, swap_num)


if __name__ == "__main__":
    N = input()
    A = map(int, raw_input().split())
    # A = [5, 2, 4, 6, 1, 3]
    # N = len(A)
    array_sorted, swap_num = bubble_sort(A, N)
    print(' '.join(map(str, array_sorted)))
    print(swap_num)

Submission status: Runtime Error

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

''' bubble sort '''

# O(n^2)


def bubble_sort(A, N):
    swap_num = 0  # the number of swap operations

    for i in range(N - 1):
        for j in range(N - 1, i, -1):
            if A[j] < A[j - 1]:
                # swap
                temp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = temp
                swap_num += 1

    return (A, swap_num)


if __name__ == "__main__":
    N = input()
    A = map(int, raw_input().split())
    # A = [5, 2, 4, 6, 1, 3]
    # N = len(A)
    array_sorted, swap_num = bubble_sort(A, N)
    print(' '.join(map(str, array_sorted)))
    print(swap_num)

Submission status: Runtime Error

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

''' ?????????????????? '''

# O(n^2)


def bubble_sort(A, N):
    swap_num = 0  # ?????????????????°

    for i in range(N - 1):
        for j in range(N - 1, i, -1):
            if A[j] < A[j - 1]:
                # swap
                temp = A[j]
                A[j] = A[j - 1]
                A[j - 1] = temp
                swap_num += 1

    return (A, swap_num)


if __name__ == "__main__":
    N = input()
    A = list(map(int, input().split()))
    # A = [5, 2, 4, 6, 1, 3]
    # N = len(A)
    array_sorted, swap_num = bubble_sort(A, N)
    print(' '.join(map(str, array_sorted)))
    print(swap_num)

Submission status: Runtime Error

Solution code:

# coding:utf-8

def bubble_sort(list):
    n = len(list)-1 ; count=0
    for i in range(0,n):
        for j in range(n, i, -1):
            if list[j-1]>list[j]: temp=list[j] ; list[j]=list[j-1] ; list[j-1]=temp ; count+=1
                print(list) ; print(count)


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

Submission status: Runtime Error

Solution code:

# coding:utf-8

def bubble_sort(list):
    n = len(list)-1 ; count=0
    for i in range(0,n):
        for j in range(n, i, -1):
            if list[j-1]>list[j]: temp=list[j] ; list[j]=list[j-1] ; list[j-1]=temp ; count+=1
    print(" ".join(list)) ; print(count)


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

Submission status: Runtime Error

Solution code:
N = int(input())

A = (int(n) for n in input().split())

flag = 1
num_c  = 0
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
            num_c += 1
            flag = 1
print((" ").join((str(a) for a in A)))
print(num_c)

Submission status: Runtime Error

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

def trace(A):
    for index, v in enumerate(A):
        print(v, end='')
        if index != len(A) - 1:
            print(' ', end='')
    print()

def bubble_sort(A, n):
    i = n - 1
    counter = 0
    while i >= 0:
        j = i - 1
        while j >= 0:
            if A[i] < A[j]:
                A[i], A[j] = A[j], A[i]
                counter += 1
            j -= 1
        i -= 1
    trace(A)
    print(counter)

bubble_sort(A, 20)

Submission status: Runtime Error

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

def trace(A):
    for index, v in enumerate(A):
        print(v, end='')
        if index != len(A) - 1:
            print(' ', end='')
    print()

def bubble_sort(A, n):
    i = n - 1
    counter = 0
    while i >= 0:
        j = i - 1
        while j >= 0:
            if A[i] < A[j]:
                A[i], A[j] = A[j], A[i]
                counter += 1
            j -= 1
        i -= 1
    trace(A)
    print(counter)

bubble_sort(A, n)

Submission status: Runtime Error

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

def trace(A):
    for index, v in enumerate(A):
        print(v, end='')
        if index != len(A) - 1:
            print(' ', end='')
    print()

def bubble_sort(A, n):
    i = n - 1
    counter = 0
    while i >= 1:
        j = i - 1
        while j >= 0:
            if A[i] < A[j]:
                A[i], A[j] = A[j], A[i]
                counter += 1
            j -= 1
        i -= 1
    trace(A)
    print(counter)

bubble_sort(A, n)

Submission status: Runtime Error

Solution code:
# coding: utf-8

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

def main():
    trash = input()
    ls = list(map(int, input))

    i = bubblesort(ls)

    print(ls)
    print(i)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#inclide<stdio.h>
int main(void)
{
  int i, j, temp;
scanf("5d)

  for (i = 0; i < (array_size - 1); i++) {
    for (j = (array_size - 1); j > i; j--) {
      if (numbers[j-1] > numbers[j]) {
        temp = numbers[j-1];
        numbers[j-1] = numbers[j];
        numbers[j] = temp;
printf("%d" ,temp);
}
    }
  }
return 0;

}

Submission status: Runtime Error

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

bubble(A, N)