Sample code for Bubble sort in Python
Sample output(In Python 2.7):-
>>>
Enter number of numbers
5
Enter numbers separated by space
12 3 56 78 2
[2, 3, 12, 56, 78]
Explanation:-
#!/bin/python import sys print "Enter number of numbers" n = int(raw_input().strip()) print "Enter numbers separated by space" a = map(int,raw_input().strip().split(' ')) i = n numberOfSwaps = 0 for i in xrange(n, 0, -1): for j in range(0,i-1): #print a[j] if a[j]>a[j+1]: temp = a[j] a[j] = a[j+1] a[j+1] = temp numberOfSwaps =numberOfSwaps+1 ; i = i-1 print "Sorted array :"
print a
>>>
Enter number of numbers
5
Enter numbers separated by space
12 3 56 78 2
[2, 3, 12, 56, 78]
Explanation:-
raw_input()- Reads input from console. map(int,raw_input().strip().split(' '))- Read input from console and convert it into list
In python3 input() is used instead of raw_iput() to read input from console.
Such an excellent and interesting blog, do post like this more with more information, this was very useful, Thank you.
ReplyDeletebest aviation academy in Chennai
air hostess training academy in Chennai
diploma in airport management in Chennai
Ground staff training in Chennai
Aviation Academy in Chennai
air hostess training in Chennai
airport management courses in Chennai
ground staff training in Chennai
Aivivu đại lý vé máy bay, tham khảo
ReplyDeletesăn vé máy bay giá rẻ đi Mỹ
lich bay tu my ve vietnam
lịch bay từ nhật về việt nam hôm nay
mua ve may bay gia re tu duc ve viet nam
giá vé máy bay từ Vancouver về việt nam
Có chuyến bay từ Hàn Quốc về Việt Nam không
khách sạn cách ly nha trang