#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
}
Labels
Bubble Sort
Subscribe to:
Post Comments (Atom)
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.
ReplyDeleteThe 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.
Easiest way ever seen
ReplyDeleteeyy how on my dev-C++ it doesnt compile ,it says there no such library as ...plz help!!!
ReplyDeleteeyy how come on my dev-C++ it doesnt compile ,it says there is no such library as "iostream.h"...plz help!!!
ReplyDeleteremove the import i think it should work.
ReplyDelete#include
ReplyDelete#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();
}
Please add two include file in last code as iostream.h and conio.h
ReplyDeleteor this might help
ReplyDeleteusing namespace std;
hahahaha yes u deserve a million dollars i just had my best evenx
ReplyDeletedo you have code about bubble sort?
ReplyDeletecan you post a bubble sort without loop?
ReplyDelete