Merge sort

Program to implement Merge sorting with Time complexity

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
void merge(int b[],int c[],int a[],int p,int q)
{
int i=0,j=0,k=0;
while(i<p&&j<q)
{
if(b[i]<=c[j])
{
a[k]=b[i];
i=i+1;
k=k+1;
}
else
{
a[k]=c[j];
j=j+1;
k=k+1;
}
}

if(i==p)
{
while(j<q)
a[k++]=c[j++];
}
else
{
while(i<p)
a[k++]=b[i++];
}
}
void mergesort(int a[],int n)
{
int b[2000],c[2000],i=0,j=0,k=0;
if(n>1)
{

while(i<n/2)
b[j++]=a[i++];
while(i<n)
c[k++]=a[i++];
mergesort(b,j);
mergesort(c,k);
merge(b,c,a,j,k);
}
}

void main()
{
int n,i,a[2000];
clock_t t1,t2;
clrscr();
printf("Enter the array size\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
//scanf("%d",&a[i]);
a[i]=rand()%1000;
}
printf("\nArray is\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
t1=clock();
for(i=0;i<10000;i++)
mergesort(a,n);
t2=clock();
printf("\nThe sorted array is\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\nTime complexity is %fms",(t2-t1)*1000/(CLK_TCK*10000));
getch();
}



Leap year

Program to check whether the entered year is a Leap year or not..

#include<stdio.h>
#include<conio.h>

int main(void)
{
int year;
printf("Enter the year: ");
scanf("%d",&year);

if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
{
printf("Year %d is a leap year",year);
}
else
{
printf("Year %d is not a leap year",year);
}
return 0;
getch();
}

Simple calculator

Program to perform imlpement simple calculator performing basic operations.

#include<stdio.h>
#include<conio.h>

float add(float,float);
float sub(float,float);
float product(float,float);
float divide(float,float);

void main()
{
float n1,n2;
char sym,choice;
clrscr();
printf("Enter the operation you want to perform(Format eg :- 2+3) \n\n");
scanf("%f%c%f",&n1,&sym,&n2);
if(sym=='+')
printf("\n%f",add(n1,n2));
if(sym=='-')
printf("\n%f",sub(n1,n2));
if(sym=='*')
printf("\n%f",product(n1,n2));
if(sym=='/')
printf("%f",divide(n1,n2));
printf("\nDo you wish to continue[y/n]");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
main();
}

float add(float m1,float m2)
{
return(m1+m2);
}

float sub(float m1,float m2)
{
return(m1-m2);
}

float product(float m1,float m2)
{
return(m1*m2);
}

float divide(float m1,float m2)
{
return(m1/m2);
}

Insertion sort

Program to implement sorting on an entered array using Insertion sort method.


#include<stdio.h>
#include<conio.h>
void ins_sort(int *,int);
main()
{
int n,i,a[5000];
clrscr();
printf("\nEnter n: ");
scanf("%d",&n);
printf("\nEnter the %d array elements ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ins_sort(a,n);
printf("\nSorted array \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
return 0;
}

void ins_sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

Selection sorting

Program to implement selection sort on an array.


#include<stdio.h>
#include<conio.h>
main()
{
int i,n,a[20];
void sel_sort(int *,int);
clrscr();
printf("\nEnter the no of elements : ");
scanf("%d",&n);
printf("\nEnter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sel_sort(a,n);
printf("\nSorted array\n-------------\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
return 0;
}

void sel_sort(int a[],int n)
{
int i,j,p,temp,k;
for(i=0;i<n-1;i++)
{
p=i;
for(j=i+1;j<n;j++)
if(a[p]>a[j])
p=j;

temp=a[p];
a[p]=a[i];
a[i]=temp;
}
}

Linear search

Program to search for a key element in an entered array using Linear search [Recursively]


#include<stdio.h>
#include<conio.h>
int count=0,key,n;
main()
{
void lin_srch(int *,int);
int i,a[10];
clrscr();
printf("\nEnter the no of elements : ");
scanf("%d",&n);
printf("\nEnter the array elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&key);
i=0;
lin_srch(a,i);
if(count==0)
printf("\nElement not found in the array..!");
else
printf("\n%d found in the array!! \nCount = %d",key,count);
getch();
return 0;
}

void lin_srch(int a[],int i)
{
if(i==n-1)
return;
if(a[i]==key)
{
count++;
}
lin_srch(a,i+1);
}

Bubble sort

Program to implement bubble sorting on a string.


#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n,a[10],temp;
clrscr();
printf("\nEnter the no of elements : ");
scanf("%d",&n);
printf("\nEnter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Sorting array.... " );
getch();
for(i=0;i<n-1;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;
}
printf("\nSorted array is \n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
return 0;
}