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;
}
ALGORITHM (Insertion of element into a linked list) Step 1 Begin the program Step 2 if the list is empty or any new element comes before the start (head) element, then add t
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
We have discussed already about three tree traversal methods in the earlier section on general tree. The similar three different ways to do the traversal -inorder , preorder, and p
1) Why space complexity is comparatively more critical than time complexity? 2) Determine the space complexity of Euclid Algorithm?
Program segment for the deletion of any element from the queue delmq(i) /* Delete any element from queue i */ { int i,x; if ( front[i] == rear[i]) printf("Queue is
Thus far, we have been considering sorting depend on single keys. However, in real life applications, we may desire to sort the data on several keys. The simplest instance is that
Merge sort is a sorting algorithm which uses the basic idea of divide and conquers. This algorithm initially divides the array into two halves, sorts them separately and then merge
CIRCULARLY LINKED LISTS IMPLEMENTATION A linked list wherein the last element points to the first element is called as CIRCULAR linked list. The chains do not specified first o
Explain Optimal Binary Search Trees One of the principal application of Binary Search Tree is to execute the operation of searching. If probabilities of searching for elements
Task If quicksort is so quick, why bother with anything else? If bubble sort is so bad, why even mention it? For that matter, why are there so many sorting algorithms? Your
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: +91-977-207-8620
Phone: +91-977-207-8620
Email: [email protected]
All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd