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 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()))
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]
            count += 1
            flag = 1
print(" ".join(map(str,a)))

print(count)

Submission status: Accepted

Solution code:
def inputInline():
    N=int(input())
    numbers=list(map(int,input().split(" ")))
    return numbers
def bubbleSort(list):
    N=len(list)
    count=0
    flag=True
    while flag:
        flag=False
        for i in range(N-1):
            if list[i]>list[i+1]:
                temp=list[i]
                list[i]=list[i+1]
                list[i+1]=temp
                flag=True
                count+=1
    return (list,count)
result=bubbleSort(inputInline())
print(" ".join(list(map(str,result[0]))))
print(result[1])

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

def main():
	n = int(input())
	data = list(map(int, input().split()))

	c = bubblesort(data, n)
	print(*data)
	print(c)

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
# coding:utf-8
n = int(input())
nums = [int(x) for x in input().split(" ")]
flag = True
count = 0
while flag:
    flag = False
    for i in range(n - 1, 0, -1):
        if nums[i] < nums[i - 1]:
            nums[i], nums[i - 1] = nums[i - 1], nums[i]
            flag = True
            count += 1
print(" ".join([str(i) for i in nums]))
print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

flag = 1
i = 0
count = 0

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

for w in range(n):
    if w != n-1:
        print(a[w],end=" ")
    else:
        print(a[w])
print(count)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    ret = 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
                ret += 1
    return ret


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 bubblesort(N, A):
    c, flag = 0, 1
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j- 1], A[j]
                c += 1
                flag = True
    return (A, c)

A, c = bubblesort(int(input()), list(map(int, input().split())))
print(' '.join([str(v) for v in A]))
print(c)

Submission status: Accepted

Solution code:
def bubbleSort(lst, n):
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in range(n-1, 0, -1):
            if lst[j] < lst[j-1]:
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp
                flag = 1
                i = i + 1
    print(" ".join([str(n) for n in lst]))
    print(i)


if __name__ == "__main__":
    n = int(input())
    lst = [int(n) for n in input().split()]
    bubbleSort(lst, n)

Submission status: Accepted

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

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

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

bubbleSort(A, n)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

for i in range(n):
	if i != n-1:
		print(a[i]),
	else:
		print(a[i])

print cnt

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

A,count = bubbleSort(A,N)

for i in range(len(A)):  # output
    if i == len(A) - 1:
        print(A[i])
    else:
        print(A[i], end=' ')

print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split(" ")]
x=0
flg=1
while flg:
    flg=0
    for i in range(n-1,0,-1):
        if a[i-1] > a[i]:
            a[i],a[i-1] = a[i-1],a[i]
            x+=1
            flg=1
print(" ".join([str(i) for i in a]))
print(x)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

    flag = True
    count = 0
    index = 0
    while flag :
        flag = False
        for i in reversed(range(index+1, n)) :
            if nums[i-1] > nums[i] :
                nums[i-1], nums[i] = nums[i], nums[i-1]
                count += 1
                flag = True
        index += 1

    nums_str = [str(i) for i in nums]
    print(" ".join(nums_str))
    print(count)

if __name__ == '__main__' :
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    s = 0
    flag = 0
    while flag == 0:
        flag = 1
        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 = 0
                s += 1
    t = str(A[0])
    for i in range(1,N):
        t = t + " " + str(A[i])
    print(t)
    print(s)

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

bubbleSort(a, n)

Submission status: Accepted

Solution code:
NN=raw_input()
N=raw_input()
A = [int(i) for i in N.split()]
swap=0

for i in range(int(NN)):
  for j in reversed(range(i+1,int(NN))):
    if A[j] < A[j-1] :
      t = A[j]
      A[j] = A[j-1]
      A[j-1] = t
      swap+=1
      
print ' '.join([str(k) for k in A])
print swap

Submission status: Accepted

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

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
line = input()
arr= line.split(" ")
nums = []
for c in range(n):
    nums.append(int(arr[c]))

flag = "true"
convert_n = 0
while flag == "true":
    flag = "false"
    for i in range(len(nums)-1, 0, -1):
        if nums[i] < nums[i-1]:
            tmp = nums[i]
            nums[i] = nums[i-1]
            nums[i-1] = tmp
            flag = "true"
            convert_n += 1
            
n_list_str = ""
for num in nums:
    n_list_str += str(num)
    n_list_str += " "
print(n_list_str.rstrip(" "))
print(convert_n)

Submission status: Accepted

Solution code:
import sys

ERROR_INPUT = 'input is invalid'


def main():
    n = get_length()
    arr = get_array(length=n)

    sourtLi, count = bubbleSort(li=arr, length=n)
    print(*sourtLi)
    print(count)
    return 0


def get_length():
    n = int(input())
    if n < 0 or n > 100:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def get_array(length):
    nums = input().split(' ')
    return [str2int(string=n) for n in nums]


def str2int(string):
    n = int(string)
    if n < 0 or n > 100:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def bubbleSort(li, length):
    count = 0
    for n in range(length):
        for n in range(1, length - n):
            if li[n] < li[n - 1]:
                li[n], li[n - 1] = li[n - 1], li[n]
                count += 1

    return li, count


main()

Submission status: Accepted

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

def bubbleSort(a, n):
    flag = 1
    i = 0
    count = 0
    while flag:
        flag = 0
        for j in range(n-1, i, -1):
            if a[j] < a[j-1]:
                a[j],a[j-1] = a[j-1],a[j]
                flag = 1
                count += 1
        i += 1
    print(*a)
    print(count)
                   
bubbleSort(a,n)

Submission status: Accepted

Solution code:
#coding: UTF-8

import sys
import math

class Algo:
  @staticmethod
  def bubbleSort(r, n):
    flag = 1
    count = 0
    while flag:
      flag = 0
      for j in range(n-1, 0, -1):
        if r[j] < r[j-1]:
          r[j], r[j-1] = r[j-1], r[j]
          count += 1
          flag = 1

    for i in range(0, n):
      if i == n-1:
        print(r[i])
      else:
        print(r[i], " ", sep="", end="")
    print(count)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split(' ')))
sw=0
flag=False
for i in range(n):
    flag=False
    for j in range(i+1,n)[::-1]:
        if a[j]<a[j-1]:
            a[j],a[j-1]=a[j-1],a[j]
            flag=True
            sw=sw+1
    if not flag: break
print(" ".join(map(str,a)))
print(str(sw))

Submission status: Accepted

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


arr_length = int(input())
arr_num = [int(i) for i in input().split(" ")]

bubbleSort(arr_num, arr_length)

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
def bubble(l):
	count = 0
	N = len(l)
	flag = 1

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

	return l, count


if __name__ == '__main__':
	n = int(input())
	l = input().split()

	for i in range(n):
		l[i] = int(l[i])

	l, c = bubble(l)
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i)
			print(c)

Submission status: Accepted

Solution code:

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



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

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

s_A, count = bubble_sort(A, n)

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

Submission status: Accepted

Solution code:
def bubble_sort_aoj(nums, len):
    '''バブルソート：隣接項の比較'''
    flag = True
    count = 0
    
    while flag:
        flag = False
        
        for i in range(len - 1, 0, -1):
            if nums[i] < nums[i-1]:
                nums[i], nums[i-1] = nums[i-1], nums[i]
                flag = True
                count += 1
    
    return nums, count

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

    ans = bubble_sort_aoj(A, N)

    print(' '.join([str(x) for x in ans[0]]))
    print(ans[1])

Submission status: Accepted

Solution code:
input()
A = [int(i) for i in input().split()]
c = 0
def bubble_sort(A):
    global c
    flag = True
    while flag:
        flag = False
        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]
                c += 1
                flag = True
bubble_sort(A)
print(*A)
print(c)

Submission status: Accepted

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


def bubbleSort(A, N):
    flag = True  # 未ソートの可能性があるか
    i = 0  # 未ソート部分の先頭
    inv_num = 0  # 転倒数
    while flag:
        flag = False
        for j in range(N - 1, i, -1):
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
        i += 1

    return A, inv_num


if __name__ == '__main__':

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

    A, inv_num = bubbleSort(A, N)
    print(" ".join(map(str, A)))
    print(inv_num)

Submission status: Accepted

Solution code:
def bubbleSort(a):
    sw = 0
    for _ in range(len(a) - 1):
        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]
                sw += 1
    return a, sw

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

a, sw = bubbleSort(a)

print(*a)
print(sw)

Submission status: Accepted

Solution code:
n=int(input())
s=input().split(' ')

flag=1
m=0

while flag:
    flag=0
    for j in range(1,n):
        j=n-j
        if int(s[j])<int(s[j-1]):
            a=s[j]
            s[j]=s[j-1]
            s[j-1]=a
            m+=1
            flag=1

print(' '.join(s))
print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    cnt = 0
    for _ in range(N):
        for j in range(N - 1, 0, -1):
            if A[j] < A[j - 1]:
                cnt += 1
                A[j], A[j - 1] = A[j - 1], A[j]
    print(*A)
    print(cnt)
N = int(input())
A = list(map(int, input().split()))
bubbleSort(A, N)

Submission status: Accepted

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

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

print(*arr)
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
count = int(input());
data = [int(n) for n in input().split(" ")];

def bubble_sort(data):
    count = len(data);
    o = 0;
    for i in range(count):
        for j in range(count - 1, i, -1):
            if data[j] < data[j - 1]:
                o += 1;
                temp = data[j];
                data[j] = data[j - 1];
                data[j - 1] = temp;
    show(data);
    print(o);

def show(data):
    print(" ".join(str(n) for n in data));

bubble_sort(data);

Submission status: Accepted

Solution code:
def bubbleSort(A,N):  # N個の要素を含む0-オリジンの配列A
    flag = 1
    i = 0  # 未ソート部分列の先頭インデックス
    while flag == 1:
        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
                i += 1
    print(*A)
    print(i)

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

bubbleSort(A,N)

Submission status: Accepted

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

def bubbleSort(nums,n):
    flag=True
    count=0
    while flag:
        flag=False
        for j in range(n-1,0,-1):
            if int(nums[j])<int(nums[j-1]):
                tmp=nums[j]
                nums[j]=nums[j-1]
                nums[j-1]=tmp
                flag=True
                count+=1

    print(' '.join(nums))
    print(count)

bubbleSort(nums,n)

Submission status: Accepted

Solution code:
# coding: UTF-8
def pprint(numlist):
  print(" ".join(map(str,numlist)))
N = int(input())
A = list(map(int,input().split()))
count = 0
flag = True
while flag:
  flag = False
  j = N - 1
  while j >= 1:
    if A[j-1] > A[j]:
      v = A[j]
      A[j] = A[j-1]
      A[j-1] = v
      flag = True
      count += 1
    j -= 1
pprint(A)
print(count)

Submission status: Accepted

Solution code:
n, data = int(input()), list(map(int, input().split()))
i, t = 0, 0
while i != n-1:
    for j in range(n-1, i, -1):
        if data[j-1] > data[j]:
            data[j-1], data[j] = data[j], data[j-1]
            t += 1
    i += 1
print(" ".join(map(str, data)))
print(t)

Submission status: Accepted

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

if __name__ == '__main__':
    length = input()
    data = [int(x) for x in input().split()]
    data_s, c = bSort(data)
    print (' '.join([str(x) for x in data_s]))
    print (c)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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],a[j+1]=a[j+1],a[j];c+=1
print(*a)
print(c)

Submission status: Accepted

Solution code:
# Bubble sort: ALDS1_2_A

def bsort(A, N):
    flag = 1
    swap = 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
                swap += 1
    return swap

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

swap = bsort(A, N)
print " ".join([repr(i) for i in A])
print swap

Submission status: Accepted

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

import sys

def bubble_sort(A):
    swap_count = 0
    for i in range(len(A)):
        for x in range(len(A)-1, i, -1):
            if A[x] < A[x-1]:
                swap_count += 1
                A[x], A[x-1] = A[x-1], A[x]

    return swap_count

n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

swap_count = bubble_sort(A)
s_list = list(map(str, A))
print(' '.join(s_list))
print(swap_count)

Submission status: Accepted

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

Submission status: Accepted

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


def bubbleSort(array):
    flag = True
    count = 0
    while flag:
        flag = False
        for j in sorted(range(1, len(array)), reverse=True):
            if array[j] < array[j - 1]:
                array[j], array[j - 1] = [array[j - 1], array[j]]
                flag = True
                count += 1
    show(array)
    return count


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

print(bubbleSort(array))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(i) for i in 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], a[j-1] = a[j-1], a[j]
                flag = True
                count += 1
    
    print(*a)
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def Bubble(A, N):
    f = 1
    c = 0
    while f:
        f = 0
        j = N - 1
        while j > 0:
            if A[j] < A[j - 1]:
                c += 1
                a = A[j]
                A[j] = A[j - 1]
                A[j - 1] = a
                f = 1
            j -= 1
    return A, c

N = int(input())
A = list(map(int, input().split(' ')))
A, c = Bubble(A, N)
ans = ' '.join(map(str, A)) + '\n' + str(c)
print(ans)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    flag = 1
    i = 0
    swapnum = 0
    while flag:
        flag = 0
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                temp = A[j-1]
                A[j-1] = A[j]
                A[j] = temp
                swapnum += 1
                flag = 1
        i = i + 1
    return swapnum
def showlist(A):
    for i in range(len(A)):
        if i==len(A)-1:
            print(A[i])
        else:
            print(A[i], end=' ')
N = eval(input())
A = list(map(int, input().split()))
swapnum = bubbleSort(A, N)
showlist(A)
print(swapnum)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
for i in range(int(n)-1):
    for j in range(int(n)-1):
        if nums[j] > nums[j+1]:
            tmp = nums[j+1]
            nums[j+1] = nums[j]
            nums[j] = tmp
            cnt += 1

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

Submission status: Accepted

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


def bubble_sort(a):
    c = 0
    flag = 1
    i = 0
    while flag:
        flag = 0

        # range(a, b, c) jがbになったときに処理中断する
        for j in range(n-1, i, -1):
            if a[j] < a[j-1]:

                a[j], a[j-1] = a[j-1], a[j]
                c += 1
                flag = 1

        i += 1

    print(*a)
    print(c)


bubble_sort(array)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

l = []
for i in sys.stdin:
    l.append(i.split())

def bubble_sort(data):
    count = 0
    for i in range(0,len(data)):
        for j in range(len(data)-1,i,-1):
            if data[j] < data[j-1]:
                count += 1
                temp = data[j]
                data[j] = data[j-1]
                data[j-1] = temp
    for i in range(0,len(data)):
        print(data[i],end='')
        print(" ",end='') if i < len(data)-1 else print()
    print(count)

for i in range(0,len(l[1])):
    l[1][i] = int(l[1][i])

bubble_sort(l[1])

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubble_sort(alist):
    """Sort alist by bubble sort.
    Returns (number of swap operations, sorted list)

    >>> bubble_sort([5, 3, 2, 4, 1])
    ([1, 2, 3, 4, 5], 8)
    """
    size = len(alist)
    count = 0

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

    return (alist, count)


def run():
    _ = int(input()) # flake8: noqa
    nums = [int(i) for i in input().split()]

    (sorted_list, num_swap) = bubble_sort(nums)

    print(" ".join([str(i) for i in sorted_list]))
    print(num_swap)


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
# -*- coding: UTF-8 -*-
#Bubble Sort

#標準入力から
N = int(input()) #要素数
A = list(map(int,input().split())) #ソート対象（スペース区切り→配列に展開）

#print('input')
#print(N)
#print(A)

flag = 1
i = 0
cnt = 0

#Sort処理（自分メモ：flagが0になったらループを抜ける）
while flag:
    flag = 0
    for j in reversed(range(i+1,N)):
        if A[j] < A[j-1]:
            tmp = A[j-1]
            A[j-1] = A[j]
            A[j] = tmp
            flag = 1
            cnt += 1
    i += 1

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

Submission status: Accepted

Solution code:
num = int(input())
cards = [int(i) for i in input().split()]

def bubble_sort(num, cards):
	flag = 1
	count = 0
	while flag:
		flag = 0
		for j in range(num-1, 0, -1):
			if cards[j] < cards[j-1]:
				card_tmp = cards[j]
				cards[j] = cards[j-1]
				cards[j-1] = card_tmp
				flag = 1
				count += 1
	return output(cards), count

def output(cards):
	sorting = [str(i) for i in cards]
	return (" ".join(sorting))

output, count = bubble_sort(num, cards)
print(output)
print(count)