Sorting array elements with bubble sort | Without Using Function | Using Function | Pointer | C-program
Bubble Sort:
Tips:
- Use for loop.
- Use if and else.
- Do swapping.
- Bubble sort is done from starting of array.
Without Function:
Code:
#include<stdio.h>
void main()
{
int i,j,k,a[]={9,2,7,3};
for(j=0;j<=3;j++)
for(i=0;i<3;i++)
{
if(a[i]>a[i+1])
{
k=a[i];
a[i]=a[i+1];
a[i+1]=k;
}
}
for(i=0;i<=3;i++)
{
printf("%d\t",a[i]);
}
getch();
}
Using function
Code:
#include<stdio.h>
void main()
{
int i,a[]={9,6,7,3},n;
sort(a,3);
for(i=0;i<=3;i++)
{
printf("%d\t",a[i]);
}
getch();
}
void sort(int a[],int n)
{
int i,j,k;
for(j=0;j<=n;j++)
for(i=0;i<=n;i++)
{
if(a[i]>a[i+1])
{
k=a[i];
a[i]=a[i+1];
a[i+1]=k;
}
}
}
Wow
ReplyDelete