Bubble sort, Data Structure & Algorithms

Assignment Help:

Q. The reason bubble sort algorithm is inefficient is that it continues execution even after an array is sorted by performing unnecessary comparisons. Therefore, the number of comparisons in the best and worst cases both are same. Modify the algorithm such  that it will not make the next pass when the array is already sorted.                                                          

Ans:

The bubble sort continues the execution even after an array is sorted. To avoid unnecessary comparisons we add a Boolean variable say switched and initialize it by True in the starting. Along with the "for" loop, we hear add the condition (switched=true) and make it false inside the outer for loop. If a swapping is done then the value of switched is made true. Thus if no swapping has been done in the first pass, then no more comparisons will be done further and the program shall exit.

The algorithm after modifying it in  the  above  stated manner will  be  as follows:-

void bubble(int x[],int n)

{

int j,pass,hold;

bool switched=true;

for(pass=0;pass

{

switched=false;

for(j=0;j

{

switched=true; hold=x[j]; x[j]=x[j+1];

x[j+1]=hold;

}

}

}


Related Discussions:- Bubble sort

Implement stack using two queues, How To implement stack using two queues ,...

How To implement stack using two queues , analyze the running time of the stack operations ?

Prims algorithm, how to implement prims algorithm dynamically

how to implement prims algorithm dynamically

space, What is Space complexity of an algorithm? Explain

What is Space complexity of an algorithm? Explain.

If else, design algorithm and flow chart that computes the absolute differe...

design algorithm and flow chart that computes the absolute difference of two values x and y

Compare and contrast various sorting techniques, Q. Compare and contrast va...

Q. Compare and contrast various sorting techniques or methods with respect to the memory space and the computing time.

The space - time trade off, The best algorithm to solve a given problem is ...

The best algorithm to solve a given problem is one that requires less space in memory and takes less time to complete its execution. But in practice it is not always possible to

Graph with n vertices will absolutely have a parallel edge, A graph with n ...

A graph with n vertices will absolutely have a parallel edge or self loop if the total number of edges is greater than n-1

Red black tree, red black tree construction for 4,5,6,7,8,9

red black tree construction for 4,5,6,7,8,9

C, padovan string

padovan string

Post order traversal, Post order traversal: The children of node are vi...

Post order traversal: The children of node are visited before the node itself; the root is visited last. Each node is visited after its descendents are visited. Algorithm fo

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd