0% found this document useful (0 votes)
5 views6 pages

Sorting Algorithms in C: Bubble, Insertion, Selection, Merge

The document contains C programs for four different sorting algorithms: bubble sort, insertion sort, selection sort, and merge sort. Each program prompts the user to input an array size and elements, sorts the array using the respective algorithm, and then displays the sorted array. The implementations include function definitions for sorting and necessary input/output operations.

Uploaded by

theratimanideep
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Sorting Algorithms in C: Bubble, Insertion, Selection, Merge

The document contains C programs for four different sorting algorithms: bubble sort, insertion sort, selection sort, and merge sort. Each program prompts the user to input an array size and elements, sorts the array using the respective algorithm, and then displays the sorted array. The implementations include function definitions for sorting and necessary input/output operations.

Uploaded by

theratimanideep
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// program for bubble sort

#include<stdio.h>
#include<conio.h>
void bubblesort(int [],int);
int a[20];
void main()
{
int i,n;
clrscr();
printf("enter array size ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n before sorting: \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
bubblesort(a,n);
printf("\n after sorting: \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
void bubblesort(int a[], int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
// program for insertion sort
#include<stdio.h>
#include<conio.h>
void insertionsort(int [],int);
int a[20];
void main()
{
int i,n;
clrscr();
printf("enter array size ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n before sorting: \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
insertionsort(a,n);
printf("\n after sorting: \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
void insertionsort(int a[], int n)
{
int i,j,item;
for(j=1;j<n;j++)
{
item=a[j];
i=j-1;
while((i>=0) && (item<a[i]))
{
a[i+1]=a[i];
i=i-1;
}
a[i+1]=item;
}
}
// write program for selection sort
#include<stdio.h>
#include<conio.h>
int a[30],n;
void selection(int[],int);
void main()
{
int i;
clrscr();
printf("enter the array size :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection(a,n);
printf("the sorted order is \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
void selection(int a[],int n)
{
int min,i,j,t;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{
t=a[i];
a[i]=a[min];
a[min]=t;
}
}
}
// merge sort algorithm

#include<stdio.h>
#include<conio.h>
mergesort(int,int);
merge(int,int,int);
int a[20];
void main()
{
int n,i;
printf("enter array size ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
mergesort(int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
merge(int low, int mid, int high)
{
int b[20],h,i,j,k;
h=low;
i=low;
j=mid+1;
while(h<=mid && j<=high)
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++)
a[k]=b[k];
}

You might also like