Professional Documents
Culture Documents
S.NO
week
1
week
2
week
3
i) C programs that use both recursive and non-recursive functions to find the
factorial of a given integer.
ii) C programs that use both recursive and non-recursive functions to find the GCD
of two given integers.
iii) C programs that use both recursive and non-recursive functions to sovle
towers of Hanoi problem.
week
4
A)Program to find the total distance travelled by vehicle in 't' seconds is given by
distance = ut+1/2at2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration
(m/sec2) for
a) different values of 'u' and 'a'.
b) A C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result.
(Consider the operators +,-,*,/,% and use Switch Statement)
week
5
week
6
Page 1
week
7
week
8
week
9
A C program to read in two numbers, x and n, and then compute the sum of this
geometric progression: 1+x+x2+x3+.+xn
For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum Perform error checking. For example, the formula does not make
sense for negative exponents - if n is less than 0. Have your program print an error
message if n<0,then go back and read in the next pair of numbers of without
computing the sum. Are any values of x also illegal ? If so, test for them too.
week
10
week
11
week
12
week
13
week
14
Page 2
week
15
week
16
week
17
week
18
week
19
week
20
ii)
A C program that implements the following sorting methods to sort a given list of
integers in ascending order
i) Bubble Sort
ii) Quick Sort
week
21
A C program that implements the following sorting methods to sort a given list of
integers in ascending order
i) insertion Sort
ii)Merge sort Sort
week
22
week
23
week
24
Page 3
Output:
INPUT:
Enter a positive integer : 1234
RESULT:
Sum of the digits of the given positive integer 1234 is 10
Page 4
Fibonacci sequence
Program to generate Fibbonacci Series
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i,n;
clrscr();
printf("\nEnter how many terms:");
scanf("%d",&n);
a=0;
b=1;
printf("\n%d\n%d",a,b);
i=3;
while(i<=n)
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
i++;
}
getch();
}
OUTPUT
INPUT :
Enter how many terms : 10
RESULT :
0
1
1
2
3
5
8
13
21
34
Page 5
Prime Numbers
OUTPUT
INPUT :
Enter range 1 to 20
RESULT:
2 3 7 11 13 17 19
Page 6
OUTPUT
INPUT :Enter value of x : 2
RESULT :Sum=-0.416155
Page 7
OUTPUT
INPUT :
Enter values of a,b,c:1 2 1
RESULT:
Roots are real and equal
-1.000000 -1.000000
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 8
Page 9
fact *= i;
}
return fact;
}
OUTPUT
INPUT:
Enter the number: 7
OUTPUT :
Factorial of 7 Using Recursive Function is 5040
Factorial of 7 Using Non-Recursive Function is 5040
Page 10
/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
if(n>m)
return GcdRecursive(n,m);
if(n==0)
return m;
else
return GcdRecursive(n,m%n);
}
/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{
unsigned remainder;
remainder = p-(p/q*q);
if(remainder==0)
return q;
else
while(remainder>0)
{
p=q;
q=remainder;
remainder=p%q;
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 11
}
return q;
}
OUTPUT
INPUT :
Enter the two numbers whose GCD is to be found: 15 25
RESULT:
GCD of 15 and 25 Using Recursive Function is 5
GCD of 15 and 25 Using Non-Recursive Function is 5
Towers of Hanoi
Program to solve Towers of Hanoi problem using Non-Recursive and Recursive functions
#include<conio.h>
#include<stdio.h>
/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 12
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}
/* Recursive Function*/
void hanoiRecursion( int num, char ndl1, char ndl2, char ndl3)
{
if ( num == 1 )
{
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
return;
}
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 13
INPUT :
Enter the no. of disks to be transferred: 4
RESULT :
Non-Recursive
Move top disk from needle A to needle B Move top disk from needle A to needle C
Move top disk from needle B to needle C Move top disk from needle A to needle B
Move top disk from needle C to needle A Move top disk from needle C to needle B
Move top disk from needle A to needle B Move top disk from needle A to needle C
Move top disk from needle B to needle C Move top disk from needle B to needle A
Move top disk from needle C to needle A Move top disk from needle B to needle C
Move top disk from needle A to needle B Move top disk from needle A to needle C
Move top disk from needle B to needle C
Recursive
Move top disk from needle A to needle C. Move top disk from needle A to needle B.
Move top disk from needle C to needle B. Move top disk from needle A to needle C.
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 14
Move top disk from needle B to needle A. Move top disk from needle B to needle C.
Move top disk from needle A to needle C. Move top disk from needle A to needle B.
Move top disk from needle C to needle B. Move top disk from needle C to needle A.
Move top disk from needle B to needle A. Move top disk from needle C to needle B.
Move top disk from needle A to needle C. Move top disk from needle A to needle B.
Move top disk from needle C to needle B.
INPUT :
NO OF TIME INTERVALS : 3
AT T1 TIME(sec) : 10
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 15
Page 16
printf("\n%d %c %d = %d",op1,op,op2,res);
getch();
}
OUTPUT
INPUT
Enter operator : +
Enter any two integer values : 30 40
RESULT
30 + 40 = 70
Page 17
OUTPUT
INPUT :
Enter the value of n : 5
Enter 5 values
44
55
66
33
22
RESULT :
Large : 66
Small : 22
Page 18
}
printf("\nEnter values of matrix B9\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
printf("B[%d][%d]=",i,j);
scanf("%d",&B[i][j]);
}
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
C[i][j]=A[i][j]+B[i][j];
printf("\nMatrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",A[i][j]);
printf("\n");
}
printf("\nMatrix B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",B[i][j]);
printf("\n");
}
printf("\nSum of two matrices A and B \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",C[i][j]);
printf("\n");
}
}
else
printf("Addition not possible");
getch();
}
OUTPUT
INPUT:
Enter size of matrix A : 3 3
Enter size of matrix B : 3 3
Enter the values of matrix A 1 2 3 4 5 6 7 8 9
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 19
Page 20
{
printf("A[%d][%d]=",i,j);
scanf("%d",&A[i][j]);
}
printf("\nEnter values of matrix B9\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
printf("B[%d][%d]=",i,j);
scanf("%d",&B[i][j]);
}
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
C[i][j]=A[i][j]+B[i][j];
printf("\nMatrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",A[i][j]);
printf("\n");
}
printf("\nMatrix B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",B[i][j]);
printf("\n");
}
printf("\nSum of two matrices A and B \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",C[i][j]);
printf("\n");
}
}
else
printf("Addition not possible");
getch();
}
OUTPUT
INPUT:
Enter size of matrix A : 2 3
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 21
Page 22
Page 23
OUTPUT
INPUT:
Enter the first string : establish
Enter how many characters and from which position : 3 2
RESULT:
After deletion the string is elish
Page 24
}
}
return matched;
}
int main()
{
char string[40];
clrscr();
printf("****Program to test if the given string is a palindrome****\n");
printf("Enter a string:");
scanf("%s",string);
if(IsPalindrome(string))
printf("The given string %s is a palindrome\n",string);
else
printf("The given string %s is not a palindrome\n",string);
getch();
return 0;
}
OUTPUT
INPUT
Enter a string : MALAYALAM
RESULT
The given string MALAYALAM is a palindrome
Page 25
Page 26
{
/* Reading a line of text */
c = 0;
while((ctr=getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
/* counting the words in a line */
if(line[0] == '\0')
break ;
else
{
words++;
for(i=0; line[i] != '\0';i++)
if(line[i] == ' ' || line[i] == '\t')
words++;
}
/* counting lines and characters */
lines = lines +1;
characters = characters + strlen(line);
}
printf ("\n");
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
}
OUTPUT
INPUT :
KEY IN THE TEXT.
GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETED, PRESS 'RETURN'.
Admiration is a very short-lived passion.
Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not
like those whom we admire.
Fools admire, but men of sense approve.
RESULT :
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 27
Number of lines = 5
Number of words = 36
Number of characters = 205
Pascal's triangle
Pascal Triangle
#include<stdio.h>
#include<conio.h>
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("How many rows you want :");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q<r)
{
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();
}
OUTPUT:
INPUT:
Enter how many rows you want : 5
RESULT :
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 28
Pascal's Triangle:
1
1
1
1
1
1
2
3
4
1
3
1
4
OUTPUT
INPUT :
Enter the number to generate the : 5
RESULT :
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 29
2
3 2
4 3 2
5 4 3 2
To
1
1
1
1
1
0
0
0
0
0
0
1
1
1
1
1
2
2 3
2 3 4
2 3 4 5
read in two numbers, x and n, and then compute the sum of this
geometric progression: 1+x+x2+x3+. . . xn
program to read in two numbers, x and n, and then compute the sum of this geometric
progression: 1+x+x2+x3+. . . xn
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long x,n,i,sum=0,v;
clrscr();
lab:
printf("Enter the values of x and n:");
scanf("%ld%ld",&x,&n);
if(n<0)
{
printf("does not take negative exponents");
goto lab;
}
else
for(i=0;i<=n;i++)
{
v=pow(x,i);
printf("+%ld",v);
sum=sum+v;
}
printf("=%ld\n",sum);
getch();
}
OUTPUT
INPUT :Enter the values of x and n : 2
8
Page 30
RESULT:
1+2+4+8+16+32+64+128+256=511
Page 31
OUTPUT
INPUT:
Enter the binary number1111
RESULT:
The 2's complement is 0001
Page 32
Page 33
OUTPUT
INPUT:
Enter the Roman Numeral : XXVIII
RESULT:
Its Decimal Equivalent is : 28
Page 34
c c1,c2,c3;
clrscr();
printf("Enter first complex ");
scanf("%f%f",&c1.real,&c1.imag);
printf("Enter second complex ");
scanf("%f%f",&c2.real,&c2.imag);
do
{
printf("\n1.Addition");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4.Division");
printf("\n5.exit");
printf("\nEnter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:c3=add(c1,c2);
printf("%.2f+i%.2f",c3.real,c3.imag);
break;
case 2:c3=sub(c1,c2);
printf("%.2f+i%.2f",c3.real,c3.imag);
break;
case 3:c3=mul(c1,c2);
printf("%.2f+i%.2f",c3.real,c3.imag);
break;
case 4:c3=div(c1,c2);
printf("%.2f+i%.2f",c3.real,c3.imag);
break;
case 5:exit(0);
}
}while(ch!=5);
getch();
}
c add(c x,c y)
{
c z;
z.real=x.real+y.real;
z.imag=x.imag+y.imag;
return z;
}
c sub(c x,c y)
{
c z;
z.real=x.real-y.real;
z.imag=x.imag-y.imag;
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 35
return z;
}
c mul(c x,c y)
{
c z;
z.real=(x.real*y.real)-(x.imag*y.imag);
z.imag=(x.real*y.imag)+(x.imag*y.real);
return z;
}
c div(c x,c y)
{
c z;
z.real=((x.real*y.real)+(x.imag*y.imag))/((x.real*x.real)+(x.imag*x.imag));
z.imag=((y.imag*x.real)+(x.real*y.imag))/((x.real*x.real)+(x.imag*x.imag));
return z;
}
OUTPUT
INPUT:
Enter first complex 2.5
Enter second complex 4
6.5
9
RESULT:
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.exit
Enter ur choice :1
Addtion : 6.50+i15.50
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.exit
Enter ur choice :3
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 36
Multiplication : -48.50+i48.50
Binary Search
ALGORITHM :
step 1: start
step 2: read how many elements n
step 3: read n elements
step 4: enter the key element to be searched
step 5: sort the elements
step 6: compare the key element with n elements
step 7:
step 8: write the element is found in postition else not found
step 9: stop
Programs that use both recursive and non recursive functions to perform binary search
operations
#include <stdio.h>
#define MAX_LEN 10
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 37
/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}
/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}
void read_list(int l[],int n)
{
int i;
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 38
scanf("%d",&l[i]);
}
void print_list(int l[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",l[i]);
}
/*main function*/
void main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;
clrscr();
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);
if(ch<=2 & ch>0)
{
printf("\nEnter the number of elements : ");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);
switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
printf("Element is not found");
}
else
{
printf("Element is found at %d position",pos);
}
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 39
getch();
break;
case 2:printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();
}
OUTPUT
INPUT :
==================================
MENU
==================================
[1] Binary Search using Recursion method
[2] Binary Search using Non-Recursion method
Enter your Choice:1
Enter the number of elements : 5
Enter the elements: 45 50 68 74 85
RESULT :
Elements present in the list are:
45
50
68
74
85
Enter the element you want to search : 68
Recursive method:
Element is found at 3 position
INPUT :
======================================================
MENU
=====================================================
[1] Binary Search using Recursion method
[2] Binary Search using Non-Recursion method
Enter your Choice:2
Enter the number of elements : 5
Enter the elements:
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 40
25 65 87 98 153
RESULT :
Elements present in the list are: 25
65
87
98
153
Bubble Sort
Definition :
Bubble Sort is an elementary sorting algorithm. It works by repeatedly exchanging adjacent
elements, if necessary. When no exchanges are required, the data is sorted.
ALGORITHM :
Procedure BubbleSort(A, MAX) : Here A is an array consisting of MAX elements. This
procedure sorts the elements in Ascending Order by repeatedly exchanging adjacent elements, if
necessary. Here 'temp' is a temporary variable used to exchange the adjacent elements in this
procedure.
ASCENDING ORDER
for i 1 to MAX do
for j MAX downto i+1 do
If A[j] < A[j-1] then
Exchange A[j] A[j-1]
Page 41
}
// Function for Bubble Sort
void bub_sort(int list[], int n)
{
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j<(n-(i+1));j++)
if(list[j] > list[j+1])
swapList(&list[j],&list[j+1]);
}
void readlist(int list[],int n)
{
int j;
printf("\nEnter the elements: \n");
for(j=0;j<n;j++)
scanf("%d",&list[j]);
}// Showing the contents of the list
void printlist(int list[],int n)
{
int j;
for(j=0;j<n;j++)
printf("%d\t",list[j]);
}
void main()
{
int list[MAX], num;
printf(\nBUBBLE SORT);
printf("\n\n\nEnter the number of elements [Maximum 10]\n");
scanf("%d",&num);
readlist(list,num);
printf("\n\nElements in the list before sorting are:\n");
printlist(list,num);
bub_sort(list,num);
printf("\n\nElements in the list after sorting are:\n");
printlist(list,num);
}
OUTPUT:
******BUBBLE SORT *****
INPUT :
Enter the number of elements [Maximum 10] : 6
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 42
QUICK SORT
Definition :
Quick Sort works on the principle of divide-and-conquer. It is an algorithm of choice in many
situations because it is not difficult to implement, it is a good "general purpose" sort and it
consumes relatively fewer resources during execution.
PARTITION (A, p, r)
x = A[p]
i = p-1
j = r+1
while TRUE do
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 43
Repeat j = j-1
until A[j] x
Repeat i = i+1
until A[i] x
if i < j
then exchange A[i] = A[j]
else return j
Quick sort
#include <stdio.h>
#define MAX 10
void swap(int *m,int *n)
{
int temp;
temp = *m;
*m = *n;
*n = temp;
}
int get_key_position(int x,int y )
{
return((x+y) /2);
}
// Function for Quick Sort
void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if( m < n)
{
k = get_key_position(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
quicksort(list,m,j-1);
quicksort(list,j+1,n);
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 44
}
}
// Function to read the data
void read_data(int list[],int n)
{
int j;
printf("\n\nEnter the elements:\n");
for(j=0;j<n;j++)
scanf("%d",&list[j]);
}
// Function to print the data
void print_data(int list[],int n)
{
int j;
for(j=0;j<n;j++)
printf("%d\t",list[j]);
}
void main()
{
int list[MAX], num;
clrscr();
printf("\n***** Enter the number of elements Maximum [10] *****\n");
scanf("%d",&num);
read_data(list,num);
printf("\n\nElements in the list before sorting are:\n");
print_data(list,num);
quicksort(list,0,num-1);
printf("\n\nElements in the list after sorting are:\n");
print_data(list,num);
getch();
}
OUTPUT:
******QUICK SORT *****
INPUT :
Enter the number of elements [Maximum 10] : 10
Enter the elements:
15 4 11 6 28 25 9 35 45 3
RESULT :
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 45
3
45
Insertion Sort
Definition :
Insertion Sort is an example of an incremental algorithm. It builds the sorted sequence one
number at a time.
If the first few objects are already sorted, an unsorted object can be inserted in the sorted set in
proper place. This is called insertion sort. An algorithm consider the elements one at a time,
inserting each in its suitable place among those already considered (keeping them sorted).
ALGORITHM :
Procedure InsertionSort(A, MAX) : Here A is an array consisting of MAX elements. This
procedure sorts the elements in Ascending Order.
ASCENDING ORDER
For j = 2 to length [A] do
key = A[j]
Put A[j] into the sorted sequence A[1 . . j-1]
i = j-1
while i > 0 and A[i] > key do
A[i+1] = A[i]
i = i-1
A[i+1] = key
Program to implement Insertion sort
#include<stdio.h>
#include<conio.h>
void inst_sort(int []);
void main()
{
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 46
int num[5],count;
clrscr();
printf(\n******INSERTION SORT******);
printf("\nEnter the Five Elements to sort:\n");
for (count=0;count<5;count++)
scanf("%d",&num[count]);
inst_sort(num);
printf("\n\nElements after sorting: \n");
for(count=0;count<5;count++)
printf("%d\n",num[count]);
getch();
}
// Function for Insertion Sorting
void inst_sort(int num[])
{
int i,j,k;
for(j=1;j<5;j++)
{
k=num[j];
for(i=j-1;i>=0 && k<num[i];i--)
num[i+1]=num[i];
num[i+1]=k;
}}
OUTPUT:
*****INSERTION SORT********
INPUT :
Enter the Five Elements to sort:
5
66
22
44
55
RESULT :
Elements after sorting:
5
22
44
55
66
Merge Sort
Definition :
Merge-sort is based on the divide-and-conquer paradigm. The Merge-sort algorithm can be
described in general terms as consisting of the following three steps:
1. Divide Step
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 47
If given array A has zero or one element, return S; it is already sorted. Otherwise, divide A into two
arrays, A1 and A2, each containing about half of the elements of A.
2. Recursion Step
Recursively sort array A1 and A2.
3. Conquer Step
Combine the elements back in A by merging the sorted arrays A1 and A2 into a sorted sequence.
ALGORITHM :
To begin, suppose that we have two sorted arrays A1[1], A1[2], . . , A1[M] and A2[1], A2[2], . . . ,
A2[N]. The following is a direct algorithm of the obvious strategy of successively choosing the
smallest remaining elements from A1 to A2 and putting it in A
MERGE_SORT (A)
A1[1 . . floor(n/2)] = A[1 . . floor(n/2)]
A2[1 . . floor(n/2)] = A[1 + floor(n/2) . . n]
Merge Sort (A1)
Merge Sort (A1)
Merge Sort (A1, A2, A)
Page 48
int ary[MAX_ARY];
int j = 0;
printf("\n\nEnter the elements to be sorted: \n");
for(j=0;j<MAX_ARY;j++)
scanf("%d",&ary[j]);
/* array before mergesort */
printf("Before :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);
printf("\n");
merge_sort(ary, 0, MAX_ARY - 1);
/* array after mergesort */
printf("After Merge Sort :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);
printf("\n");
getch();
}
/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[MAX_ARY];
if(end == start)
return;
mid = (end + start) / 2;
merge_sort(x, end, mid);
merge_sort(x, mid + 1, start);
for(j = 0; j < size; j++)
executing[j] = x[end + j];
mrg1 = 0;
mrg2 = mid - end + 1;
for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
else
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
}
JOGINPALLY B.R.ENGINEERING COLLEGE
Page 49
}
OUTPUT:
*******MERGE SORT**********
Enter 10 elements to be sorted:
55
66
22
33
1
44
77
58
25
35
Before : 55 66 22 33 1 44 77 58 25 35
After Merge Sort : 1 22 25 33 35 44 55 58 66 77
Page 50