Already have an account? Get multiple benefits of using own account!
Login in your account..!
Remember me
Don't have an account? Create your account in less than a minutes,
Forgot password? how can I recover my password now!
Enter right registered email to receive password!
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; } } }
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; } } }
switched=false;
for(j=0;j { switched=true; hold=x[j]; x[j]=x[j+1]; x[j+1]=hold; } } }
switched=true; hold=x[j]; x[j]=x[j+1];
x[j+1]=hold;
}
what is frequency count with examble? examble?
The controversy of RISC versus CISC never ends. Suppose that you represent an advocate for the RISC approach; write at least a one-page critic of the CISC approach showing its disa
State in brief about assertion Assertion: A statement which should be true at a designated point in a program.
As we have seen, as the traversal mechanisms were intrinsically recursive, the implementation was also easy through a recursive procedure. Though, in the case of a non-recursive me
A BST is traversed in the following order recursively: Right, root, left e output sequence will be in In Descending order
What are stacks? A stack is a data structure that organizes data similar to how one organizes a pile of coins. The new coin is always placed on the top and the oldest is on the
The following DNA sequences are extracted from promoter region of genes which are co-regulated by the same transcription factor (TF). The nucleotide segments capitalized in the giv
You are supposed to do the following: Write a parallel implementation of the raytracer using pthreads. Measure and compare the execution times for (i) the sequential ver
Q. Write down the algorithm for binary search. Which are the conditions under which sequential search of a list is preferred over the binary search?
what are the disadvantages of sparse matrix?
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!
whatsapp: +1-415-670-9521
Phone: +1-415-670-9521
Email: [email protected]
All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd