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;
}
The fundamental element of linked list is a "record" structure of at least two fields. The object which holds the data & refers to the next element into the list is called a node .
a) Find the shortest paths from r to all other nodes in the digraph G=(V,E) shown below using the Bellman-Ford algorithm (as taught in class). Please show your work, and draw the f
Algorithm for Z-Buffer Method (a) Initialize every pixel in the viewport to the smallest value of z, namely z0 the z-value of the rear clipping plane or "back-ground". Store a
Q. By giving an example show how multidimensional array can be represented in one the dimensional array.
Q. Sort the sequence written below of keys using merge sort. 66, 77, 11, 88, 99, 22, 33, 44, 55 Ans:
Ways to implement abstract data types A large part of the study of data structures and algorithms is learning about alternative ways to implement an ADT and evaluating alternat
red black tree construction for 4,5,6,7,8,9
While BFS is applied, the vertices of the graph are divided into two categories. The vertices, that are visited as part of the search & those vertices that are not visited as part
An advertising project manager developed the network diagram shown below for a new advertising campagign. In addition, the manager gathered the time information for each activity,
In what ways we can get the context sensitive F1 help on a field?' Data element documentation. Data element additional text in screen painter. Using the process on help r
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