Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. import sys
  2.  
  3. n = int(raw_input().strip())
  4.  
  5. a = map(int, raw_input().strip().split(' '))
  6.  
  7. numberofswaps = 0
  8. for i in range(n):
  9.  
  10.     j = 0
  11.     while j < n - 1:
  12.         if (a[j] > a[j+1] ):
  13.             temp1 = a[j]
  14.             temp2 = a[j+1]
  15.             a[j] = temp2
  16.             a[j+1] = temp1
  17.             numberofswaps = numberofswaps + 1
  18.            
  19.  
  20.         j=j+1
  21.  
  22.     if numberofswaps == 0:
  23.         break
  24.  
  25.  
  26. print 'Array is sorted in ' + str(numberofswaps) + ' swaps.'
  27. print 'First Element: ' + str(a[0])
  28. print 'Last Element: ' + str(a[len(a)-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement