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: Time Limit Exceeded

Solution code:
N = int(input())
A = 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)

Submission status: Time Limit Exceeded

Solution code:
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)

if __name__ == "__main__":
    N = int(input())
    A = [int(i) for i in input().split(" ")]
    print(bubble(A, N))

Submission status: Time Limit Exceeded

Solution code:
import sys
x = input()
y = input().split()
r = sorted(y)
c = 0
while y != r:
    for i, j in enumerate(y[:-1]):
        if y[i] > y[i+1]:
            y[i], y[i+1] = y[i+1], y[i]
            c += 1
        else:
            break
print(" ".join(y))
print(c)

Submission status: Time Limit Exceeded

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

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


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

bubble_sort(A, N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

i = 0
count = 0
while 1:
    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
    i+=1
    if i == N-1: break

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
arr = [int(input()) for i in range(N)]

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

Submission status: Accepted

Solution code:

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

def BubbleSort(A):
    count = 0
    for i in range(len(A)):
        for j in reversed(range(i+1,len(A))):
            if A[j]<A[j-1]:
                #入れ替え
                a = A[j]
                b = A[j-1]
                A[j-1] = a
                A[j] = b
                count+=1
    return [A,count]

result = BubbleSort(li)    

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

Submission status: Accepted

Solution code:
numberOfArray=int(input())
arrayList=list(map(int,input().split()))

def bubbleSort():
    frag=1
    count=0
    while frag:
        frag=0
        for i in range(1,numberOfArray):
            if arrayList[i-1]>arrayList[i]:
                arrayList[i-1],arrayList[i]=arrayList[i],arrayList[i-1]
                frag=1
                count+=1
    arrayForReturn=str(arrayList[0])
    for i2 in range(1,numberOfArray):
        arrayForReturn+=" "+str(arrayList[i2])
    print(arrayForReturn)
    print(count)

#------------------main--------------
bubbleSort()

Submission status: Accepted

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

cnt = 0
for i in range(n) :
    for j in range(n - 1, i, -1) :
        if s[j - 1] > s[j] :
            s[j - 1], s[j] = s[j], s[j - 1]
            cnt += 1     
print(" ".join(map(str, s)))
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def bubbleSort(N, A):
    flag = 1
    count = 0
    while flag != 0:
        flag = 0
        for i in range(N - 1, 0, -1):
            if A[i] < A[i - 1]:
                k = A[i]
                A[i] = A[i - 1]
                A[i - 1] = k
                flag = 1
                count += 1
    print(" ".join(map(str, A)))
    print(count)


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Bubble Sort
n = int(input())
line = [int(i) for i in input().rstrip().split()]
# print(line)

count = 0
for i in range(n - 1):
    a = n - 1
    while a > i:
        if line[a - 1] > line[a]:
            line[a - 1], line[a] = line[a], line[a - 1]
            # print(line)
            count += 1
        a -= 1

for i in range(n):
    print(line[i], end = "")
    if i < n - 1:
        print(" ", end = "")
print()
# print(line)
print(count)

Submission status: Accepted

Solution code:
def print_list(A):
    print(*A, sep=" ")

def swap(a, b):
    return b, a

def bubble_sort(A, n):
    flg = 1 #逆の隣接要素が存在する
    i = 0
    replace_num = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
        i += 1
    print_list(A)
    print(replace_num)

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

bubble_sort(A, n)

Submission status: Accepted

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

num = 0

is_swapped = True
while is_swapped:
    is_swapped = False
    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
            is_swapped = True
            num += 1

print(' '.join([str(i) for i in a]))
print(num)

Submission status: Accepted

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

num = 0

is_swapped = True
i = 0
while is_swapped:
    is_swapped = 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
            is_swapped = True
            num += 1
    i += 1

print(' '.join([str(i) for i in a]))
print(num)

Submission status: Accepted

Solution code:
def bubble_sort(a, n):
    sw_num = 0
    is_swapped = True
    i = 0
    while is_swapped:
        is_swapped = 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
                is_swapped = True
                sw_num += 1
        i += 1

    return sw_num


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

sw_num = bubble_sort(a, n)

print(' '.join([str(i) for i in a]))
print(sw_num)

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
        w = a[j]
        a[j] = a[j-1]
        a[j-1] = w
        flag = 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:
n = int(input())
s = [ i for i in input().split()]
flag = True
cnt = 0
while flag:
    flag = False
    for i in range(n-1):
        if int(s[i]) > int(s[i+1]):
            s[i],s[i+1] = s[i+1],s[i]
            flag = True
            cnt += 1
print(' '.join(s))
print(cnt)

Submission status: Accepted

Solution code:
# inputList = list(map(int, '''5
# 5 3 2 4 1'''.split()))
# N = inputList.pop(0)


def formatting(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 = 1
    while flag:
        flag = 0

        for j in range(N - 1, 0, -1):
            if A[j] < A[j-1]:
                temp = A[j]
                A[j] = A[j-1]
                A[j-1] = temp
                count += 1
                flag = 1
    formatting(A)
    print(count)




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

bubbleSort(inputList, N)

Submission status: Accepted

Solution code:
# inputList = list(map(int, '''5
# 5 3 2 4 1'''.split()))
# N = inputList.pop(0)


def formatting(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
    i = 0
    flag = 1
    while flag:
        flag = 0

        for j in range(N - 1, i, -1):
            if A[j] < A[j-1]:
                temp = A[j]
                A[j] = A[j-1]
                A[j-1] = temp
                count += 1
                flag = 1
        i += 1
    formatting(A)
    print(count)




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

bubbleSort(inputList, N)

Submission status: Accepted

Solution code:
bit=[0 for i in xrange(200)]
n=int(raw_input().strip())
class dig:
    def __init__(self,val,id):
        self.val=val
        self.id=id
    def __gt__(self,other):
        return self.val<other.val


def sum(i):
    s=0
    i=int(i)
    while i>0:
        s+=bit[i]
        i-=i&(-i)
    return s
def add(i,x):
    i=int(i)
    while i<=n:
        bit[i]+=x
        i+=i&-i


def sort2(lst):
    lens=len(lst)
    sm=0
    for i in xrange(lens):
        for j in xrange(i+1,lens):
            if lst[i]>lst[j]:
                sm+=1
    return sm
lst=map(int,raw_input().strip().split())
vals=[]
lens=len(lst)
for i in xrange(lens):
    vals.append(dig(lst[i],i+1))
vals.sort()
ans=0
for i in vals:
    #print i.val,i.id
    ans+=sum(i.id)
    add(i.id,1)
ans=sort2(lst)
lst.sort()
print " ".join(str(pp) for pp in lst)
print ans

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def bubble_sort(num_list):
    list_length = len(num_list)-1
    cnt = 0

    for i in range(list_length):
        for j in range(list_length,i,-1):
            if num_list[j] < num_list[j-1]:
                num_list[j],num_list[j-1] = num_list[j-1],num_list[j]
                cnt += 1
    return num_list,cnt

n = int(input())
num_list = list(map(int,input().split()))
ans_list,cnt = bubble_sort(num_list)
str_ans_list = [str(i) for i in ans_list]
print(' '.join(str_ans_list))
print(cnt)

Submission status: Accepted

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

# A = [5, 3, 2, 4, 1]

def bubbleSort(arr):
    arr_len = len(arr)
    flg = True
    cnt = 0
    while flg:
        flg = False
        for i in range(arr_len -1, 0, -1):
            if arr[i] < arr[i-1]:
                arr[i], arr[i-1] = arr[i-1], arr[i]
                flg = True
                cnt = cnt + 1
                
    print(' '.join([str(x) for x in arr]))
    print(cnt)

bubbleSort(A)

Submission status: Accepted

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

    cnt = 0
    while True:
        flag = False
        for i in range(1, n)[::-1]:
            if array[i-1] > array[i]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
                cnt += 1
        if not flag:
            break
    print(" ".join(map(str, array)))
    print(cnt)


main()

Submission status: Accepted

Solution code:
def showList(A, N):
    for i in range(N-1):
        print(A[i],end=' ')
    print(A[N-1])

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]:
                v = A[i]
                A[i] = A[i-1]
                A[i-1] = v
                flag = 1
                count += 1
    showList(A, N)
    print(count)

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
nums = [n - i for i in range(1, n)]
flag = 1
exchange = 0

while flag == 1:
    flag = 0
    for j in nums:
        if A[j] < A[j - 1]:
            A[j], A[j - 1] = A[j - 1], A[j]
            exchange += 1
            flag = 1
        else:
            pass
        
print(*A)
print(exchange)

Submission status: Accepted

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

def bubbleSort(A):
    n = len(A)
    flag = True
    i = 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]
                i += 1
                flag = True
    print(*A)
    print(i)
bubbleSort(A)

Submission status: Accepted

Solution code:
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: Accepted

Solution code:
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: Accepted

Solution code:
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: 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):
    flag = 1
    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
    show(A)
    print(count)

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


bubbleSort(A,N)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int, input().strip().split(' ')))
e = int()
e = 0
flag = True
while flag:
        flag = False
        for i in range(n - 1, 0, -1):
            if s[i] < s[i - 1]:
                s[i - 1], s[i] = s[i], s[i - 1]
                e = e + 1
                flag = True

for i in range(0 , len(s)):
    if i != len(s) - 1:
        print(s[i], end=" ")
    else:
        print(s[i])

print(e)

Submission status: Accepted

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

cnt = 0
flag = 1
while flag > 0:
    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
print(' '.join(map(str,a)))
print(cnt)

Submission status: Accepted

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

flag = True
swap_count = 0
for i in range(0, n):
    if not flag:
        break
    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
            swap_count += 1

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

Submission status: Accepted

Solution code:
from typing import List


def bublle_sort(A: List[int]) -> List[int]:
    cnt = 0
    flg = True
    while flg:
        flg = False
        for i in reversed(range(1, N)):
            if A[i] < A[i - 1]:
                A[i], A[i - 1] = A[i - 1], A[i]
                cnt += 1
                flg = True
    return A, cnt


N = int(input())
A = [int(i) for i in input().split()]
A, cnt = bublle_sort(A)
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

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

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


A, count = bubble_sort(A, N)
string = list(map(str, A))
print(' '.join(string))
print(count)

Submission status: Accepted

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

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

print(" ".join([str(i) for i in a]))
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def bubblesort(n,l):
  flag=True
  m=0
  c=0
  for j in range(n):
    for i in range(n-1,m,-1):
      if int(l[i])<int(l[i-1]):
        l[i],l[i-1]=l[i-1],l[i]
        
        c+=1
    m+=1
  print(" ".join(l))
  print(c)
n=int(input())
l=list(input().split())
bubblesort(n,l)

Submission status: Accepted

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

print(*A[::-1])
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
flag = True
ans = 0
for i in range(N):
    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
            ans += 1
    if not flag:
        break
print(*A)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf 8 -*-
# Bubble Sort
# 2019. 4/22

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

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

    A_sorted, num_sw = bubbleSort(A, N)

    for i in range(N-1):
        print(A[i], end=" ")
    print(A[N-1])
    print(num_sw)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 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]
            count += 1

print(*a)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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


def bubble_sort(a, n):
    flag = True
    count = 0
    while flag:
        flag = False
        for i in range(n-2, -1, -1):
            if a[i] > a[i+1]:
                a[i], a[i+1] = a[i+1], a[i]
                flag = True
                count += 1
    print(" ".join(map(str, a)))
    print(count)


bubble_sort(ll, n)

Submission status: Accepted

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

Submission status: Accepted

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


while True:
    flag = 0

    for j in range(n - 1):
        if (nums[j + 1] < nums[j]):
            temp = nums[j]
            nums[j] = nums[j + 1]
            nums[j + 1] = temp
            count += 1
            flag = 1

    if flag != 1:
        break

print(' '.join([str(i) for i in nums]))
print(count)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0

    flag = True
    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
                ans += 1
    print(*a)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def bubble_sort(A):
    flag = True
    i = 0
    global count
    while flag:
        flag = False
        for j in range(i, len(A)-1):
           if A[j+1] < A[j]:
                A[j], A[j+1] = A[j+1], A[j]
                count += 1
                flag = True
        if not flag:
            i += 1
            flag = True
        if i >= len(A)-1:
            flag = False

n = int(input())
A = list(map(int, input().split()))
count = 0
bubble_sort(A)
print(*A)
print(count)

Submission status: Accepted

Solution code:
def bubble_sort(A):
    can_continue = True
    i = 0
    global count
    while can_continue:
        can_continue = False
        for j in range(i, len(A)-1):
           if A[j+1] < A[j]:
                A[j], A[j+1] = A[j+1], A[j]
                count += 1
                can_continue = True
        if not can_continue:
            i += 1
            can_continue = True
        if i >= len(A)-1:
            break

n = int(input())
A = list(map(int, input().split()))
count = 0
bubble_sort(A)
print(*A)
print(count)

Submission status: Accepted

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

n = int(input())
A = list(map(int, input().split()))
count = bubble_sort(A)
print(*A)
print(count)

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
A,count = bubbleSort(A,N)
print(" ".join(map(str,A)))
print("%d"%(count))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#bubble sort
N = int(input())
nums = list(map(int,input().split()))

count = 0 #交換回数
for i in range(N): #先頭が確定するまで調べる（確定したら先頭の次の値が先頭になる）
    j = N-1
    while j > i:
        if nums[j-1] > nums[j]:
            temp = nums[j-1]
            nums[j-1] = nums[j]
            nums[j] = temp
            count = count + 1 #交換したらカウントする
        j = j - 1 #交換したら次の値に移動する

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

Submission status: Accepted

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

Submission status: Accepted

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

def sort():
    flag = 1
    swap = 0
    while flag:
        flag = 0
        for j in range(N-1,0,-1):
            if A[j] < A [j-1]:
                temp = A[j]
                A[j] = A[j-1]
                A[j-1] = temp
                swap += 1
                flag = 1
    pr(A)
    print(swap)

def pr(list):
    n = len(list)
    for i in range(n):
        if i < n-1:
            print(list[i],end=" ")
        else:
            print(list[i],end="\n")

sort()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = input()
n = map(int,raw_input().split(' '))
c = 0
for i in range(0,N-1):
	for j in range(1,N-i):
		if n[j-1] > n[j]:
			temp = n[j-1]
			n[j-1] = n[j]
			n[j] = temp
			c += 1
print " ".join(str(i) for i in n)
print c

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

cnt = 0

for i in xrange(len(A)-1):
    for j in xrange(len(A)-1, i, -1):
        if A[j] < A[j-1]:
            A[j], A[j-1] = A[j-1], A[j]
            cnt += 1

for x in A:
	print x,
print 
print cnt

Submission status: Accepted

Solution code:

def bubble_sort(buff):
	count = 0
	k = len(buff) - 1
	for i in xrange(k):
		for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	print " ".join(map(str, buff))
	print count

N = int(raw_input())
buff = map(int, raw_input().split(' '))
bubble_sort(buff)

Submission status: Accepted

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

print(*A)
print(count)

Submission status: Accepted

Solution code:
def bubblesort(A):
    tot = 0
    flag = 1
    N = len(A)
    
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                tot += 1
    return A,tot

while True:
    try:
        N = int(raw_input())
    except EOFError:
        break
    A = map(int,raw_input().split())
    
    A,tot = bubblesort(A)
    print ' '.join(map(str,A))
    print tot

Submission status: Accepted

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

cnt = bubble_sort(nl)

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

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_A
# Result:
import sys

nums_len = int(sys.stdin.readline())
nums_str = sys.stdin.readline().split(' ')
nums = map(int, nums_str)

swap_count = 0
flag = True
while flag:
    flag = False
    for i in reversed(range(1, nums_len)):
        if nums[i] < nums[i - 1]:
            v = nums[i]
            nums[i] = nums[i - 1]
            nums[i - 1] = v
            swap_count += 1
            flag = True

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

Submission status: Accepted

Solution code:
def bubbleSort( nums ):
	cnt = 0
	for i in range( 0, len( nums ) ):
		for j in range( len( nums )-1 , i, -1 ):
			if nums[j] < nums[ j-1 ]:
				nums[j], nums[ j-1 ] = nums[ j-1 ], nums[j]
				cnt += 1

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


n = int( raw_input( ) )
nums = [ int( val ) for val in raw_input( ).split( " " ) ]
bubbleSort( nums )