Description:
In the program given below first length of the array is accepted from the user, later the array is accepted from the user in the random order, and this array is sorted using bubble sort technique. Finally sorted array is displayed On the console.
Code:
//C program to illustrate bubble sort//
//------------------------------------
//Program tested in:Code::blocks 8.02
//Tested by:Programmed geek
//Date:28/07/2013
//-------------------------------------
#include<stdio.h>//Standard input output header file
//header file for including std commands like clear screen
#include<conio.h>
int main()//start of the program
{
int a[100];//initializing variable for the array
int i,j,n,t;//declaring temp variables for sorting
//accepting the value of length of the array
printf("Enter the length of the array: ");
scanf("%d",&n);
//accepting the values of the array
printf("Enter the elements of the array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
//Bubble sorting
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
//printing the sorted list
printf("Sorted array is: \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
Output:
Comments
Post a Comment