Friday, February 1, 2019

Selection Sort - 2

  • Problem Description

    Sort the given set of numbers using Selection 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 4th iteration and the final sorted array in the given format
  • CODING ARENA

  • a=int(input())
    A = []
    for i in range(a):
        b=int(input())
        A.append(b)
                   
    c=0
    for i in range(len(A)):
        
       
        min_idx = i
        c+=1
        for j in range(i+1, len(A)):
            if A[min_idx] > A[j]:
                
                min_idx = j
                
        if(c==4):
            for k in range(a):
                print(A[k],end=" ")
        A[i], A[min_idx] = A[min_idx], A[i]


    print ("\nSorted Array:")
    for i in range(len(A)):
        print(A[i],end=" ")
  • Test Case 1

    Input (stdin)
    5
    
    25
    
    47
    
    11
    
    65
    
    1
    
    
    Expected Output
    1 11 25 65 47 
    
    Sorted Array:
    
    1 11 25 47 65
  • Test Case 2

    Input (stdin)
    7
    
    14
    
    83
    
    25
    
    47
    
    9
    
    77
    
    1
    
    
    Expected Output
    1 9 14 47 83 77 25 
    
    Sorted Array:
    
    1 9 14 25 47 77 83

No comments:

Post a Comment