1.
Selection Sort.
#include<stdio.h>
int main()
{
int i,j,n,min,temp,arr[100];
printf("enter size of array: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("enter the element%d : ",i+1);
scanf("%d",&arr[i]);
}
for(i=0; i<n-1; i++)
{
min=i;
for(j=i+1; j<n; j++)
{
if(arr[j]<arr[min])
min=j;
}
if(min!=i)
{
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
printf("sorted array :");
for(i=0; i<n; i++)
{
printf("%d",arr[i]);
}
return 0;
}
OUTPUT. :
enter size of array : 7
enter the element1 : 1
enter the element2 : 0
enter the element3 : 5
enter the element4 : 15
enter the element5 : 82
enter the element6 : 7
enter the element7 : 3
sorted array :0 1 3 5 7 15 82
///////////////////////////////////////////////////////////
BUBBLE SORT:
PROGRAM :
include<stdio.h>
int main()
{
int i,j,n,temp,arr[100];
printf("enter size of array: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("enter the element%d : ",i+1);
scanf("%d",&arr[i]);
}
for(i=0; i<n-1; i++)
{
for(j=0; j<n-1; j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("sorted array :");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
return 0;
}
OUTPUT :
enter size of array: 7
enter the element1 : 8
enter the element2 : 47
enter the element3 : 3
enter the element4 : 2
enter the element5 : 9
enter the element6 : 1
enter the element7 : 5
sorted array :1 2 3 5 8 9 47
///////////////////////////////////////
Insertion sort.
"insert an element from unsorted array to its correct position in sorted array"
Program.
#include<stdio.h>
int main()
{
int i,j,n,temp,arr[100];
printf("enter size of array: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("enter the element%d : ",i+1);
scanf("%d",&arr[i]);
}
for(i=1; i<n; i++)
{
temp=arr[i] ;
j=i-1;
while(j>=0 && arr[j]>temp )
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
printf("sorted array :");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
return 0;
}
OUTPUT:
enter size of array: 7
enter the element1 : 4
enter the element2 : 9
enter the element3 : 6
enter the element4 : 1
enter the element5 : 3
enter the element6 : 8
enter the element7 : 0
sorted array :0 1 3 4 6 8 9
Comments
Post a Comment