We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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]}')
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 20: Sorting
You are viewing a single comment's thread. Return to all comments →
Python3 solution *Check on the indentation python if name == 'main': n = int(input().strip())