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:
list_size = input()                                                                                                                                                                                         
list_str = input()                                                                                                                                                                                          
val_list = list(map(int, list_str.split()))                                                                                                                                                                 
                                                                                                                                                                                                            
def swap(first_idx, second_idx, val_list):                                                                                                                                                                  
    val = val_list[first_idx]                                                                                                                                                                               
    val_list[first_idx] = val_list[second_idx]                                                                                                                                                              
    val_list[second_idx] = val                                                                                                                                                                              
                                                                                                                                                                                                            
counter = 0                                                                                                                                                                                                 
while(True):                                                                                                                                                                                                
    finish = True                                                                                                                                                                                           
    for idx_num in range(0, int(list_size) - 1):                                                                                                                                                            
        if val_list[idx_num + 1] < val_list[idx_num]:                                                                                                                                                       
            swap(idx_num, idx_num + 1, val_list)                                                                                                                                                            
            finish = False                                                                                                                                                                                  
            counter += 1                                                                                                                                                                                    
    if finish == True: break                                                                                                                                                                                
                                                                                                                                                                                                            
print_list = ','.join(list(map(str, val_list)))                                                                                                                                                             
print_list = print_list.replace(',', ' ')                                                                                                                                                                   
print(print_list)                                                                                                                                                                                           
print(counter)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 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
                count += 1
                flag = 1
    print(' '.join(map(lambda x: str(x), A)))
    print(count)
    return(A)
N = input()
# for r in range(N):
line = raw_input()
line = line.strip().split()
line = map(lambda x: int(x), line)
bubbleSort(line, N)

Submission status: Accepted

Solution code:
# coding: utf-8

def printA(a, n):
    for i in range(n):
        print(a[i],end="")
        if i < n - 1:
            print(" ", end="")
        else:
            print()

def bubbleSort(a, n):
    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]
                count += 1
                flag = True
    return count

n = int(input().rstrip())
a = list(map(int,input().rstrip().split()))
c = bubbleSort(a, n)
printA(a,n)
print(c)

Submission status: Accepted

Solution code:
def bubblesort(array):
    flag = True
    res = 0
    while flag:
        flag = False
        for i in range(len(array)-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                res += 1
                flag = True
    return (array, res)
    
int(input())
arr, c = bubblesort([int(i) for i in input().split()])
print(*arr,sep=' ')
print(c)

Submission status: Accepted

Solution code:
def bubbleSort(A: list, N:int) -> list:
    """
    A[N]	サイズがNの整数型配列。
    i	未ソートの部分の先頭を指すループ変数で、配列の先頭から末尾に向かって移動します。
    j	未ソートの部分の隣り合う要素を比較するためのループ変数で、Aの末尾であるN-1 から開始しi+1 まで減少します。
    """
    has_finished = True
    i = 0
    count = 0
    while has_finished and len(A) > 1:
        has_finished = False
        for j in reversed(range(i + 1, N)):
            if A[j] < A[j - 1]:
                # alter position
                a, b = A[j - 1], A[j]
                A[j - 1], A[j] = b, a
                has_finished = True
                count += 1
        i += 1
    return A, count

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

print(*A)
print(count)

Submission status: Accepted

Solution code:
def show(nums):
    for i in range(len(nums)):
        if i!=len(nums)-1:
            print(nums[i],end=' ')
        else:
            print(nums[i])
def bubbleSort(A,N):
    count = 0
    flag = True
    while flag:
        flag = False
        for i in range(1,N):
            if A[i-1] > A[i]:
                A[i-1],A[i]=A[i],A[i-1]
                count+=1
                flag = True
    show(A)
    print(count)
X = int(input())
Y = list(map(int,input().split()))
bubbleSort(Y,X)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_A&lang=jp
def bubble_sort(array):
    N = len(array)
    is_reversed_items_exist = True
    reverse_time = 0

    while  is_reversed_items_exist:
        is_reversed_items_exist = False
        for i in range(N - 1):
            if array[i] > array[i + 1]:
                array[i], array[i + 1] = array[i + 1], array[i]
                is_reversed_items_exist = True
                reverse_time += 1
    return reverse_time
    
def putsarray(array):
    for i in range(len(array) - 1):
        print(array[i], end=' ')
    print(array[-1])

if __name__ == "__main__":
    N = int(input())
    array = input().split()
    array = [int(n) for n in array]
    reverse_time = bubble_sort(array)
    putsarray(array)
    print(reverse_time)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

flag = 1
counter = 0

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

show(A)
print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    '''list of int, int -> list of int(sorted)
    N = len(A)
    return list(bubble sorted)
    '''
    assert(len(A) == N)
    outA = A.copy()
    flag = True
    numofcng = 0
    while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if outA[j] < outA[j - 1]:
                outA[j], outA[j - 1] = outA[j - 1], outA[j]
                numofcng += 1
                flag = True
    return outA, numofcng

def printlist(L):
    for itm in L[:-1]:
        print(itm, end = ' ')
    print(L[-1])

n = int(input())
A = list(map(int, input().split()))
outA, num = bubbleSort(A, n)
printlist(outA)
print(num)

Submission status: Accepted

Solution code:
num = int(input())

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

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

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

Submission status: Accepted

Solution code:
n=int(input());a=list(map(int,input().split()));c=0
for i in range(n):
 for j in range(i+1,n)[::-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:
def bubbleSort(a, n):   #n個の要素を持つ配列a
    flag = 1    #逆の隣接要素あり
    i = 0       #未ソートの先頭
    count = 0
    while flag:
        flag = 0
        for j in range(n-1, i, -1): #j 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
    print(' '.join(map(str, a)))
    print(count)


#配列出力用
def trace(a, n):
    output = ''
    for i in a:
        if i == a[n-1]:
            output += str(i)
        else:
            output += str(i) + ' '
    print(output)

#配列の要素数と要素の入力
n = int(input())
a = list(map(int, input().split()))

bubbleSort(a, n)

Submission status: Accepted

Solution code:
def bobbleSort(L, n):
  cnt = 0
  flag = True
  while flag:
    flag = False
    for i in range(n-1, 0, -1):
      if L[i] < L[i-1]:
        L[i], L[i-1] = L[i-1], L[i]
        flag = True
        cnt += 1
  return L, cnt

N = int(input())
A = list(map(int, input().split()))
A, t = bobbleSort(A, N)
print(' '.join(map(str, A)), t, sep='\n')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 26 22:45:33 2019

@author: Yamazaki Kenichi
"""

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

counta = 0
flg = 1
while flg == 1:
    flg = 0
    for j in range(N-1):
        if A[j] > A[j+1]:
            temp = A[j]
            A[j] = A[j+1]
            A[j+1] = temp
            flg = 1
            counta += 1
print(' '.join(map(str,A)))
print(counta)

Submission status: Accepted

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

cnt = 0
for i in range(1, N):
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if I[j] < I[j-1]:
                tmp = I[j]
                I[j] = I[j-1]
                I[j-1] = tmp
                flag = True
                cnt += 1
print(str(I).replace(',','').replace('[','').replace(']',''))
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
flag=1
count=0
while flag:
    flag=0
    j=1
    while j < n:
        if A[j]<A[j-1]:
            A[j], A[j-1]=A[j-1], A[j]
            count+=1
            flag=1
        j+=1

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

Submission status: Accepted

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


n = int(input())
a = [int(i) for i in input().split()]
count = bubbleSort(a, n)
print(" ".join(map(str, a)))
print(count)

Submission status: Accepted

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

flag = True
count = 0

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

print(*A)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

c = bubbleSort(a, n)

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

Submission status: Accepted

Solution code:
def showSeq(seq):
  for i in range(len(seq)):
    if i<len(seq)-1: # 最后一个元素的下标为 len(seq)-1
      print(seq[i],end=' ')
    else:
      print(seq[i])
      
      
n = input()
list1 = list(map(int,input().split()))
count = 0

for i in range(len(list1)-1):
  for j in range(len(list1)-1,i,-1):
    if list1[j]<list1[j-1]:
      list1[j],list1[j-1] = list1[j-1],list1[j] 
      # python中的变量保存的是变量值所在的地址； 例如 a=5;b=5 a和b指向内存中同一个地址 id(a) == id(b)
      count+=1
showSeq(list1)
print(count)

Submission status: Accepted

Solution code:
def bubbleSort(A, N): # N 個の要素を含む 0-オリジンの配列 A
    flag = 1 # 逆の隣接要素が存在する
    counter = 0
    while flag:
        flag = 0
        for j in range(N-1,0,-1):
            if A[j] < A[j-1]:
                counter += 1
                buf = A[j]
                A[j] = A[j-1]
                A[j-1] = buf
                flag = 1
    return A, counter

if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))
    a, num = bubbleSort(a, N)
    for i in range(len(a)):
        if i != len(a)-1:
            print(a[i], end=" ")
        else:
            print(a[i])
    print(num)

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]

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

print(*A)
print(count)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

def bubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        j = N - 1
        while j > 0:
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                count += 1
                flag = 1
            j -= 1
    return count


c = bubbleSort(A, N)
for i in range(len(A)):
    if i != len(A) - 1:
        print(A[i], end=" ")
    else:
        print(A[i])
print(c)

Submission status: Accepted

Solution code:
def printer(A):
    ret = str(A[0])
    for i in range(1, len(A)):
        ret += " " + str(A[i])
    print(ret)
    return True


def bubblesort(A):
    ret = 0
    flag = True
    num = len(A)
    while flag is True:
        flag = False
        for i in reversed(range(1, num)):
            if A[i - 1] > A[i]:
                tmp = A[i - 1]
                A[i - 1] = A[i]
                A[i] = tmp
                flag = True
                ret += 1
        # num -= 1
    return ret


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

ans = bubblesort(A)
# print(A)
printer(A)
print(ans)

Submission status: Accepted

Solution code:
import sys


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


n = int(sys.stdin.readline())
a = [int(v) for v in sys.stdin.readline().strip().split()]
count = bubble_sort(a)
print(' '.join(map(str, a)))
print(count)

Submission status: Accepted

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

flag = True
count = 0
while flag:
    flag = False
    for i in range(1, n):
        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
i = 0
k = 0
while flag:
    flag = 0
    for j in range(n - 1, i, -1):
        if a[j] < a[j - 1]:
            t = a[j]
            a[j] = a[j - 1]
            a[j - 1] = t
            flag = 1
            k += 1
    i += 1
for i in range(0, n-1):
    print(a[i], end=" ")
print(a[n-1])
print(k)

Submission status: Accepted

Solution code:

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

flag = 1 #一度でも入れ替えをした
cnt = 0
while flag:
    flag = 0
    for i in range(1,N,1):
        if A[i-1] > A[i]:
            A[i-1], A[i] = A[i], A[i-1]
            cnt += 1
            flag = 1
print (' '.join(map(str,A)))
print(cnt)

Submission status: Accepted

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


N = int(input())
A = [int(i) for i in input().split()]
cnt = bubbleSort(A, N)
for i in range(len(A)):
    if i:
        print(" ", end = "")
    print(A[i], end = "")
print()
print(cnt)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_A&lang=jp
# Bubble Sort: python3
#  2018.12.05 yonezawa

import sys
input = sys.stdin.readline


def bubbleSort(l):
    cnt = 0    
    flg = True
    while True:
        flg = False
        for i in range(len(l)-1):
            if l[i] > l[i + 1]:
                (l[i],l[i+1]) = (l[i+1],l[i])
                cnt += 1
                flg = True
        if flg == False:
            break
    return l,cnt

def main():
    n = int(input())
    l = list(map(int,input().split()))
    (ans,cnt) = bubbleSort(l)
    print (*ans)
    print (cnt)
if __name__ == '__main__':
    main()

Submission status: Accepted

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


def bubble_sort(Ary):
    N = len(Ary)
    tento_su = 0
    flag = 1 # 0->整列済み
    while flag == 1: 
        flag = 0
        for i in range(N-1):
            if Ary[-1-i]<Ary[-2-i]:
                X = Ary[-1-i]
                Ary[-1-i] = Ary[-2-i]
                Ary[-2-i] = X
                flag = 1
                tento_su += 1
    return Ary, tento_su

A_sorted , tento_su = bubble_sort(A)
print(' '.join(map(str, A_sorted)))
print(tento_su)

Submission status: Accepted

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


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

a, count = bubblesort(n,a)
print(' '.join(map(str, a)))
print(count)

Submission status: Accepted

Solution code:
def main():
	n = int(input())
	r = [int(a) for a in input().split()]
	ct = bubbleSort(r, n)
	printlist(r)
	print(ct)

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


def printlist(a):
	values = map(str, a)
	print(' '.join(values))

main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

a = input().split()

a = [int(x) for x in a]

flag = True
i = 0
count = 0

while flag:
    flag = False

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

    i += 1

for i,x in enumerate(a):
    if i != len(a)-1:
        print(x, end=" ")
    else:
        print(x, end="")
print()
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

N = int(input())
aN = list(map(str, input().split()))

done = False
cnt = 0
while not done:
    done = True
    for i in range(N-1, 0, -1):
        if int(aN[i]) < int(aN[i-1]):
            tmp = aN[i]
            aN[i] = aN[i-1]
            aN[i-1] = tmp
            done = False
            cnt += 1
print(' '.join(aN))
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    _ = input()
    l = list(map(int, input().split()))
    count = 0
    is_sorted = True
    while is_sorted:
        is_sorted = False
        for i in range(len(l) - 1, 0, -1):
            if l[i] < l[i - 1]:
                l[i], l[i-1] = l[i-1], l[i]
                is_sorted = True
                count += 1

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def bubble_sort(n, nums):
    flag = 1
    cnt = 0
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if nums[i] < nums[i-1]:
                tmp = nums[i]
                nums[i] = nums[i-1]
                nums[i-1] = tmp
                flag = 1
                cnt += 1
    print(' '.join(map(str, nums)))
    print(cnt)

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

    bubble_sort(n, nums)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

# input
n=int(input())
nums=list(map(int, input().split()))
count=BubbleSort(nums)
maplist=map(str, nums)
print(' '.join(maplist))
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(num_list):
    cnt = 0
    flag = 1

    while flag:
        flag = 0
        for i in range(len(num_list)-1,0,-1):
            if num_list[i] < num_list[i-1]:
                num_list[i],num_list[i-1] = num_list[i-1],num_list[i]
                cnt += 1
                flag = 1
    return cnt

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

ans = bubble_sort(num_list)
ans_list = [str(i) for i in num_list]
print(' '.join(ans_list))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

cnt = 0

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

for i in range(n):
    if i == n-1:
        print(blist[i])
    else:
        print(str(blist[i]) + " ", end="")

print(cnt)

Submission status: Accepted

Solution code:
import sys

def main():
    #inputs = [5, 3, 2, 4, 1]
    #inputslen = len(inputs) - 1

    inputslen = int(sys.stdin.readline().rstrip())
    inputs= sys.stdin.readline()

    sortvalue = [int(i) for i in inputs.split()]
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, inputslen):
            j = inputslen - i
            compvalue = sortvalue[j]
            if compvalue < sortvalue[j-1]:
                sortvalue[j] = sortvalue[j-1]
                sortvalue[j-1] = compvalue
                flag = 1
                count += 1

    printvalue = map(str, sortvalue)
    print(' '.join(printvalue))
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x=int(input())
n=list(map(int,input().split()))
count=0
flag=0
while flag==0:
	flag=1
	a=0
	for i in range(1,len(n)):
		if n[i-1]>n[i]:
			a=n[i-1]
			n[i-1]=n[i]
			n[i]=a
			count+=1
			flag=0
print(' '.join(list(map(str,n))))
print(count)

Submission status: Accepted

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

print(*A)
print(count)

Submission status: Accepted

Solution code:
N = int(input())
A =list(map(int, input().split()))
flag = 1
c = 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]
            c += 1
            flag = 1
print(*A)
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class Bubble_sort:
    def __init__(self):
        self.swap_num=0
    
    def sort(self,n,list):
        for i in range(n):
            for j in range(i+1,n)[::-1]:
                if(list[j] < list[j-1]):
                    list[j],list[j-1]=list[j-1],list[j]
                    self.swap_num+=1
        return list




n=int(input())
data=list(map(int,input().split()))
bubble_sort=Bubble_sort()
print(" ".join(list(map(str,bubble_sort.sort(n,data)))))
print(bubble_sort.swap_num)

Submission status: Accepted

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

count = 0
flag = 1
i = 0
while flag:
    flag = 0
    for j in range(N-1, i, -1):
        if A[j] < A[j-1]:
            A[j], A[j-1] = A[j-1], A[j] #交換
            flag = 1
            count += 1
    i += 1
        
for k in range(N):
        if k > 0:
            print(' ', end='')
        print(A[k], end='')

print('')
print(count)

Submission status: Accepted

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

def bubbleSort(A, N):
    cnt = 0
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                cnt += 1
                flag = True
    return A, cnt
    
result = bubbleSort(a, n)
print(' '.join(map(str, result[0])))
print(result[1])

Submission status: Accepted

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

n=int(input())
r=list(map(int,input().split()))
flag = 1
change_num = 0
while flag:
    flag = 0
    for j in range(n-1, 0, -1):
        if r[j] < r[j-1]:
            v = r[j]
            r[j] = r[j-1]
            r[j-1] = v
            flag = 1
            change_num += 1
print_list(r)
print(change_num)

Submission status: Accepted

Solution code:
def bubbleSort(a,n):
	cnt=0
	for i in range(n-1,0,-1):
		for j in range(i):
			if a[j]>a[j+1]:
				a[j],a[j+1]=a[j+1],a[j]
				cnt+=1
	return cnt	
	
n=int(input())
a=list(map(int,input().split()))
cnt=bubbleSort(a,n)
print(' '.join(map(str,a)))
print(cnt)