Bubble sort

Mar 29, 2014 at 10:47am
can someone help to bubble sort this one, thank you

#include<iostream>
using namespace std;

int main ()
{
int sum=0;
int mean=0;
int median=0;
int mode=0;
int grades[10] = {71,82,83,83,95,96,67,78,89,90};

for (int i = 0; i < 10; ++i)
sum+=grades[i];
mean = sum/10;

cout<<"Mean:"<<mean<<endl;

{
int j=0;

for (int i = 0; i < 10; ++i)
{
for (j=0; j<i; ++i)
{
if (grades[i]>grades[j])
{
int temp=grades[i];
grades[i]=grades[j];
grades[j]=temp;
}
}
}
}

system("pause");
return 0;
}
Mar 29, 2014 at 3:15pm
@eLypots

In your for loop of j, you have ++i instead of ++j

You still need a for loop to print out grades after the sort.
Mar 30, 2014 at 12:39pm
how ?
Mar 30, 2014 at 2:09pm
@eLypots

Just before you finish the program, use a for loop
1
2
3
4
for (int x=0;x<10;x++)
{
  cout << grades[x] << " "; // The " ", is to put a space between each number in the array
}


Last edited on Mar 30, 2014 at 4:22pm
Topic archived. No new replies allowed.