## Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

## Cloudizza

### Bubble Sort

`#include <stdio.h>#include <iostream.h>void bubbleSort(int *array,int length)//Bubble sort function {    int i,j;    for(i=0;i<10;i++)    {        for(j=0;j<i;j++)        {            if(array[i]>array[j])            {                int temp=array[i]; //swap                 array[i]=array[j];                array[j]=temp;            }        }    }}void printElements(int *array,int length) //print array elements{    int i=0;    for(i=0;i<10;i++)    cout<<array[i]<<endl;}void main(){    int a[]={9,6,5,23,2,6,2,7,1,8};   // array to sort     bubbleSort(a,10);                 //call to bubble sort      printElements(a,10);               // print elements }`

#### 11 comments:

1. Awesome post. I never could figure it out on my own and all other posts on the subject were far to complex for me to be able to understand them.

The only thing is that it sorts from highest to lowest, and I can't seem to get it to sort from lowest to highest. It isn't a big deal, but perhaps including in comments how to do this would make it an even better post.

2. Easiest way ever seen

3. eyy how on my dev-C++ it doesnt compile ,it says there no such library as ...plz help!!!

4. eyy how come on my dev-C++ it doesnt compile ,it says there is no such library as "iostream.h"...plz help!!!

5. remove the import i think it should work.

6. #include
#include
const int num= 5;
void main()
{
clrscr();
int arr[num];
int i, j;
int temp;
cout<<"Please enter the Numbers for array\n for bubble sort.";
for(int a=0; a<5;a++)
{ cout<<"\nEnter "<>arr[a];
}
for(i = 0; i arr[j])
{
temp = arr[j]; // swap elements
arr[j] = arr[j+1];
arr[j+1] = temp;
// flag = 1; // indicates that a swap occurred.
}
}
}
cout<<"\nsorting array\n";
for(a=0; a<5;a++)
{
cout<<arr[a]<<endl;
}
getch();
}

7. Please add two include file in last code as iostream.h and conio.h

8. or this might help
using namespace std;

9. hahahaha yes u deserve a million dollars i just had my best evenx

10. do you have code about bubble sort?

11. can you post a bubble sort without loop?