ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

C Program for Bubble Sort or sorting using Arrays

Updated on December 21, 2012

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 .

Comments

    0 of 8192 characters used
    Post Comment

    • Davesworld profile image

      Davesworld 5 years ago from Cottage Grove, MN 55016

      Change you second "for" statement to:

      for(j=i;j "less than" n;j++) /* stupid comment box won't allow the inequality sign */

      You do not need to re-examine what you have already sorted.

    • techniche profile image
      Author

      techniche 5 years ago from Ambala, India

      Are you talking about this for loop ?? : for(j=0;j"less than"n-i-1;j++);

    • profile image

      ewane nelson 5 years ago

      i'll like to be like one of you someday. please teach me programming

    • profile image

      vishakha.38@gmail.com 4 years ago

      its helpful to me

    • profile image

      rahul 3 years ago

      thanks

    Click to Rate This Article