BUBBLE SORT IN PYTHON

WRITE A PROGRAM TO IMPLEMENT BUBBLE SORT.

def Bubble_Sort(MyList):
    for i in range(len(MyList)-1,0,-1):
        for j in range(i):
            if MyList[j]>MyList[j+1]:
                temp,MyList[j]=MyList[j],MyList[j+1]
                MyList[j+1]=temp
MyList=[30,50,45,23,20,90,79]
print("Elements of List Before Sorting:",MyList)
Bubble_Sort(MyList)
print('Element of List After Sorting:',end='')
print(MyList)     

EXPLANATION:-

In the above program,the unsorted list is declared and initialized. The same list is passed as an argument to the function bubble sort.The list slicing operation is used to iterate through the loop. Each element is compared with its adjacent element and interchanged if the first element is greater than the second. Swapping of elements is done using Python's simultaneous assignment as shown.


PROGRAM:-

PROGRAM TO IMPLEMENT BUBBLE SORT


OUTPUT:-

PROGRAM TO IMPLEMENT BUBBLE SORT




Also see:-

PROGRAM FOR BINARY SEARCH


LINEAR SEARCH PROGRAM IN PYTHON



Comments

Popular posts from this blog

BINARY SEARCH IN PYTHON

Selection sort program in python

LINEAR SEARCH PROGRAM IN PYTHON