C program to arrange numbers in ascending order using Bubble sort

This page has C program to arrange numbers in ascending order using Bubble sort. Bubble sort is also known as sinking sort. The Bubble sort is an algorithm that repeatedly go through the list to be sorted. During the process of Bubble sort algorithm, compares each pair of adjacent items and swaps them if they are not in order. The below given C program to arrange the given numbers in ascending order using Bubble sort algorithm. Kindly check out the program to arrange the given numbers in ascending order using Bubble sort.

Program to Implement Bubble sort

#include <stdio.h>

int main(){

int n, p, q, exchange, ascend[100];

clrscr();

printf("How many data elements :

");

scanf("%d", &n);

printf("Enter %d integer data elements :

", n);

for (p=1; p<=n; ++p)

scanf("%d", &ascend[p]);

for (p=1; p<=n-1; ++p)

{

for (q=1; q<=n-p; ++q)

{

if (ascend[q] > ascend[q+1])

{

exchange = ascend[q];

ascend[q] = ascend[q+1];

ascend[q+1] = exchange;

}

}

}

printf("



The ascending order using Bubble sort algorithm is :



");

for (p=1; p<=n; ++p)

printf("%d

", ascend[p]);

getch();

return 0;

}

Sample output:





How many data elements : 7



Enter 6 integer data elements : 4 -6 3 18 2 7 5



The ascending order Bubble sort algorithm is . . . -6 2 3 4 5 7 18