### Bubblesort

#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::dec;
const int SIZE=12;
int bubbleSort( int a[], int last);//function prototype
int main(){//
/*double a[11], b[34];*/
int i=0;
int n;

int a[11]={2,4,12,1,1,1,5,2,3,3,4};
for (i=0; i<11; i++){
cout<<setw(3)<<a[i]<<"\n";}

int bubbleSort( int a[], int j=10);

for( int k=0; k<11; k++){

cout<<setw(3)<<dec<<bubbleSort<<"\n";
}

int b[34]={};

cin>>b[11];
return 0;
}

int bubbleSort( int a[11], int j=10){
int temp;//holding variable
bool located;
/* int walker;*/
for (int current=0; current<j; current++)
{
//Inner loop: bubble up one element each pass
for( int walker=j;// walker value starts at 10, greater than any current value
walker>current;//current value increases from 0 to 10
walker--)//when walker value reaches 5, current value reaches 5, and the loop is terminated.
if (a[walker]<a[walker-1])
{
temp=a[walker];//the value of a[walker] is removed prior to the inserting of value of a[walker-1] at element walker.
a[walker]=a[walker-1];//element a[walker] receives the value of a[walker -1]
a[walker-1]=temp;//restore the value of a[walker] to a[walker-1]
}
return a[11];
}//end bubblesort
}
Last edited on
This is the output:

2
4
12
1
1
1
5
2
3
3
4
00351299
00351299
00351299
00351299
00351299
00351299
00351299
00351299
00351299
00351299
00351299

Why is the array not being sorted?
http://www.cplusplus.com/articles/z13hAqkS/

 Why is the array not being sorted?

because you never call your bubbleSort function. This `int bubbleSort( int a[], int j=10);` is not a function call.

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
Topic archived. No new replies allowed.