• + 0 comments
    if __name__ == '__main__':
        n = int(input().strip())
    
        a = list(map(int, input().rstrip().split()))
    
        # Write your code here
        swaps = 0
        last_idx = len(a) - 1
        
        while True:
            no_swaps = True
            for i in range(last_idx):
                if a[i] > a[i+1]:
                    buf_val = a[i+1]
                    a[i+1] = a[i]
                    a[i] = buf_val
                    swaps += 1
                    no_swaps = False
        
            # If no swaps occured, the array must be sorted; break out
            if no_swaps:
                break
        
        print(f"Array is sorted in {swaps} swaps.")
        print(f"First Element: {a[0]}")
        print(f"Last Element: {a[last_idx]}")