C Program for Bubble Sort Example, C Bubble Sort integer Array, Bubble Sort Program in C Language

C Program for Bubble Sort Example, C Bubble Sort integer Array, Bubble Sort Program in C Language :-

Example - Bubble Sort Program in C for integer Array with maximum 100 elements and the source is given below :-

#include<stdio.h>
#include<conio.h>

void main( )
{
int arr[100], i, j, n, temp ;
clrscr();

printf("Enter the number of elements to be sorted :");
scanf("%d",&n);

printf("\nEnter the integer elements :\n");

for(i=0 ; i<n ; scanf("%d",&arr[i]), i++); /* here for() loop ended with ; and scanf() can also be put out side for() as we use regular */
n=n-2;
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 ( "\nArray after sorting:\n");

n++;

for ( i = 0 ; i <= n ; i++ )
printf ( "%d,", arr[i] );

getch();
}

/*

if we give no of elements as 5 and elements are 23,4,77,45,3 then output will be as below:
3,4,23,45,77

*/