Monday, 25 April 2011

Bubble Sort in C


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define N 10
int * BubbleSort(int*, int);
void main()
{

int a[10]={3,7,8,5,2,1,9,5,4,11};
//new sorted array
SelectionSort(a,N);
BubbleSort(a,N);
printf("BUBBLE SORT");
printf("\n");
for(int i=0;i<N;i++)
{
printf("%d\n",*(a+i));
}
_getch();
}

----------------------------------------------------
Bubble sort function
----------------------------------------------------

int* BubbleSort(int  a[],int n)
{
int temp=0;
for(int i=n-2;i>=0;i--)
{
for (int j=0;j<=i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

return a;
}
----------------------------------------------------------------------------------------------------
output:



No comments:

Post a Comment