Question 3
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;
}