Friday, February 1, 2019

Bubble Sort

  • Problem Description

    Sort the given set of numbers using Bubble Sort. The first line of the input contains the number of elements, the second line of the input contains the numbers to be sorted. In the output print the status of the array at the 3rd iteration and the final sorted array in the given format
  • CODING ARENA
  • a = []
    c=0
    b=int(input())
    for i in range(b):
        x=int(input())
        a.append(x)

    def p():
        for i in range(b):
            print(a[i],end=" ")
    for j in range(len(a)):
        swapped = False
        i = 0
        c+=1
        while(i<len(a)-1):
            if(a[i]>a[i+1]):
                a[i],a[i+1] = a[i+1],a[i]
                swapped = True

            i = i+1
        if(c==3):
            p()
        if(swapped == False):
            break
    print()
    print("Sorted array:")
    for i in range(b):
    print (a[i],end=" ")
  • Test Case 1

    Input (stdin)
    7
    
    64
    
    34
    
    25
    
    12
    
    22
    
    11
    
    90
    
    
    Expected Output
    12 22 11 25 34 64 90 
    
    Sorted array:
    
    11 12 22 25 34 64 90
  • Test Case 2

    Input (stdin)
    0
    
    
    Expected Output
    0

No comments:

Post a Comment