Gadgets 4 Students Career Guide Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
245 views
in RTU/BTU B.Tech(CSE-III Sem) DSA LAB by Goeduhub's Expert (7.6k points)
recategorized by
Write a program to to implement Bubble Sort

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Ques. Write a program to to implement Bubble Sort

Answer : 

Bubble sort : Bubble sort  is a sorting algorithm which  is also called  comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order.

Example : 

First Pass:
15 ,1 , 4 , 2  , 18   =>  1, 15, 4, 2, 18   # Here, algorithm compares the first two elements, and swaps since 15 > 1.
1 , 15 , 4 , 2  ,18   =>  1, 4 ,15 ,2 ,18  #Swap since 15 > 4
1 , 4 , 15 , 2 , 18   => 1 ,4 ,2 ,15, 18  #Swap since 15 > 2
1 , 4 , 2 , 15 , 18   =>  1, 4 ,2 ,15 ,18   #Now, since these elements are already in order (18 > 15), algorithm does not swap them.

Second Pass:
 1 , 4  , 2 ,15 ,18  =>  1 , 4 , 2 ,15 ,18 
 1 , 4 , 2 , 15 ,18  =>  1 , 2 , 4 ,15 ,18   #Swap since 4 > 2
 1 ,2 , 4 , 15 , 18  =>  1 , 2  ,4 ,15 ,18 
 1 , 2 , 4 ,15  ,18  =>  1 , 2 , 4 ,15 ,18 

Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

Third Pass:
 1 ,2 ,4 ,15 ,18  =>  1 ,2 ,4 ,15, 18 
 1 ,2 ,4 ,15 ,18  =>  1 ,2 ,4 ,15 ,18 
 1 ,2 ,4 ,15 ,18  =>  1 ,2 ,4 ,15, 18 
 1 ,2 ,4 ,15 ,18  =>  1 ,2 ,4 ,15 ,18 

Algorithm : 

BS(list)
   for all elements of list
      if list[i] > list[i+1]
         swap(list[i], list[i+1])
      end if
   end for

   return list

end BS

Program : 

a1=input("enter elements : ")

a=a1.split(",")

b=[]

for i in range(0,len(a)):  

    for j in range(i+1,len(a)):  

        if a[j]<a[i]:  

            temp = a[j]  

            a[j]=a[i]  

            a[i]=temp  

print("List of sorted elements : ",end="")  

for i in a:   

    b.append(i)

print(b) 

Output : 

enter elements : 45,34,23,1,54,2 

List of sorted elements: ['1', '2', '23', '34', '45', '54']


For more Rajasthan Technical University CSE-III Sem DSA Lab Experiments  CLICK HERE

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory
...