• + 0 comments

    Python3 solution *Check on the indentation python if name == 'main': n = int(input().strip())

    a = list(map(int, input().rstrip().split()))
    swap = 0
    #Traverse through all the elements in the array
    for i in range(n):
    
        for j in range(n-1):
            #Traverse the array from 0 to n-1
            #Swap the element if the element is found greater than the next element
            if a[j] > a[j+1]:
                swap += 1
                a[j], a[j+1] = a[j+1], a[j]
    
    print(f'Array is sorted in {swap} swaps.')
    print(f'First Element: {a[0]}')
    print(f'Last Element: {a[-1]}')