0% found this document useful (0 votes)
78 views3 pages

C Program On Bubble Sort

This C program implements bubble sort to sort an array of integers in ascending order. The program takes input of the array size and elements, performs bubble sort by repeatedly swapping adjacent elements if they are in the wrong order, and outputs the sorted array. It uses nested for loops to iterate through the array, compares adjacent elements, and swaps them if out of order until the array is fully sorted.

Uploaded by

Med Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views3 pages

C Program On Bubble Sort

This C program implements bubble sort to sort an array of integers in ascending order. The program takes input of the array size and elements, performs bubble sort by repeatedly swapping adjacent elements if they are in the wrong order, and outputs the sorted array. It uses nested for loops to iterate through the array, compares adjacent elements, and swaps them if out of order until the array is fully sorted.

Uploaded by

Med Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C Program on Bubble sort

/*C Program to implement Bubble sort


Input : 1. Size of the Array
2. Array elements
Output : Sorted Array elements in ascending order
*/

#include <stdio.h>

int main()
{
int array[100], n, i, d, position, swap;

printf(" Enter the size of the array\n");


scanf("%d", &n);

printf("\n Enter %d integers\n", n);

for ( i = 0 ; i < n ; i++ )


scanf("%d", &array[i]);

for ( i = 0 ; i < ( n - 1 ) ; i++ )


{
position = i;

for ( d = i + 1 ; d < n ; d++ )


{
if ( array[position] > array[d] )
position = d;
}
if ( position != i )
{

C Program on Bubble sort


swap = array[i];
array[i] = array[position];
array[position] = swap;
}
}

printf("\n Sorted list in ascending order:\n");

for ( i = 0 ; i < n ; i++ )


printf(" %3d", array[i]);
printf("\n\n");
return 0;
}

C Program on Bubble sort


Sample Input and Output:

You might also like