Sorting Hacker Solution
import sys
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
numOfSwap = 0
for i in range(n):
for j in range(n-1):
if(a[j]>a[j+1]):
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
numOfSwap += 1
print("Array is sorted in "+str(numOfSwap)+ " swaps.")
print("First Element: "+str(a[0]))
print("Last Element: "+str(a[n-1]))
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
numOfSwap = 0
for i in range(n):
for j in range(n-1):
if(a[j]>a[j+1]):
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
numOfSwap += 1
print("Array is sorted in "+str(numOfSwap)+ " swaps.")
print("First Element: "+str(a[0]))
print("Last Element: "+str(a[n-1]))
Comments
Post a Comment