Saturday, 28 January 2017

Bubble sort program in C using arrays, function ~ mkniit ~ gniitsolution

Bubble sort program in C using arrays and function. It is a type of sorting mechanism. Sample program also available for your reference. Source code of this C tutorial is given below,

BUBBLE SORT IN C

Bubble sort program in C is used to arrange the given values in an ascending order by using arrays and functions. And also an operation of this sorting will be based on some statements like unconditional statements. We can easily get the list of output values in an order. For example, your value is 3,7,1,10,9,-88. After bubble sorting, your input will display as -88,1,3,7,9,10.

BUBBLE SORT EXAMPLE PROGRAM:

Q: Write a C program for Bubble sort using arrays and function.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Header files
#include<stdio.h>
#include<conio.h>
void main()
{
 
//Program variables
int b[20],j,i,m,h=0;
 
clrscr(); //Function to clear previous output
 
printf("Enter array size:"); //Display function
scanf("%d",&m); //Getting input function
printf("Enter %d elements:" ,n);
 
for(i=1;i<=m;i++) //Looping statement
scanf("%d",&b[i]);
for(i=2;i<=m;i++)
{
j=i-1;
 
while((j>=1)&&(b[j]>b[j+1])) //Unconditional statement
{
h=b[j];
b[j]=b[j+1];
b[j+1]=h;
j=j-1;
}
}
printf("Sorted List is::\n");
for(i=1;i<=n;i++)
printf("%d\t",b[i]);
getch();
}

No comments:

Post a Comment