Descending order using Insertion Sort
#include<stdio.h>
void iSS(int sort_arr[]);
void pArr(int sort_arr[]);
int main()
{
int i;
int arr_sort[5]={17,81,35,96,67};
iSS(arr_sort);
pArr(arr_sort);
}
void iSS(int arr_sort[])
{
int i,k,j;
for(i=1;i<5;++i)
{
k=arr_sort[i];
j=i-1;
while(j>=0&&arr_sort[j]>k)
{
arr_sort[j+1]=arr_sort[j];
j=j-1;
}
arr_sort[j+1]=k;
}
}
void pArr(int arr_sort[])
{
int i=1;
for(i=4;i>=0;i--)
{
printf(" ");
for(i=4;i>=0;i--)
{
printf(" ");
printf("%d",arr_sort[i]);
printf(" ");
}
}
}
Output:
96 81 43 35 17
Descending Order Using Merge Sort
#include <stdio.h>
void m_merge(int numeric[ ], int l, int m, int r);
void sort_Merge(int numeric[ ], int l, int r);
int main( )
{
int i;
int numeric[5]={23,66,56,918,11111};
sort_Merge(numeric, 0, 5);
for(i=4;i>=0;i--)
{
printf(" ");
printf("%d", numeric[i]);
printf(" ");
}
return 0;
}
void sort_Merge(int numeeric[], int l, int r)
{
int m;
if( r > l)
{
m = ( r + l ) / 2;
sort_Merge(numeeric,l,m);
sort_Merge(numeeric,(m+1),r);
m_merge(numeeric,l,(m+1),r);
}
}
void m_merge(int numeeric[], int l, int m, int r)
{
int temp[6];
int i, alo, num, pos;
alo = m-1;
pos = l;
num = ((r-l)+1);
while((l<=alo)&&(m<=r))
{
if(numeeric[l] <= numeeric[m])
{
temp[pos++] = numeeric[l++];
}
else
{
temp[pos++]=numeeric[m++];
}
}
while(l<=alo)
{
temp[pos++] = numeeric[l++];
}
while(m <= r)
{
temp[pos++] = numeeric[m++];
}
for( i=0; i<num; i++)
{
numeeric[r] = temp[r];
r--;
}
}
Output:
98 72 56 4 0
#include <stdio.h>
int main ( )
{
int init_arr[ ]={6,41,59, 77, 29, 43, 46, 14, 78, 1, 555};
int n =11, i, j;
printf("\t \t \t ");
printf("Selection sort program");
printf("\n");
printf("Initial array before selection sort is: ");
printf("\n");
printf("\t \t");
for(i=0; i<n; i++ )
{
printf(" ");
printf("%d", init_arr[i]);
printf(" ");
}
int tempo, l;
for(i=0; i<n-1; i++)
{
l = i;
for(j=i+1; j<n; j++)
{
if(init_arr[j] < init_arr[l] )
{
l=j;
}
}
tempo = init_arr[l];
init_arr[l] = init_arr[i];
init_arr[i] = tempo;
}
printf("\n");
printf(" ");
printf("Array after selection sort is: ");
printf("\n");
printf("\t \t");
for(i=n-1; i>=0; i--)
{
printf(" ");
printf("%d", init_arr[i]);
printf(" ");
}
return 0;
}
Output:
Selection sort program
Initial array before selection sort is:
6, 41, 59, 77, 29, 43, 46, 14, 78, 1, 555
Array after selection sort is:
555, 78, 77, 59, 46, 43, 41, 29, 14, 6, 1





Leave a Reply