Bubble Sort in C

c-techaravind
                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.




#include <stdio.h>
#include <conio.h>
void main( )
{
int i,j, n,temp, arr[20];
clrscr();
gotoxy(31,1);
printf("bubble sort");
gotoxy(26,3);
printf("www.techaravind.blogspot.com");
gotoxy(1,5);
printf("\nEnter the no. of elements for array:");
scanf("%d",&n);
printf("\nEnter the elements of the array:");
for(i=0;i<=n-1;i++)
{
scanf("%d",&arr[i]);
}
for ( i = 0 ; i <= n ; i++ )
{
for ( j = 0 ; j <= n - i ; j++ )
{
if ( arr[j] > arr[j + 1] )
{
temp = arr[j] ;
arr[j] = arr[j + 1] ;
arr[j + 1] = temp ;
}
}
}

printf ( "\n\nArray after sorting:\n") ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;
getch();
}

0 comments:

Post a Comment

 
Etutos © 2010-2011