Code for BUBBLE Sort
1 Bubble
sort
Ø
#include<stdio.h>
#include<conio.h>
void
BubbleSort(int[] , int);
void
main()
{
int array[10], n, i;
clrscr();
printf("\n\n------------------------
BUBBLE SORT -----------------------");
printf("\n\n ENTER NUMBER OF ELEMENTS :
");
scanf("%d", &n);
printf("\n");
for (i = 0 ; i < n ; i++ )
{ printf("ENTER ELEMENT %d:
",i+1);
scanf("%d", &array[i]);
}
BubbleSort(array,n);
getch();
}
void
BubbleSort(int array[], int n)
{
int i,c,j,temp;
for ( i = 0 ; i < ( n-1 ) ; i++ )
{
for ( j = 0 ; j < n-i-1 ; j++ )
{
if
( array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
printf("\nPASS %d :",i+1);
for ( c = 0 ; c < n ; c++ )
{
printf(" %d ", array[c]);
}
}
printf("\n\n FINAL SORTED ELEMENTS :
");
for ( i = 0 ; i < n ; i++ )
{
printf(" %d ", array[i]); }
getch();
}
OUTPUT:
Comments
Post a Comment