-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting-Bubble Sort.py
More file actions
39 lines (29 loc) · 879 Bytes
/
Sorting-Bubble Sort.py
File metadata and controls
39 lines (29 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#Problem Link : https://www.hackerrank.com/challenges/ctci-bubble-sort/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=sorting
#Ans:
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'countSwaps' function below.
#
# The function accepts INTEGER_ARRAY a as parameter.
#
def countSwaps(a):
count = 0
for i in range(len(a)):
for j in range(i+1, len(a)):
if a[j] < a[i]:
a[i] = a[i]+a[j]
a[j] = a[i]-a[j]
a[i] = a[i] - a[j]
count += 1
print('Array is sorted in {} swaps.'.format(count))
print('First Element:', a[0])
print('Last Element:', a[-1])
if __name__ == '__main__':
n = int(input().strip())
a = list(map(int, input().rstrip().split()))
countSwaps(a)