0% found this document useful (0 votes)
33 views1 page

Question 3

The document describes implementing bubble sort to sort the numbers 7, 1, 4, 12, 67, 33, and 45. The C++ code provided implements bubble sort on this list of numbers. Running the code would output the number of passes or swaps needed to sort the list using bubble sort.

Uploaded by

Ritesh Vashisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views1 page

Question 3

The document describes implementing bubble sort to sort the numbers 7, 1, 4, 12, 67, 33, and 45. The C++ code provided implements bubble sort on this list of numbers. Running the code would output the number of passes or swaps needed to sort the list using bubble sort.

Uploaded by

Ritesh Vashisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 3

Write a program to implement bubble sort. Given the numbers 7, 1, 4, 12, 67, 33, and 45. How many swaps will be performed to
sort these numbers using the bubble sort?

INPUT:
#include <iostream>
using namespace std;
int main ()
{
  int i, j,temp,pass=0;
  int a[7] = {7, 1, 4, 12, 67, 33, 45};
  cout <<"Input list ...\n";
  for(i = 0; i<7; i++) {
   cout <<a[i]<<"\t";
  }
cout<<endl;
for(i = 0; i<7; i++) {
  for(j = i+1; j<7; j++)
  {
   if(a[j] < a[i]) {
     temp = a[i];
     a[i] = a[j];
     a[j] = temp;
   }
  }
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<7; i++) {
  cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
return 0;
}

You might also like