- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming
C Program for Bubble Sort or sorting using Arrays
Bubble Sorting is used in C programming to sort the numbers in increasing order . We just need to enter the required no. and this program will do rest of the work .
Following is the source code for Bubble Sorting program . You just need to copy it in your notepad and save it with the extension *.c or *.cpp . Then open the saved file with your compiler and run it .
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j, temp , a[100];
printf("Enter the total integers you want to enter (make it less than 100):\n");
scanf("%d",&n);
printf("Enter the %d integer array elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j+1]<a[j])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("The sorted numbers are:");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
getch();
}
You may also want to read : C program for insertion sorting in arrays
This program has been written by me for maximum of 100 integers to sort out . If you feel like you want to increase the numbers for sorting , you may change the value of a[100] to a[xyz] where xyz is any integer value .
Further more , you can change the program according to your needs . :) Enjoy Programming .
P.S - The screenshot is the proof of a working program .