Problem:


Bubble Sort

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

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

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

Output

The output consists of 2 lines. 

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

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

Constraints

1 ≤ N ≤ 100

Sample Input 1

5
5 3 2 4 1

Sample Output 1

1 2 3 4 5
8

Sample Input 2

6
5 2 4 6 1 3

Sample Output 2

1 2 3 4 5 6
9



Sample input:
5
5 3 2 4 1


Sample output:
1 2 3 4 5
8

Submission status: Accepted

Solution code:
n = int(input())
A = [int(i) for i in input().split(' ')]
# A = '0 33 43 62 29 0 8 52 56 56 19 11 51 43 5 8 93 30 66 69'.split(' ')
# A = [int(i) for i in A]

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

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

bubble_sort(A, n)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(x) for x in input().split()]
time = 0
flag = True
while flag:
     flag = False
     for i in reversed(range(n-1)):
        if R[i+1] < R[i]:
            R[i], R[i+1] = R[i+1], R[i]
            flag = True
            time += 1 
print(*R)
print(time)

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
S = int(input())
N = list(map(int,input().split()))
count=0
def bubblesort(n,s):
    global count
    flag = 1
    while flag:
        flag = 0
        for i in range(s-1,0,-1):
            key = n[i]
            if n[i]<n[i-1]:
                n[i]=n[i-1]
                n[i-1]=key
                count+=1
                flag=1
    print(" ".join(list(map(str,n))))
    print(count)
        
bubblesort(N,S)

Submission status: Accepted

Solution code:
def bubbleSort():
    l = int(input())
    A = list(map(int, input().split(" ")))
    count = 0
    for i in range(l-1):
        for j in range(l-1-i):
            if (A[j]>A[j+1]):
                temp = A[j]
                A[j] = A[j+1]
                A[j+1] = temp
                count += 1
    print(" ".join(map(str, A)))
    print(count)

bubbleSort()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split(" ")]
flag = True
swap_count = 0
i = 0
while flag:
    flag = False
    for j in range(N-1, i, -1):
        if A[j] < A[j-1]:
            A[j], A[j-1] = A[j-1], A[j]
            flag = True
            swap_count += 1
    i += 1

print(*A)
print(swap_count)

Submission status: Accepted

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

Submission status: Accepted

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

for mak in range(1,N) :
    for j in range(mak,N)[ : : -1] :
        if A[j] < A[j - 1] :
            tem      = int(A[j])
            A[j]     = int(A[j - 1])
            A[j - 1] = int(tem)
            cou = cou + 1
A   = [str(temp) for temp in A]
print(' '.join(A))
print(cou)

Submission status: Accepted

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

import sys
import os
import math


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


def bubble_sort(A):
    swap_num = 0
    while True:
        swapped = False
        for i in range(N - 1):
            if A[i] > A[i + 1]:
                A[i], A[i + 1] = A[i + 1], A[i]
                swap_num += 1
                swapped = True
        if not swapped:
            return A, swap_num

A, num = bubble_sort(A)
print(*A)
print(num)

Submission status: Accepted

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

def bubbleSort(A, N):
    swap_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]
                swap_count += 1
                flag = True
    ans = " ".join(map(str, A))
    print(ans)
    print(swap_count)

def main():
    size = int(input())
    line = list(map(int, input().split()))
    bubbleSort(line, size)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for i in range(N-1):
    print A[i],
print A[-1]

print x

Submission status: Accepted

Solution code:
# completed
def print_list(ele_list):
    print(" ".join(map(str, ele_list)))

def bubble_sort(ele_list):
    len_ele_list = len(ele_list)
    num_swap = 0
    for i in range(0, len_ele_list):
       for j in range(len_ele_list-1, i, -1):
           if ele_list[j] < ele_list[j-1]:
               key = ele_list[j]
               ele_list[j] = ele_list[j-1]
               ele_list[j-1] = key
               num_swap += 1
    print_list(ele_list)
    print(num_swap)

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

bubble_sort(ele_list)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#3.3

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

flag = 1
swapcount = 0
while flag == 1:
    flag = 0
    for i in range(0, N-1):
        if a[i] > a[i + 1]:
            tmp = a[i];
            a[i] = a[i + 1]
            a[i + 1] = tmp
            flag = 1
            swapcount += 1

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

Submission status: Accepted

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

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

times = int(input())
array = list(map(int,input().split()))

bubble_sort(array)

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

    i = bubblesort(ls)

    print(' '.join((map(str,ls))))
    print(i)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def buble_sort(a,n):
    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
    return (a,count)

n = int(input())
a = list(map(int, input().split()))
a,c = buble_sort(a,n)
print(' '.join(map(str, a)))
print(c)

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]):
                A[i-1], A[i] = A[i], A[i-1]
                cnt += 1
                flag = True

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

bubble_sort(A, N)

Submission status: Accepted

Solution code:
_n = int(raw_input())
_a = [int(x) for x in raw_input().split()]

_flg = 1
cnt = 0

while _flg:
    _flg = 0
    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 = 1

print(" ".join([str(x) for x in _a]))
print(cnt)

Submission status: Accepted

Solution code:
def bubble_sort(A, n):
	flag = 1
	i = 0
	count = 0

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

		i += 1
	
	print(' '.join(map(str, A)))
	print(count)


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

	bubble_sort(A, n)

Submission status: Accepted

Solution code:
#ALDS1_2-A Bubble Sort
def bubbleSort(A,N):
    c=0
    flag=1
    while flag:
        flag=0
        j=N-1
        while j>0:
            if(int(A[j])<int(A[j-1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
    return c

N=int(input())
A=input().split()
c=bubbleSort(A,N)
S=""
for i in range(N-1):
    S+=A[i]+" "
S+=str(A[-1])
print(S)
print(c)

Submission status: Accepted

Solution code:
itemCount = int(input())
items = list(map(int, input().split(" ")))

def bubbleSort(items, itemCount):
    hasPrevious = True
    swapCount = 0
    while hasPrevious:
        hasPrevious = False
        for index in reversed(range(1, itemCount )):
            if items[index] < items[index - 1]:
                items[index], items[index - 1] = [items[index - 1], items[index]]
                hasPrevious = True
                swapCount += 1
    print(" ".join(map(str, items)))
    print(swapCount)

bubbleSort(items, itemCount)

Submission status: Accepted

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

def BubbleSort(A, N):
 flag = 0
 sw = 0
 i = 0
 while flag == 0:
  flag = 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]
    flag = 0
    sw += 1
  i += 1
 return str(sw)

sw = BubbleSort(A, N)
print(" ".join(map(str, A)))
print(sw)

Submission status: Accepted

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

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

print(" ".join(a))
print(a_swap)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().strip().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:
x = int(input())
y = input()
y = list(map(int, y.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
print(*y)
print(c)

Submission status: Accepted

Solution code:
#coding: utf-8

def print_list(X):
    first_flag = True
    for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
    print(print_line)

def bubble_sort(A, N):
    #print_list(A)
    #flag: ???????????????????????????True???????´?
    flag = True
    counter = 0

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

                flag = True
                counter += 1
        #print_list(A)
    print_list(A)
    print(counter)

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

bubble_sort(A, N)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
n = N
cnt = 0
while n > 0:
    for i in range(N-1, 0, -1):
        if A[i] < A[i-1]:
            t = A[i]
            A[i] = A[i-1]
            A[i-1] = t
            cnt += 1
    n -= 1
for i in range(N):
    if i == N - 1:
        print(str(A[i]))
    else:
        print(str(A[i]) + " ", end = "")
print(cnt)

Submission status: Accepted

Solution code:
#
# coding: utf-8
 
def bubbleSort(arr, n):
 
    flag = True
    swap_num = 0
 
    while flag:
        flag = False
        for j in xrange(n-1, 0, -1):
            if arr[j] < arr[j-1]:
                arr[j], arr[j-1] = arr[j-1], arr[j]
                swap_num += 1
                flag = True
        pass
    return swap_num
 
 
n = int(raw_input())
arr = map(int, raw_input().split())
 
num = bubbleSort(arr, n)
print " ".join(map(str, arr))
print num

Submission status: Accepted

Solution code:
n = int(input())
s = input().split(" ")
nums = list(map(int,s))
flag = 1
koukan = 0
while flag:
    flag = 0
    for i in range(n-1, 0, -1):
        if nums[i] < nums[i-1]:
            w = nums[i]
            nums[i] = nums[i-1]
            nums[i-1] = w
            koukan += 1
            flag = 1
for i in range(n):
    if i == n-1:
        print(nums[i])
    else:
        print(nums[i],end=" ")
print(koukan)

Submission status: Accepted

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

    A, count = bubbleSort(A, N)

    print(' '.join([str(a) for a in A]))
    print(count)

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

    return A, count

if __name__ == '__main__':
    main()

Submission status: Accepted

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

flag = True
count = 0
while(flag):
    flag = False
    for i in range(1, N):
        if(a[-i] < a[-i-1]):
            hold = a[-i]
            a[-i] = a[-i-1]
            a[-i-1] = hold
            flag = True
            count += 1
print(" ".join(list(map(str, a))))
print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 17 09:42:24 2017

@author: syaga
"""

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    flag = 1
    num = 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
                flag = 1
                num += 1
    print(" ".join(map(str, A)))
    print(num)

Submission status: Accepted

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

c = 0

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

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

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
      ctn = 0
      for i in range(0,N):
            for j in range(0,N-i-1):
                  if int(A[j]) > int(A[j+1]):
                        t = A[j]
                        A[j] = A[j+1]
                        A[j+1] = t
                        ctn = ctn + 1
      for i in range(0,N):
            if i < N-1:
                  print(A[i],end=' ')
            else:
                  print(A[i])
      print(ctn)

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

Submission status: Accepted

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

flag = True
c = 0
while flag:
    flag = False
    for j in reversed(range(1, N)):
        if  A[j - 1] > A[j]:
            A[j - 1],    A[j] = A[j], A[j - 1]
            c += 1
            flag = True
    
print(' '.join(map(str, A)))
print(c)

Submission status: Accepted

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

def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
     
    sw = bubble_sort(A, N)                            

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def bubble(A, N):
    c = 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]
                c += 1
                flag = True
    return A, c

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

    A, c = bubble(A, N)
    print (*A)
    print (c)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(A) for A in input().split()]
cnt = 0
for i in range(1, N):
    for j in range(N-1, i-1, -1):
        if A[j] < A[j-1]:
            t = A[j]
            A[j] = A[j-1]
            A[j-1] = t
            cnt += 1
for i in range(N):
    if i == N - 1:
        print(A[i])
    else:
        print("{}".format(A[i]), end=' ')

print(cnt)

Submission status: Accepted

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


def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
    
    sw = bubble_sort(A, N)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

res = BubbleSort(N, A)
print(" ".join(map(str, res[0])))
print(res[1])

Submission status: Accepted

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

def print_list(A):
    for a in A[:-1]:
        print ("%d "%a, end="")
    print(A[-1])

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

A_sorted, c = BubbleSort(A, N)

print_list(A_sorted)
print(c)

Submission status: Accepted

Solution code:
import sys

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

def list_to_string(a):
    s = ""
    for n in a:
        s += str(n) + " "
    return s.strip()

num = int(sys.stdin.readline().strip())

array = map(lambda x: int(x), sys.stdin.readline().strip().split(" "))

flag = True
swap_num = 0

while flag:
    flag = False
    for i in reversed(range(1, len(array))):
        if array[i] < array[i - 1]:
            swap(array, i, i-1)
            flag = True
            swap_num += 1
            
print list_to_string(array)
print str(swap_num)

Submission status: Accepted

Solution code:
c = int(input())
s = input().split()
count = 0
for j in range(c):
    for i in range(c-1,j,-1):
        if int(s[i]) < int(s[i-1]):
            s[i],s[i-1] = s[i-1],s[i]
            count += 1
print(" ".join(s))
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
??????????????????
??????????????????????????????????????¨?????????????????????Bubble??????
?°´??¢???????????£???????????????????????????????´????????????????????????????
??????????????????????¬???????????????¢?????´???????????§??°????????????????????????????????????

1 bubbleSort(A, N) // N ??????????´?????????? 0-????????????????????? A
2   flag = 1 // ????????£??\????´?????????¨??????
3   while flag
4     flag = 0
5     for j ??? N-1 ?????? 1 ?????§
6       if A[j] < A[j-1]
7         A[j] ??¨ A[j-1] ?????????
8         flag = 1

"""
import math

def bSort(A, N):
    cnt = 0
    f = True
    s = N-1
    e = 0
    while f == True:
        f = False
#        print("start={0} end={1}\n".format(s, e))
        for j in range(s, e, -1):
            x = A[j]
            y = A[j-1]
#            print("A[{0}]={1} A[{2}]={3}".format(j, x, j-1, y))
#            print(A)
            if x < y:
#                print("swap {0} {1}".format(x, y))
                A[j] = y
                A[j-1] = x
                f = True
                cnt += 1
        e += 1
        if e > s:
            break
    return cnt

# ?????????
N = int(input().strip())
A = list(map(int,input().strip().split()))
cnt = bSort(A,N)
print(" ".join(map(str,A)))
print(cnt)

Submission status: Accepted

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

flag = 1
cnt = 0
while flag:
    flag = 0
    # ????????????????????????
    for j in reversed(range(1, N)):
        # 1?????????????´???¨??????
        if A[j] < A[j-1]:
            # 1???????????????????°????????????°??????
            A[j], A[j-1] = A[j-1], A[j]
            flag = 1
            cnt += 1

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

Submission status: Accepted

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

    a = list(A)
    ans, n = bubbleSort(a, N)
    print(*ans, sep=' ')
    print(n)

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

main()

Submission status: Accepted

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

change = 0
flag = True
while flag:
	flag = False
	for idx in range(1,n):
		if data[idx-1] > data[idx]:
			data[idx-1],data[idx] = data[idx],data[idx-1]
			flag = True
			change += 1

print(' '.join([str(d) for d in data]))
print(change)

Submission status: Accepted

Solution code:
n = int(input())
k = [int(i) for i in input().split()]
M = 0
for i in range(n-1):
    for l in range(n-i-1):

        if k[n-2-l] > k[n-1-l]:
            v = k[n-1-l]
            k[n - 1 - l]=k[n-2-l]
            k[n - 2 - l]=v
            M+=1
        else:
            pass

print(' '.join(map(str, k)))
print(M)

Submission status: Accepted

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

k=0
for i in range(len(nums)-1):
    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]
            k+=1

print(*nums)
print(k)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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]:
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp
            count += 1
            flag = 1
            #print a

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

print count

Submission status: Accepted

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

def bubblesort(A, N):
    flag = True
    i = 0
    cnt = 0
    while flag:
        flag = False
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                flag = True
                cnt += 1
        i += 1     
    print(*A)
    print(cnt)

bubblesort(A,N)

Submission status: Accepted

Solution code:
#bubble
N=int(input())
A=[int(i) for i in input().split()]
fl=1
C=0
while fl==1:
    fl=0
    for j in range(N-1):
        if A[N-1-j]<A[N-2-j]:
            t=A[N-1-j]
            A[N-1-j]=A[N-2-j]
            A[N-2-j]=t
            C+=1
            fl=1
for j in range(N):
    A[j]=str(A[j])
print(" ".join(A))
print(C)

Submission status: Accepted

Solution code:
# Aizu Problem ALDS_1_2_A: Bubble Sort
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


def printA(A):
    print(' '.join([str(a) for a in A]))

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

        
N = int(input())
A = [int(_) for _ in input().split()]
A, swaps = bubble_sort(A)
printA(A)
print(swaps)

Submission status: Accepted

Solution code:
q = int(input())

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

def bubblesort(ary):
    cnt = 0
    for i in range(q):
        for j in range(q-1, i, -1):
            if ary[j] < ary[j-1]:
                ary[j-1], ary[j] = ary[j], ary[j-1]
                cnt += 1
    return (ary, cnt)

ary, cnt = bubblesort(sort1)
print(" ".join(map(str, ary)))
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(n) for n in input().split()]
flg = 1
cnt = 0
while flg:
    flg = 0
    for i in range(N-1, 0, -1):
        if A[i] < A[i-1]:
            t = A[i]
            A[i] = A[i-1]
            A[i-1] = t
            flg = 1
            cnt += 1
for i in range(N):
    if i == N - 1:
        print(A[i])
    else:
        print(A[i], end=" ")
print(cnt)

Submission status: Accepted

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


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

bubbleSort(L, N)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    c = 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]
                c += 1
                flag = 1
    print " ".join(map(str, A))
    print c


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

bubbleSort(L, N)

Submission status: Accepted

Solution code:
n=int(input())
z=list(map(int,input().split()))
flag=1
a=0
cnt=0
while flag:
    flag=0

    for i in reversed(range(a,n)):
        if i ==0:
            break
        if z[i]<z[i-1]:
            tmp=z[i]
            z[i]=z[i-1]
            z[i-1]=tmp
            flag=1
            cnt+=1
        
    a+=1
print(' '.join(list(map(str,z))))
print(cnt)

Submission status: Accepted

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

count = 0
flag  = 1

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

print(*a)
print(count)

Submission status: Accepted

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

count = 0
flag  = 1

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

print(*a)
print(count)

Submission status: Accepted

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

count = 0
first = 0

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

print(*a)
print(count)

Submission status: Accepted

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

last  = 0
count = 0
flag  = True

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

print(*a)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

    swapOperation = 0

    flag = 1
    while flag == 1:
        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
                flag = 1
                swapOperation += 1
    print(" ".join(map(str,A)))
    print(swapOperation)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def bubble_sort(A, N):
    flag = True
    c = 0
    i = 0
    while flag:
        flag = False
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j],  A[j-1] = A[j-1], A[j]
                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: Accepted

Solution code:


def bsort(array):
    i = 0
    counter = 0
    while i < len(array)-1:
        for c in range(len(array)-1,i,-1):
            if array[c]<array[c-1]:
                a = array[c-1]
                array[c-1] = array[c]
                array[c] = a
                counter += 1

        i = i+1
    resul = ' '.join([str(i) for i in array])
    print(resul)
    print(counter)

n = int(input())

inputarr = input().split()
intarr = [int(i) for i in inputarr]

bsort(intarr)

Submission status: Accepted

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


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