python interview questions and answers

Basic Python Interview Question and Answers

Monday, 2 January 2017

What is Bubble sort?

 It compares adjacent items and exchanges those that are out of order.

Example:
x=[47, 98, 85, 85, 67, 67]
def bubble_sort(x):
for i in range(len(x)-1,0,-1):
for j in range(i):
if x[j]>x[j+1]:
temp=x[j]
x[j]=x[j+1]
x[j+1]=temp
return x

>>> print(bubble_sort(x))
[47, 67, 67, 85, 85, 98]
Posted by GEETA BASRA at 03:57
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Newer Post Older Post Home

Popular Posts

Blog Archive

  • ▼  2017 (17)
    • ▼  January (17)
      • NUMPY Queries
      • What is pickle in Python?
      • Excercise and Answers
      • Time Comparison between Python Lists and Numpy Arrays
      • what is linspace?
      • Difference between range and arange?
      • What is arange?
      • How to import data with genfromtxt?
      • How do we create a Numpy array?
      • How to convert Python array_like Objects to Numpy ...
      • Sort a python Dictionary by Value
      • What is Bubble sort?
      • Insertion Sort in Python
      • Quicksort in Python
      • what is sorting algorithm?
      • what is Numpy?
      • What is a class in Python?
  • ►  2016 (12)
    • ►  December (12)
Simple theme. Powered by Blogger.