0
Bubble Sort
hi would you help me to find out what are my mistakes in this code in c++ #include <iostream> using namespace std; int index(int, int[],int); int main() { int a[]={22,44,66,88,44,66,55}; { cout << "index (44,a,7),"<<index (44,a,7)<<endl; cout << "index (50,a,7),"<<index (50,a,7)<<endl; } int index(int x, int a[],int n); { int x,n,i; for(int i=0; i<n;i++) if (a[i]==x) return i; return n; }
3 odpowiedzi
+ 4
I think you are bit confused because your code looks like for searching not bubble sort even though it's not correct either(some syntax errors).
For bubble sort Rajeeb already answered.
+ 2
//this is a working example
#include<iostream>
using namespace std;
int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<"Array after bubble sort:";
for(i=0;i<n;++i)
cout<<" "<<a[i];
return 0;
}
0
Thank you for the answer Rajeeb.
it was helpful.