5th sem ADA programm to implement sorting algorithms
- Bubble sort in c
#include<stdio.h>
void bubble(int n,int arr[]);
void main()
{
int n;
printf("enter the size of array:");
scanf("%d",&n);
int arr[n];
int i,j,k;
printf("enter the array elemets:\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
bubble(n,arr);
printf("\nyour sorted array is :\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
}
void bubble(int n, int arr[n])
{
int i,j,swap;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
}
}
}
#include<stdio.h>
void bubble(int n,int arr[]);
void main()
{
int n;
printf("enter the size of array:");
scanf("%d",&n);
int arr[n];
int i,j,k;
printf("enter the array elemets:\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
bubble(n,arr);
printf("\nyour sorted array is :\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
}
void bubble(int n, int arr[n])
{
int i,j,swap;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
}
}
}
Comments
Post a Comment