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: Runtime Error

Solution code:
    N = int(input())
    A = [int(i) for i in input().split(" ")]
def bubble(A, N):
    count = 0
    flag = True
    while flag == True:
        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]
                count += 1
                flag = True
        print(" ".join(map(str, A)))
        print(count)
print(bubble(A,N))

Submission status: Runtime Error

Solution code:
    N = int(input())
    A = [int(i) for i in input().split(" ")]
def bubble(A, N):
    count = 0
    flag = True
    while flag == True:
        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]
                count += 1
                flag = True
        print(" ".join(map(str, A)))
        print(count)

print(bubble(A, N))

Submission status: Runtime Error

Solution code:
    N = int(input())
    A = [int(i) for i in input().split(" ")]
def bubble(A, N):
    count = 0
    flag = True
    while flag == True:
        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]
                count += 1
                flag = True
            print(" ".join(map(str, A)))
            print(count)

print(bubble(A, N))

Submission status: Runtime Error

Solution code:
    N = int(input())
    A = [int(i) for i in input().split(" ")]
def bubble(A, N):
    count = 0
    flag = True
    while flag == True:
        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]
                count += 1
                flag = True
        print(" ".join(map(str, A)))
        print(count)

bubble(A, N)

Submission status: Runtime Error

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

_flg = 1

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]
            _flg = 1

print(" ".join(_a))

Submission status: Runtime Error

Solution code:
_n = int(raw_input())
_a = [int(x) in x for 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(_a))
print(cnt)

Submission status: Runtime Error

Solution code:
_n = int(raw_input())
_a = [int(x) in x for 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(_a))
print(cnt)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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: Runtime Error

Solution code:
n = int(input())
a = [input() for i in range(n)]
a_swap = 0

flag = 1
while flag:
    flag = 0
    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 = 1
       a_swap += 1

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = [input() for i in range(n)]
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: Runtime Error

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

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

Submission status: Runtime Error

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: Runtime Error

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, test_arr)),"\n",num

Submission status: Runtime Error

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, test_arr))
print num

Submission status: Runtime Error

Solution code:
N = int(input())

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

trials = 0
for i in range(0, 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]
            trials += 1

print(trials)
for i in range(len(A)):
    print(A[i], end=" ")
print("")

Submission status: Runtime Error

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

c = 0

for i in range(n):
    for j in range(i + 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: Runtime Error

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 range(1, len(array)).reverse():
        if array[i] < array[i - 1]:
            swap(a, i, i-1)
            flag = True
            swap_num += 1
            
print list_to_string(array)
print str(swap_num)

Submission status: Runtime Error

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(a, i, i-1)
            flag = True
            swap_num += 1
            
print list_to_string(array)
print str(swap_num)

Submission status: Runtime Error

Solution code:
import output
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
output.print_1(A)
print(cnt)

Submission status: Runtime Error

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: Runtime Error

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

A, c = bubblesort(int(input()), list(map(int, input().split())))
print(A)
print(c)

Submission status: Runtime Error

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

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

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

bubbleSort(A, n)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0
Flag = True
while Flag:
    Fag = Flase
    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 = True
            count += 1
print(*A)
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0
Flag = True
while Flag:
    Flag = Flase
    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 = True
            count += 1
print(*A)
print(count)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

bubbleSort(a, n)

Submission status: Runtime Error

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


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

Submission status: Runtime Error

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


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

Submission status: Runtime Error

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

flag=1
m=0

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

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

Submission status: Runtime Error

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

flag=1
m=0

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

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

Submission status: Runtime Error

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

flag=1
m=0

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

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

Submission status: Runtime Error

Solution code:
n = int(input())
*array, = [int(input()) for i in range(n)]


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

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

        i += 1

    print(*a)
    print(c)


bubble_sort(array)

Submission status: Runtime Error

Solution code:
use std::io;
use std::str::FromStr;

fn read_line() -> String {
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s
}

macro_rules! from_line {
    ($($a:ident : $t:ty),+) => {
        $(let $a: $t;)+
        {
            let _line = read_line();
            let mut _it = _line.trim().split_whitespace();
            $($a = _it.next().unwrap().parse().unwrap();)+
                assert!(_it.next().is_none());
        }
    };
}

fn main() {

    from_line!(n: u64);

    let stdin = io::stdin();
    let mut buf = String::new();
    stdin.read_line(&mut buf).ok();
    let mut it = buf.split_whitespace().map(|n| usize::from_str(n).unwrap());
    let mut v: Vec<i64> = buf.split_whitespace()
            .map(|n| i64::from_str(n).unwrap())
            .collect();

    let mut flag = true;
    let mut count = 0;
    while flag {
        flag = false;
        for j in 1..n {
            if v[j as usize] < v[(j - 1) as usize] {
                let tmp = v[(j - 1) as usize];
                v[(j - 1) as usize] = v[j as usize];
                v[j as usize] = tmp;
                count += 1;
                flag = true;
            }
        }
    }

    while !v.is_empty() {
        if v.len() == 1 {
            print!("{}", v.pop().unwrap());
        }
        else {
            print!("{} ", v.pop().unwrap());
        }
    }
    println!("");

    println!("{:?}", count);
}

Submission status: Runtime Error

Solution code:
# リストの宣言
A = []

# 入力の最初の行に、数列の長さを表す整数 N が与えられます。
# 2 行目に、N 個の整数が空白区切りで与えられます。
LENGTH =int(input())
A = input().split()

# 変数
N = 0
M = LENGTH - 1
CHANGE = 0

# BubbleSort(A)
#  for i = 0 to A.length-1
while N <= LENGTH -1 :
    M = LENGTH - 1
    print ('N=',N)
#      for j = A.length-1 downto i+1
    while M >= N + 1:
        print  ('-M=',M)
#          if A[j] < A[j-1]
        if A[M] < A[M-1] :
            tmp = A[M-1]
            A[M-1] = A[M]
            A[M] = tmp
            CHANGE += 1
        M -= 1
    N += 1

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

Submission status: Runtime Error

Solution code:
N = int(input()) #要素数
A = list(map(int,input().split())) #ソート対象（スペース区切り→配列に展開）
 
flag = 1
i = 0
cnt = 0
 
#Sort処理（自分メモ：flagが0になったらループを抜ける）
while flag:
    flag = 0
    for j in reversed(range(i+1,N)):
        if A[j] < A[j-1]:
            tmp = A[j-1]
            A[j-1] = A[j]
            A[j] = tmp
            flag = 1
            cnt += 1
    i += 1
 
print(' '.join(map(str,A)))
print(cnt)

Submission status: Runtime Error

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: Runtime Error

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

if __name__ == '__main__':
    cnt = 0
    for i in range(N):
        for j in range(N-1, i, -1):
            if j > 0 and A[j-1] > A[j]:
                A[j-1], A[j] = A[j], A[j-1]
                cnt += 1
    print(" ".join(map(str, A)))
    pritn(cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
nlist = list(map(int, input().split()))
def BubbleSort(C, n):
    c = 0
    for i in range(n-1):
        for w in range(i+1, n):
            j = n-k+i
            if (C[j] < C[j-1]):
                a = C[j]
                C[j] = C[j-1]
                C[j-1] = a
                c += 1
BubbleSort(nlist, n)
for i in range(n):
    if (i == n-1):
        print('{}'.format(nlist[i]), end = '')
    else : 
        print('{}'.format(nlist[i]), end = ' ')
print()
print(c)

Submission status: Runtime Error

Solution code:
import sys
#A=[[5],[4,5,2,3,1]]
for line in sys.stdin:
    line=line.split()
    line=map(int,line)
    A.append(line)
N=A[0][0]
B=A[1]
n=0
for i in range (N):
    for k in range(N-i-1):
        print k
        j=(N-2)-k
        if B[j]>B[j+1]:
            C=B[j]
            B[j]=B[j+1]
            B[j+1]=C
            n=n+1
E=map(str,B)
D=" ".join(E)
print n
print D

Submission status: WA: Presentation Error

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
    B = []
    for i in range(N):
        B += str(A[i])
    print(" ".join(B))
    print(count)


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

Submission status: WA: Presentation Error

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)):
    print(s[i], end=" ")

print()
print(e)

Submission status: WA: Presentation Error

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 #交換したら次の値に移動する

for i in range(N):
    print(nums[i], end=" ")
print()
print(count)

Submission status: WA: Presentation Error

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 '\n',cnt

Submission status: WA: Presentation Error

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 '\n',cnt,'\n'

Submission status: WA: Presentation Error

Solution code:
def input_data():
    num = raw_input()
    array = []
    for line in num:
        if line != " ":
            array.append(line)

    return array


def main():
    n = raw_input()
    count = 0
    array = input_data()
    flag = True
    while flag:
        flag = False
        for j in reversed(xrange(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
    print " ".join(array)
    print count


if __name__ == '__main__':
    main()

Submission status: WA: Presentation Error

Solution code:
def input_data():
    num = raw_input()
    array = []
    for line in num:
        if line != " ":
            array.append(line)

    return array


def main():
    n = raw_input()
    count = 0
    array = input_data()
    flag = True
    while flag:
        flag = False
        for j in reversed(xrange(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
    print " ".join(array)
    print str(count)


if __name__ == '__main__':
    main()

Submission status: WA: Presentation Error

Solution code:
import sys
printf = sys.stdout.write

x = input()
num = map(int,raw_input().split())
count = 0

for i in xrange(x):
    for j in xrange(x - i - 1):
        if num[j] > num[j + 1]:
            inp = num[j]
            num[j] = num[j + 1]
            num[j + 1] = inp
            count += 1

for i in xrange(x - 1):
    print num[i],
printf(" " + str(num[x - 1]) + "\n")
print count

Submission status: WA: Presentation Error

Solution code:

import sys
printf = sys.stdout.write

x = input()
num = map(int,raw_input().split())
count = 0

for i in xrange(x):
    for j in xrange(x - i - 1):
        if num[j] > num[j + 1]:
            inp = num[j]
            num[j] = num[j + 1]
            num[j + 1] = inp
            count += 1

for i in xrange(x):
    print num[i],
printf("\n" + str(count))

Submission status: WA: Presentation Error

Solution code:

import sys
printf = sys.stdout.write

x = input()
num = map(int,raw_input().split())
count = 0

for i in xrange(x):
    for j in xrange(x - i - 1):
        if num[j] > num[j + 1]:
            inp = num[j]
            num[j] = num[j + 1]
            num[j + 1] = inp
            count += 1

for i in xrange(x - 1):
    print num[i],
printf(str(num[x - 1]))

printf("\n" + str(count))

Submission status: WA: Presentation Error

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

for i in range(len(list1)):
  print(list1[i], end = " ")
print("\n%d" % (count))

Submission status: WA: Presentation Error

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] = ''''''
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) + ' '
result.rstrip()
print(result)
print(swap_count)

Submission status: WA: Presentation Error

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) + ' '
result.rstrip()
print(result)
print(swap_count, end='')

Submission status: WA: Presentation Error

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)
for i in A:
    print(i, end=" ")
print("")
print(count)

Submission status: WA: Presentation Error

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):
    print(str(A[i]) + " ", end = "")
print()
print(cnt)

Submission status: WA: Presentation Error

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: WA: Presentation Error

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)),"\n",num

Submission status: WA: Presentation Error

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: WA: Presentation Error

Solution code:
N = int(input())

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


trials = 0
for i in range(0, 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]
            trials += 1

for i in range(len(A)):
    print(int(A[i]), end=" ")
print("")
print(trials)

Submission status: WA: Presentation Error

Solution code:
import sys

def bubble_sort(x):
    count = 0
    for i in range(0,len(l)-1):
        for j in range(i+1,len(l)):
            if(x[i] > x[j]):
                count += 1
                temp = x[i]
                x[i] = x[j]
                x[j] = temp
    for data in x:
        print data,
    print '\n',count


l = []
for input in sys.stdin:
    l.append(input)
l = l[1:]
l = l[0].split()
for i in range(0,len(l)):
    l[i] = int(l[i])
bubble_sort(l)

Submission status: WA: Presentation Error

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


num = int(input())
lst = [int(i) for i in input().split()]

lst = bubbleSort(lst,num)
for i in lst[0]:
    print(i,end=" ")
    if i == lst[0][len(lst[0])-1]:
        print("\n",end="")

print(lst[1])

Submission status: WA: Presentation Error

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

if __name__ == "__main__":
    ls=[]
    n=int(input())
    ls = list(map(int,input().split()))
    ch=bubble_sort(ls)
    for i in ls:
        print(i,end=" ")
    print()
    print(ch)

Submission status: WA: Presentation Error

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

if __name__ == "__main__":
    ls=[]
    n=int(input())
    ls = list(map(int,input().split()))
    ch=bubble_sort(ls)
    for i in ls:
        print(i,end=" ")
    print("\n",end="")
    print(ch)

Submission status: WA: Presentation Error

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