0% found this document useful (0 votes)
17 views

Question 3

Uploaded by

Ritesh Vashisht
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Question 3

Uploaded by

Ritesh Vashisht
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

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