0% found this document useful (0 votes)
70 views3 pages

Algorithims Labmanual 3 Solve

The document contains the C++ code to implement bubble sort algorithm. It runs the code on an sample array of numbers - {32, 54, 27, 80, 63, 23, 11, 57}. It prints the output of each pass of the algorithm, showing the sorted elements moving towards the front of the array with each pass. The code takes a total of 8 passes to completely sort the array in ascending order.

Uploaded by

irfan_chand_mian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views3 pages

Algorithims Labmanual 3 Solve

The document contains the C++ code to implement bubble sort algorithm. It runs the code on an sample array of numbers - {32, 54, 27, 80, 63, 23, 11, 57}. It prints the output of each pass of the algorithm, showing the sorted elements moving towards the front of the array with each pass. The code takes a total of 8 passes to completely sort the array in ascending order.

Uploaded by

irfan_chand_mian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q.1 Convert the Bubble Sort algorithm into C++ Code.

1. #include<iostream.h>
2. #include<conio.h>
3. const int size=8;
4. main()
5. {
6. clrscr();
7. int Array[size];
8. int i,j,temp;
9. cout<<"Enter a element of the array :\n";
10. for(i=0 ; i<size ; i++)
11. cin>>Array[i];
12. for(i=0 ; i<size ; i++)
13.{
14. for(j=0 ; j<size-1 ; j++)
15. {
16. if(Array[j]>Array[j+1])
17. {
18. temp=Array[j];
19. Array[j]=Array[j+1];
20. Array[j+1]=temp;
21. }
22. }
23. }
24. cout<<"\nThe sorted array are :";
25. for(i=0 ; i<size ; i++)
26. cout<<Array[i]<<" ";
27. getch();
28. }
Q.2 Run the above code for the best case (all the array elements in increasing
order).
Best Case: The best case would be if the list were already sorted. a) there will be
comparisons as it is but no exchanges and execution time is in O(n2) b) But if we keep a track of
exchanges in each pass and terminate the program checking if no exchanges. Then the program
would require only one pass and max. (n-1) comparisons are required in that single pass and we
can say that the complexity is of order of O (n).Best case performance is O (n).
Q.3 Run the above code for the worst case (all the array elements in decreasing
order).
The worst case is that you will have the smallest value in the last space in the array. This means
that it will move exactly once each pass towards the first space in the array so passes one takes
n-1 comparisons, passes two n-2 comparisons, and passes three n-3 comparisons and so on.
Worst case performance is O (n²)
Q.4 Run the above code for the following array: A={32, 54, 27, 80, 63, 23, 11,
57}Also print the result for each pass of the algorithm.

#include<iostream.h>
#include<conio.h>

main()
{
clrscr();
int Array[8]={32,54,27,80,63,23,11,57};
int i,j,temp,k;

for(i=0 ; i<8 ; i++)


{
cout<<"Passes "<<i+1<<":";
cout<<endl;
for(j=0 ; j<7; j++)
{
if(Array[j]>Array[j+1])
{
temp=Array[j];

Array[j]=Array[j+1];
Array[j+1]=temp;

}
for(k=0;k<8;k++)
cout<<Array[k]<<" ";
cout<<"\n";

}
cout<<"\n";
}
cout<<"\nThe sorted array are :";
for(i=0 ; i<8 ; i++)
cout<<Array[i]<<" ";
getch();
}
Output:
Pass 1: Pass 5:
32, 54,27,80,63,23,11,57 23, 27,11,32,54,57,63,80
32,27,54,80,63,23,11,57 23, 11,27,32,54,57,63,80
32,27,54,80,63,23,11,57 23, 11,27,32,54,57,63,80
32, 27,54,63,80,23,11,57 23, 11,27,32,54,57,63,80
32, 27,54,63,23,80,11,57 23, 11,27,32,54,57,63,80
32, 27,54,63,23,11,80,57 23, 11,27,32,54,57,63,80
32, 27,54,63,23,11,57,80 Pass6:
Pass 2: 11, 23,27,32,54,57,63,80
27, 32,54,63,23,11,57,80 11, 23,27,32,54,57,63,80
27, 32,54,63,23,11,57,80 11, 23,27,32,54,57,63,80
27, 32,54,63,23,11,57,80 11, 23,27,32,54,57,63,80
27, 32,54,23,63,11,57,80 11, 23,27,32,54,57,63,80
27, 32,54,23,11,63,57,80 11, 23,27,32,54,57,63,80
27, 32,54,23,11,57,63,80, 11, 23,27,32,54,57,63,80
27, 32,54,23,11,57,63,80 Pass7:
Pass 3: 11, 23,27,32,54,57,63,80
11, 23,27,32,54,57,63,80
27, 32,54,23,11,57,63,80 11, 23,27,32,54,57,63,80
27, 32,54,23,11,57,63,80 11, 23,27,32,54,57,63,80
27, 32,23,54,11,57,63,80 11, 23,27,32,54,57,63,80
27,32, 23,11,54,57,63,80 11, 23,27,32,54,57,63,80
27, 32,23,11,54,57,63,80 11, 23,27,32,54,57,63,80
27, 32,23,11,54,57,63,80 Pass8:
27, 32,23,11,54,57,63,80 11, 23,27,32,54,57,63,80
Pass 4: 11, 23,27,32,54,57,63,80
27, 32,23,11,54,57,63,80 11, 23,27,32,54,57,63,80
27, 23,32,11,54,57,63,80 11, 23,27,32,54,57,63,80
27, 23,11,32,54,57,63,80 11, 23,27,32,54,57,63,80
27, 23,11,32,54,57,63,80 11, 23,27,32,54,57,63,80
27, 23,11,32,54,57,63,80 11, 23,27,32,54,57,63,80
27, 23,11,32,54,57,63,80

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy