Problem:


Bubble Sort

Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

BubbleSort(A)
1 for i = 0 to A.length-1
2     for j = A.length-1 downto i+1
3         if A[j] N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by spaces characters.

Output

The output consists of 2 lines. 

In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

In the second line, please print the number of swap operations.

Constraints

1 ≤ N ≤ 100

Sample Input 1

5
5 3 2 4 1

Sample Output 1

1 2 3 4 5
8

Sample Input 2

6
5 2 4 6 1 3

Sample Output 2

1 2 3 4 5 6
9



Sample input:
5
5 3 2 4 1


Sample output:
1 2 3 4 5
8

Submission status: Accepted

Solution code:
def bubbleSort( 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( input( ) )
nums = [ int( val ) for val in input( ).split( " " ) ]
bubbleSort( nums )

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

c = 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]
            c += 1
            flag = True
print(' '.join((str(x) for x in A)))
print(c)

Submission status: Accepted

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

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

print(*numbers)
print(count)

Submission status: Accepted

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

    return list_, count

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

Submission status: Accepted

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

    return count

n = input()
nums = map(int, raw_input().split())
count = bubbleSort(nums, n)
print ' '.join(map(str, nums))
print count

Submission status: Accepted

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

if __name__ == '__main__':
    n = int(input())
    array, count = bubble_sort(list(map(int, input().split())))
    print(' '.join(map(str, array)))
    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], a[j - 1] = a[j - 1], a[j]
            count += 1
            flag = True

print(*a)
print(count)

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
count = 0
flag = True
while flag:
    flag = False
    for i in xrange(N-1, 0, -1):
        if A[i-1] > A[i]:
            A[i-1], A[i] = A[i], A[i-1]
            count+=1
            flag = True
print ' '.join(map(str, A))
print count

Submission status: Accepted

Solution code:
def bubble(A,N):
	flag=1
	cnt=0
	while flag==1:
		flag=0
		for j in range(1,N):
			if A[j]<A[j-1]:
				k=A[j-1]
				A[j-1]=A[j]
				A[j]=k
				flag=1
				cnt+=1
	return A,cnt
n=int(raw_input())			
list=map(int,raw_input().split())
list,cnt=bubble(list,n)
for x in list:
	print x,
print
print cnt,

Submission status: Accepted

Solution code:
#coding:utf-8
#1_2_A
def bubble_sort(ary, n):
    count = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in reversed(range(i+1, n)):
            if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
        i += 1

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

n = int(input())
numbers = list(map(int, input().split()))
bubble_sort(numbers, n)

Submission status: Accepted

Solution code:
N=int(input())

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

count=0
i=N-1

while i>0:
	j=0
	while j<i:
		if A[N-1-j]<A[N-2-j]:
			(A[N-1-j],A[N-2-j])=(A[N-2-j],A[N-1-j])
			count+=1
			#print(' '.join(map(str,A)))
		j+=1
	i-=1


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

Submission status: Accepted

Solution code:
def bubble_sort(a,n):
    m=0
    flag=True
    while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1] > a[i]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
    b=list(map(str,a))
    print(" ".join(b))
    print(m)

a=[]
n=int(input())
s=input()
a=list(map(int,s.split()))
bubble_sort(a,n)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_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]
            A[j] = A[j-1]
            A[j-1] = A_j
            flag = True
            count += 1
print " ".join(map(str, A))
print count

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
numbers = []
n = int(raw_input())
s = raw_input()
count = 0

numbers = s.split(" ")
for i in range(int(n)):
	numbers[i] = int(numbers[i])

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

Submission status: Accepted

Solution code:
num = raw_input()
num_list = raw_input().split()
num_list = map(int, num_list)
count = 0 

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

count = bubble_sort(num_list, count)

num_list =  map(str, num_list)
print " ".join(num_list)
print count

Submission status: Accepted

Solution code:
import sys

lines = sys.stdin.readlines()

N = lines[0]
A = lines[1].strip().split(" ")

flag = 1

c = 0
i = 0
while flag == 1:
    flag = 0
    for j in reversed(range(i+1,len(A))):
        if int(A[j]) < int(A[j-1]):
            a = A[j]
            b = A[j-1]
            A[j] = b
            A[j-1] = a
            flag = 1
            c += 1
    i += 1

print " ".join(A)
print c

Submission status: Accepted

Solution code:
N = int(raw_input())
A = raw_input().split(" ")
count = 0
flag = 1

i = 0
while flag == 1:
    flag = 0
    for j in reversed(range(i+1, N)):
        if int(A[j]) < int(A[j-1]):
            temp = A[j]
            A[j] = A[j-1]
            A[j-1] = temp
            count += 1
            flag = 1
    i += 1
    
print " ".join(A)
print count

Submission status: Accepted

Solution code:
def input_data():
    num = raw_input()
    array = []
    array = num.strip().split(" ")
    array = map(int, array)
    return array


def main():
    n = raw_input()
    count = 0
    array = input_data()
    flag = True
    i = 0
    while flag:
        flag = False
        for j in reversed(xrange(i + 1, int(n))):
            if array[j] < array[j - 1]:
                v = array[j]
                array[j] = array[j - 1]
                array[j - 1] = v
                flag = True
                count += 1
        i += 1
    array = map(str, array)
    print " ".join(array)
    print str(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

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

y = sys.stdin.readline()
y = int(y)

x = sys.stdin.readline()
x_list = x.split(" ")

for i in range(y):
  x_list[i] = int(x_list[i])

a = bubbleSort(x_list, y)

for k in range(0, y):
  print x_list[k],
print
print a

Submission status: Accepted

Solution code:
n = int(input())

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

flag = 1

cnt = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

numbers = list(map(int, input().split(" ")))

flag = 1

cnt = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

numbers = list(map(int, input().split(" ")))

flag = 1

cnt = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

numbers = list(map(int, input().split(" ")))

cnt = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

numbers = list(map(int, input().split(" ")))

cnt = 0

flag = 1

si = 0

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

numbers = map(str, numbers)
print(" ".join(numbers))

print(cnt)

Submission status: Accepted

Solution code:
def bubble_sort(List):
	cnt=0
	l=len(List)
	for i in range(l):
		for j in range(l-1,i,-1):
			if List[j]<List[j-1]:
				temp=List[j]
				List[j]=List[j-1]
				List[j-1]=temp
				cnt+=1
	return cnt
def main():
	N=input()
	N_List=map(int,raw_input().split())
	cnt=bubble_sort(N_List)
	for k in range(len(N_List)):
		N_List[k]=str(N_List[k])
	print(" ".join(N_List))
	print(cnt)

if __name__=='__main__':
	main()

Submission status: Accepted

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


def print_array(a):
    print(" ".join(map(str, a)))


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


def main():
    n = int(input())
    a = list(map(int, input().split()))
    (b, swap) = bubblesort(a, n)
    print_array(b)
    print(swap)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# encoding: utf-8

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

def main():
    N = input()
    A = map(int, raw_input().split())
    A, count = bubble_sort(A, N)
    print " ".join(map(str,A))
    print count

main()

Submission status: Accepted

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

length = int(raw_input())
nums = map(int, raw_input().split())
ans = bubbleSort(nums, length)
print " ".join(map(str, ans[0]))
print ans[1]

Submission status: Accepted

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

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

print ' '.join([str(v) for v in a])
print nswap

Submission status: Accepted

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

Submission status: Accepted

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


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

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

bubble_sort(A, N)

Submission status: Accepted

Solution code:
def bubbleSort(A):
    N = len(A)
    cnt = 0
    for i in range(N):
        for j in range(N - 1, i, -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)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    global count
    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

N = int(input())
A = [int(i) for i in input().split()]
count = 0
bubbleSort(A, N)
print(*A)
print(count)

Submission status: Accepted

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

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

print(*a)
print(c)

Submission status: Accepted

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

    return a, c

if __name__ == "__main__":
    n = int(input())
    a_l = [int(a) for a in input().split()]

    as_l, c  = bubbleSort(a_l, n)
    print(' '.join(map(str, as_l)))
    print(c)

Submission status: Accepted

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

def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = bubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def newbubbleSort(A, N):
    flag = 1
    count = 0
    i = 0
    while flag:
        flag = 0
        for j in xrange(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


def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = newbubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    N = input()
    A = map(int, raw_input().split())
    bubbleSort(A, N)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    count = 0
    for j in range(N - 1):
        for i in reversed(range(j + 1, N)):
            if A[i - 1] > A[i]:
                temp = A[i]
                A[i] = A[i - 1]
                A[i - 1] = temp
                count = count + 1
    for i in range(N):
        print A[i],
    print
    print count

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

bubbleSort(A, N)

Submission status: Accepted

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

def bubble_sort(raw_list):
    n_list = len(raw_list)
    n_swap = 0
    sorted_i = 1
    flag = True
    while flag:
        flag = False
        for j in reversed(range(sorted_i, n_list)):
            if raw_list[j] < raw_list[j-1]:
                raw_list[j-1], raw_list[j] = raw_list[j], raw_list[j-1]
                n_swap += 1
                flag = True
        sorted_i += 1
    print(' '.join([str(i) for i in raw_list]))
    print(n_swap)
        
bubble_sort(data)

Submission status: Accepted

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

def bubble_sort(raw_list):
    n_list = len(raw_list)
    n_swap = 0
    sorted_i = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(sorted_i + 1, n_list)):
            if raw_list[j] < raw_list[j-1]:
                raw_list[j-1], raw_list[j] = raw_list[j], raw_list[j-1]
                n_swap += 1
                flag = True
        sorted_i += 1
    print(' '.join([str(i) for i in raw_list]))
    print(n_swap)
        
bubble_sort(data)

Submission status: Accepted

Solution code:
def trace(a,n) :
    li=[]
    for i in xrange(n):
        li.append(str(a[i]))
    print(" ".join(li))
    
def bubblesort(a,n):
    flag=1
    ct=0
    while flag:
        flag=0
        if n==0:
            break
        else :
            for i in xrange(n-1,0,-1):
                if a[i]<a[i-1]:
                    keep=a[i-1]
                    a[i-1]=a[i]
                    a[i]=keep
                    ct+=1
                    flag=1
    return ct
n=input()
a=map(int,raw_input().split())
ct=bubblesort(a,n)
trace(a,n)
print(ct)

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
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]
            A[j] = A[j-1]
            A[j-1] = A_j
            flag = True
            count += 1
print " ".join(map(str, A))
print count

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
i = 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]
            A[j] = A[j-1]
            A[j-1] = A_j
            flag = 1
            i += 1
print " ".join(map(str, A))
print i

Submission status: Accepted

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

def show(a, n):
    for i in range(n):
        if i == n-1:
            print "%d" % a[i]
        else:
            print "%d" % a[i],

n = input()
a = map(int, raw_input().split())
c = bubbleSort(a, n)
show(a, n)
print "%d" % c

Submission status: Accepted

Solution code:
#coding: UTF-8

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

c = 0

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

for i in range(a - 1):
    print(N[i],end=' ')
print(N[a-1]);
print(c);

Submission status: Accepted

Solution code:
def bubbleSort(R, N):
	flag = True
	cnt = 0
	while flag == True:
		flag = False
		for j in range(N - 1, 0, -1):
			if R[j] < R[j - 1]:
				tmp = R[j]
				R[j] = R[j - 1]
				R[j - 1] = tmp
				cnt = cnt + 1
				flag = 1
	print(" ".join(map(str, R)))
	return cnt

if __name__ == '__main__':
	n = int(input())
	A = list(map(int, input().split()))
	ans = bubbleSort(A, n)
	print(ans)

Submission status: Accepted

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

N = int(raw_input())
A = map(int, raw_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-1], A[j] = A[j], A[j-1]
            count += 1
            flag = 1
print " ".join(map(str, A))
print count

Submission status: Accepted

Solution code:
import math

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

flag = 1
count = 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
            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()))

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

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

Submission status: Accepted

Solution code:
data_num = int(input())
data = [int(n) for n in input().split(' ')]
data_len = len(data)

flag = True
i = 0
changed = 0

while flag:
    flag = False
    for j in range(data_len-1, i, -1):
        if data[j] < data[j-1]:
            data[j], data[j-1] = data[j-1], data[j]
            changed += 1
            flag = True
    i += 1
print(' '.join(str(i) for i in data))

print(changed)

Submission status: Accepted

Solution code:
def show_list(a):
    for x in a:
        print x,
    print

def bubble_sort(a,n):
    flag = True
    count = 0
    while flag:
        flag = False
        for j in reversed(xrange(1,n)):
            if a[j] < a[j-1]:
                tmp = a[j]
                a[j] = a[j-1]
                a[j-1] = tmp
                count+=1
                flag = True
    show_list(a)
    print count
def main():
    n = input()
    a = map(int,raw_input().split())
    bubble_sort(a,n)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def merge_count(array):
    if len(array) <= 1:
        return array, 0
    mid = len(array) // 2
    left, cl = merge_count(array[:mid])
    right, cr = merge_count(array[mid:])
    count = cl + cr
    left = list(reversed(left))
    right = list(reversed(right))
    for i in range(len(array)):
        if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
    return array, count

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    a, b = merge_count(A)
    print(*a)
    print(b)

Submission status: Accepted

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

# MAIN
N = input()
A = map(int, raw_input().split())
c = bubbleSort(A, N)
for i in A: print i,
print 
print c

Submission status: Accepted

Solution code:
#coding: utf-8
#ALDS1_2A

def pnt(s):
 for i in xrange(len(s)):
  print s[i],
 print

n=int(raw_input())
a=map(int,raw_input().split())

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

pnt(a)
print sw

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
def bubbleSort(A, N):
	flag = 1
	count = 0
	while flag:
		flag = 0
		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 = 1
				count += 1
	return A,count
ans,c = bubbleSort(a,N)
print(*ans)
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

n = int(input())
num_list = input().split()

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

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

bubbleSort(num_list, n)

Submission status: Accepted

Solution code:
n = int(input())
num_list = input().split()

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

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

bubbleSort(num_list, n)

Submission status: Accepted

Solution code:
count = 0
list1 = []

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

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

print(" ".join(str(x) for x in list1))
print(count)

Submission status: Accepted

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

x = int(input())
y = list(map(int,input().split()))
z = 0

flag = 1
while flag:
	flag = 0
	i = x - 1
	while i > 0:
		if y[i] < y[i - 1]:
			y[i], y[i - 1] = y[i - 1], y[i]
			flag = 1
			z += 1
		i -= 1

print(" ".join(list(map(str,y))))
print(z)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_A&lang=jp
sample_input = list(range(3))
sample_input[0] = '''5
5 3 2 4 1'''
sample_input[1] = '''6
5 2 4 6 1 3'''
sample_input[2] = '''1
100'''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main part
def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp

num_of_data = int(input())
list_of_data = list(map(int, input().split()))

swap_count = 0
flag = True
while flag:
    flag = False
    n = num_of_data - 1
    while n >= 1:
        if list_of_data[n] < list_of_data[n-1]:
            swap_list_item(list_of_data, n, n-1)
            flag = True
            swap_count += 1
        n -= 1

result = ''
for number in list_of_data:
    result += str(number) + ' '
print(result.strip())
print(swap_count)

Submission status: Accepted

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


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


if __name__ == "__main__":
    N = input()
    a = map(int, raw_input().split())
    result, change = bubble_sort(a, N)
    print ' '.join(map(str, result))
    print change

Submission status: Accepted

Solution code:
n = int(input())
A = input().split(' ')
count = 0

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

print(" ".join(A))
print(count)

Submission status: Accepted

Solution code:
n = int(input())
A = input().split(' ')
count = 0

flag = True
while flag:
    flag = False
    i = 0

    for j in reversed(range(i + 1, n)):
        if int(A[j]) < int(A[j-1]):
            (A[j-1], A[j]) = (A[j], A[j-1])
            count += 1
            flag = True

    i += 1


print(" ".join(A))
print(count)