0% found this document useful (0 votes)
18 views3 pages

SORTING

LANGUAGE C SORTING CODE'S

Uploaded by

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

SORTING

LANGUAGE C SORTING CODE'S

Uploaded by

Avi Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

SORTING

#include<stdio.h>
void bubble(int a[],int n);
void insertion(int a[],int n);
void selection(int a[],int n);

void main()
{
int i,n,s,a[10];
printf("Enter size of array \n");
scanf("%d",&n);
printf("Enter the array \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

printf("Enter 1 for bubble sort.\nEnter 2 for insertion sort.\nEnter 3 for


selection sort.\n");
scanf("%d",&s);

switch(s)
{
case 1:bubble(a,n);
break;

case 2:insertion(a,n);
break;

case 3:selection(a,n);
break;

default:printf("wrong choice");
break;

void bubble(int a[],int n)


{
int i,j,k,temp;
for(i=0;i<n-1;i++)
{
printf("\n For pass %d\n",i+1);
for(j=0;j<n-i-1;j++)
{
printf("\n Row %d:",j+1);
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(k=0;k<n;k++)
{
printf("%d",a[k]);
}
}
}
}
printf("Sorted array");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
}

void insertion(int a[],int n)


{
int i,j,k,key;
for(i=1;i<n;i++)
{
printf("\n For pass %d\n",i);
key=a[i];
j=i-1;
while(j>=0 && a[j]>key)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
for(k=0;k<n;k++)
{
printf("%d",a[k]);
}
}
printf("Sorted array");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
}

void selection(int a[],int n)


{
int i,j,k,temp,min;
for(i=0;i<n-1;i++)
{
printf("\nFor pass%d\n",i+1);
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
for(k=0;k<n;k++)
{
printf("%d",a[k]);
}
}
printf("Sorted array");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}

You might also like