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: Wrong Answer

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()))
    print(merge_count(A)[1])

Submission status: Wrong Answer

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(z)
print(" ".join(list(map(str,y))))

Submission status: Wrong Answer

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


def bubble_sort(a):
    change = 0
    for i in xrange(len(a) - 1):
        for j in xrange(len(a) - 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__":
    a = map(int, raw_input().split())
    # a = [5, 2, 4, 6, 1, 3]
    result, change = bubble_sort(a)
    print result
    print change

Submission status: Wrong Answer

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())
    # a = [5, 2, 4, 6, 1, 3]
    result, change = bubble_sort(a, N)
    print ','.join(map(str, result))
    print change

Submission status: Wrong Answer

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())
    # a = [5, 2, 4, 6, 1, 3]
    result, change = bubble_sort(a, N)
    print ','.join(map(str, result))
    print change

Submission status: Wrong Answer

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())
    # a = [1, 2, 3, 4, 5]
    result, change = bubble_sort(a, len(a))
    print ','.join(map(str, result))
    print str(change)

Submission status: Wrong Answer

Solution code:
def main():
  element_num = int(input())
  elements = [int(s) for s in input().split()]
  swap_count = 0

  for i in range(1, element_num):
    for j in reversed(range(i, element_num)):
      if elements[j-1] > elements[j]:
        elements[j-1], elements[j] = elements[j], elements[j-1]
        swap_count += 1

  print(elements)
  print(swap_count)
  return 0

Submission status: Wrong Answer

Solution code:
def main():
  element_num = int(input())
  elements = [int(s) for s in input().split()]
  swap_count = 0

  for i in range(1, element_num):
    for j in reversed(range(i, element_num)):
      if elements[j-1] > elements[j]:
        elements[j-1], elements[j] = elements[j], elements[j-1]
        swap_count += 1

  print(elements)
  print(swap_count)
  return 0

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

cnt = 0

for i in range(0, N):
    min_i = i
    for j in range(i+1, N)[::-1]:
        if A[j] < A[min_i]:
            min_i = j
    if i != min_i:
        A[i], A[min_i] = A[min_i], A[i]
        cnt = cnt + 1

print(' '.join([str(i) for i in A]))
print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:UTF-8
def BS(N,A):
    count=0
    flag=True
    while flag==True:
        flag=False
        for i in range(1,N):
            if A[N-i]<A[N-i-1]:
                swap=A[N-i]
                A[N-i]=A[N-i-1]
                A[N-i-1]=swap
                flag=True
                count+=1
    print(" ".join(A))
    print(count)

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

Submission status: Wrong Answer

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


class Solution:
    """
    @param prices: Given an integer array
    @return: Maximum profit
    """

    @staticmethod
    def bubble_sort():
        # write your code here
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

        flag = 1
        count = 0
        while flag:
            flag = 0
            for j in range(array_length - 1, 0, -1):
                if unsorted_array[j] < unsorted_array[j - 1]:
                    unsorted_array[j], unsorted_array[j - 1] = unsorted_array[j - 1], unsorted_array[j]
                    flag = 1
                    count += 1
        print(unsorted_array)
        print(count)


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Submission status: Wrong Answer

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


class Solution:

    @staticmethod
    def bubble_sort():
        # write your code here
        array_length = int(input())
        array = [int(x) for x in input().split()]

        flag, count, cursor = 1, 0, 0
        while flag:
            flag = 0
            for j in range(array_length - 1, cursor, -1):
                if array[j] < array[j - 1]:
                    array[j], array[j - 1] = array[j - 1], array[j]
                    flag = 1
                    count += 1
                    cursor += 1
        print(" ".join(map(str, array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Submission status: Wrong Answer

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

N = int(raw_input())

A = raw_input().split() 
counter = 0 
for i in range(len(A)):
    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]
            counter += 1
        print A

print counter

Submission status: Wrong Answer

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

N = int(raw_input())

A = raw_input().split() 
counter = 0 
for i in range(len(A)):
    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]
            counter += 1
    

print counter

Submission status: Wrong Answer

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

N = int(raw_input())

A = map(int,raw_input().split())
print ' '.join(map(str,A))
counter = 0 
for i in range(len(A)):
    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]
            counter += 1

print counter

Submission status: Wrong Answer

Solution code:
N = input()
A = map(int, raw_input().split())
ans = 0
for i in xrange(N):
    for j in xrange(i+1, N):
        if A[i] > A[j]:
            ans += 1
            A[i], A[j] = A[j], A[i]
print ' '.join(map(str, A))
print ans

Submission status: Wrong Answer

Solution code:
def bubble_sort(numbers, n):
    """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)

    Returns:
        sorted list
    """
    flag = True
    counter = 0
    while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if numbers[index] < numbers[index - 1]:
                numbers[index], numbers[index - 1] = numbers[index - 1], numbers[index]
                flag = True
                counter += 1
    return numbers, counter


n = int(raw_input())
numbers = [int(x) for x in raw_input().split()]
numbers, swapped_numbers = bubble_sort(numbers, n)

print(numbers)
print(swapped_numbers)

Submission status: Wrong Answer

Solution code:
def bubble_sort(list):
    for i in range(0,n-1):
        for j in range(n-1, i, -1):
            if list[j-1] > lsit[j]:
                temp = list[j]
                list[j] = list[j-1]
                list[j-1] = temp

Submission status: Wrong Answer

Solution code:

# coding:utf-8

def bubble_sort(list):
    n = len(list)-1 ; count=0
    for i in range(0,n):
        for j in range(n, i, -1):
            if list[j-1]>list[j]: temp=list[j] ; list[j]=list[j-1] ; list[j-1]=temp ; count+=1
    return list, count


num = int(input())
list = list(map(int,input().split()))
print(bubble_sort(list))

Submission status: Wrong Answer

Solution code:

# coding:utf-8

def bubble_sort(list):
    n = len(list)-1 ; count=0
    for i in range(0,n):
        for j in range(n, i, -1):
            if list[j-1]>list[j]: temp=list[j] ; list[j]=list[j-1] ; list[j-1]=temp ; count+=1
    print(list) ; print(count)


num = int(input())
list = list(map(int,input().split()))
bubble_sort(list)

Submission status: Wrong Answer

Solution code:

# coding:utf-8

def bubble_sort(list):
    n = len(list)-1 ; count=0
    for i in range(0,n):
        for j in range(n, i, -1):
            if list[j-1]>list[j]: temp=list[j] ; list[j]=list[j-1] ; list[j-1]=temp ; count+=1
    print(set(list)) ; print(count)


num = int(input())
list = list(map(int,input().split()))
bubble_sort(list)

Submission status: Wrong Answer

Solution code:
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]
                flag = True
                swap_count += 1
                print(A)
    return swap_count

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

Submission status: Wrong Answer

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

N = int(input())
A = [int(s) for s in input().split()]
print(' '.join(str(s) for s in bubble_sort(A)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

def bubble(list):
    flag = 1
    count = 0
    while flag == 1:
        flag = 0
        for i in range(len(list)-1):
            if list[len(list)-1-i] < list[len(list)-1-i-1]:
                x = list[len(list)-i-1]
                list[len(list)-1-i] = list[len(list)-1-i-1]
                list[len(list)-1-i-1] = x
                flag = 1
                count += 1
                print(count)
    return [list,count]

answer = bubble(list)
for i in range(len(answer[0])):
    print(str(answer[0][i]) + ' ',end='') if i != len(answer[0])-1 else print(answer[0][i])
print(answer[1])

Submission status: Wrong Answer

Solution code:
n = int(input())

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

def bubble(list):
    flag = 1
    count = 0
    while flag == 1:
        flag = 0
        for i in range(len(list)-1):
            if list[len(list)-1-i] < list[len(list)-1-i-1]:
                x = list[len(list)-i-1]
                list[len(list)-1-i] = list[len(list)-1-i-1]
                list[len(list)-1-i-1] = x
                flag = 1
                count += 1
                print(count)
    return [list,count]

answer = bubble(list)
for i in range(len(answer[0])):
    print(str(answer[0][i]) + ' ',end='') #if i != len(answer[0])-1 else print(answer[0][i])
print(answer[1])

Submission status: Wrong Answer

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

n = int(input())
data = [int(i) for i in input().split()]
print(bubbleSort(data,n))

Submission status: Wrong Answer

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

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

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

A, count = bubble_sort(A)
trace(A)
print(count)

Submission status: Wrong Answer

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

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 = n - 1
    counter = 0
    while i >= 1:
        j = i - 1
        while j >= 0:
            if A[i] < A[j]:
                A[i], A[j] = A[j], A[i]
                counter += 1
            j -= 1
        i -= 1
    trace(A)
    print(counter)

bubble_sort(A, n)

Submission status: Wrong Answer

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
        
bubblesort(N,S)
print(count)

Submission status: Wrong Answer

Solution code:
N   = int(input())
A   = input().split()
cou = 0

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

Submission status: Wrong Answer

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(num)
print(*A)

Submission status: Wrong Answer

Solution code:
N = input()
NArray = []
for x in raw_input().split():
  NArray.append(x)

i = 0
while i < len(NArray):
  j = len(NArray)-1
  while j > i:
    if  NArray[j] < NArray[j-1]:
      NArray[j], NArray[j-1] = NArray[j-1], NArray[j]
    j = j-1
  i=i+1

print N
for x in NArray:
  print x,
print

Submission status: Wrong Answer

Solution code:
N = input()
NArray = []
for x in raw_input().split():
  NArray.append(x)

i = 0
while i < len(NArray):
  j = len(NArray)-1
  while j > i:
    if  NArray[j] < NArray[j-1]:
      NArray[j], NArray[j-1] = NArray[j-1], NArray[j]
    j = j-1
  i=i+1


for x in NArray:
  print x,
print
print N

Submission status: Wrong Answer

Solution code:
N = input()
NArray = []
for x in raw_input().split():
  NArray.append(x)

i = 0
s = 0
while i < len(NArray):
  j = len(NArray)-1
  while j > i:
    if  NArray[j] < NArray[j-1]:
      NArray[j], NArray[j-1] = NArray[j-1], NArray[j]
      s = s + 1
    j = j-1
  i=i+1


for x in NArray:
  print x,
print
print s

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(input().split())
count = 0
for i in range(n):
    for j in range(n):
        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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input("Enter the length of the array: "))

text = input("Enter the array: ")
A = text.split()
A = list(map(int, A))


#if N != len(A):
#    print("The length of the array is incorrect!! ")
#    print("N=" + str(N) + ", the actual length of A = " + str(len(A)))
#else:
#    print("The length of the array is: " + str(len(A)))

# Bubble Sort
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 the number of trials
print(trials)
# print bubble-sorted array
for i in range(len(A)):
    print(A[i], end=" ")
print("")

Submission status: Wrong Answer

Solution code:
# A = []
N = int(input("Enter the length of the array: "))

text = input("Enter the array: ")
A = text.split()
A = list(map(int, A))


#if N != len(A):
#    print("The length of the array is incorrect!! ")
#    print("N=" + str(N) + ", the actual length of A = " + str(len(A)))
#else:
#    print("The length of the array is: " + str(len(A)))

# Bubble Sort
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 the number of trials
# print bubble-sorted array
for i in range(len(A)):
    print(A[i], end=" ")
print("")
print(trials)

Submission status: Wrong Answer

Solution code:
N = int(input("Enter the length of the array: "))

text = input("Enter the array: ")
A = text.split()
A = list(map(int, A))


#if N != len(A):
#    print("The length of the array is incorrect!! ")
#    print("N=" + str(N) + ", the actual length of A = " + str(len(A)))
#else:
#    print("The length of the array is: " + str(len(A)))

# Bubble Sort
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 the number of trials
# print bubble-sorted array
for i in range(len(A)):
    print(int(A[i]), end=" ")
print("")
print(trials)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
def bubbleSort(A, N):
      ctn = 0
      for i in range(0,N):
            for j in range(0,N-i-1):
                  if A[j] > 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()).split()
      bubbleSort(A, N)

Submission status: Wrong Answer

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]):
                        print(i)
                        print(A[j],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: Wrong Answer

Solution code:
N = int(input())
A = [int(A) for A in input().split()]
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

for i in range(N):
    if i == N - 1:
        print(A[i])
    else:
        print("{}".format(A[i]), end=' ')

Submission status: Wrong Answer

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):
    print_list(A)
    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: Wrong Answer

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:
#        print("start={0} end={1}\n".format(s, e))
        for j in range(s, e, -1):
            f = False
            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}\n".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: Wrong Answer

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

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

        else:
            pass


print(k)

Submission status: Wrong Answer

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

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

        else:
            pass

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

Submission status: Wrong Answer

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):
        print(i,j)
        if nums[j]<nums[j-1]:
            nums[j],nums[j-1]=nums[j-1],nums[j]
            print(nums)
            k+=1

print(*nums)
print(k)

Submission status: Wrong Answer

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):
        print(i,j)
        if nums[j]<nums[j-1]:
            nums[j],nums[j-1]=nums[j-1],nums[j]
            k+=1

print(*nums)
print(k)

Submission status: Wrong Answer

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     
    return cnt

print(*A)
print(bubblesort(A,N))

Submission status: Wrong Answer

Solution code:
"""
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
"""
def sort(A,N):
    count=0
    flag=True
    while flag:
        flag=False
        for j in range(N-1,1,-1):
            if A[j]<A[j-1]:
                tmp=A[j]
                A[j]=A[j-1]
                A[j-1]=tmp
                flag=True
                count+=1
    return count

N=int(input())
A=input().split()
out=sort(A,N)
for i in range(len(A)):
    print(A[i],end=' ')
print('\b\n'+str(out))

Submission status: Wrong Answer

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


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


N = int(input())
A = input().split()
out = sort(A, N)
for i in range(len(A)):
    print(A[i], end=' ')
print('\b\n' + str(out))

Submission status: Wrong Answer

Solution code:
'''
BubbleSort(A)
1 for i = 0 to A.length-1
2     for j = A.length-1 downto i+1
3         if A[j] < A[j-1]
4             swap A[j] and A[j-1]
'''
def sort(A):
    count=0
    for i in range(len(A)):
        for j in range(len(A)-1,i,-1):
            if A[j]<A[j-1]:
                tmp=A[j]
                A[j]=A[j-1]
                A[j-1]=tmp
                count+=1
    return count

n=int(input())
A=input().split()
t=sort(A)
for i in range(len(A)):
    print(A[i],end=' ')
print('\b\n'+str(t))

Submission status: Wrong Answer

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
        print(z)
    a+=1
print(' '.join(list(map(str,z))))
print(cnt)

Submission status: Wrong Answer

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(count)
print(*a)

Submission status: Wrong Answer

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(count)
print(*a)

Submission status: Wrong Answer

Solution code:
def inputInline():
    N=int(input())
    numbers=list(map(int,input().split(" ")))
    return numbers
def bubbleSort(list):
    N=len(list)
    count=0
    flag=True
    while flag:
        flag=False
        for i in range(N-1):
            if list[i]>list[i+1]:
                temp=list[i]
                list[i]=list[i+1]
                list[i+1]=temp
                flag=True
                count+=1
    return (list,count)
result=bubbleSort(inputInline())
print(result[0])
print(result[1])

Submission status: Wrong Answer

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(A)
    print(count)