Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programs ds.1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Ex : Recursion method

#include<stdio.h>#include<conio.h>int Fibonacci(int);voidmain()

int i,n;clrscr();

printf("\nEnterthenoofelementstobedisplayed:");scanf("%d",&n);

for(i=0;i<n;i++)printf("%d\t",Fibonacci(i));getch();

intFibonacci(intn)

if(n<=0)return0;

else if (n==1)return1;

else

returnFibonacci(n-1)+Fibonacci(n-2);

4. Aim: Implementationofcircularqueueusingarray.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAXQ 100int front=-1,rear=-1;int items[MAXQ];int Isempty();

intIsfull();void Insert(int);int Delete();void Display();voidmain()

int x;

char ch='1';clrscr();while(ch!='4')
{

printf("\n 1-INSERT");printf("\n 2-DELETE");printf("\n3-DISPLAY");printf("\n4-QUIT");

printf("\n Enter your choice:");fflush(stdin);

ch=getchar();switch(ch)

case'1':

printf("\nEnterthenosofelement tobeinserted:");scanf("%d",&x);

Insert(x);break;case'2':

x=Delete();

printf("\n Deleted element is %d\n:",x);break;

case'3':

Display();break;case'4':

break;default:

printf("\nWrongchoice!Tryagain:");

getch();

intIsempty()

if(front==-1)return1;else

return0;

intIsfull()
{

if(front==(rear+1)%MAXQ)return1;

elsereturn0;

voidInsert(intx)

if(Isfull())

printf("\n Queue full");return;

if(front==-1)

front=0;rear=0;

elserear=(rear+1)%MAXQ;items[rear]=x;

intDelete()

int x;if(Isempty())

printf("\n Queue is empty");exit(0);

x=items[front];if(front==rear)
{

front=-1;rear=-1;

elsefront=(front+1)%MAXQ;returnx;

voidDisplay()

int i,n;if(Isempty())

printf("\n Queue is empty");return;

printf("\n Elements in the Queue are :\n");if(front<=rear)

for(i=front;i<=rear;i++)printf("%d\n",items[i]);

else

for(i=front;i<=MAXQ-1;i++)printf("%d\n",items[i]);for(i=0;i<=rear;i++)printf("%d\n",items[i]);

}
OUTPUT
5. Aim : Implementation of binary search tree using array.

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

#defineTREENODES100

#define FALSE 0structtree

int info;intused;

};

structtreenode[TREENODES];voidCreatetree();

void Insert(int);voidDisplay();voidSetleft(int,int);

void Setright(int,int);voidmain()

int x;

char ch='1';clrscr();

printf("\nEnterrootnodevalue:");scanf("%d",&x);

Createtree(x);while(ch!='3')

printf("\n1-INSERT");printf("\n2-DISPLAY");printf("\n3-QUIT");

printf("\n Enter your choice:");fflush(stdin);

ch=getchar();switch(ch)

case '1':

printf("\n Enter the element to be inserted:");scanf("%d",&x);

Insert(x);break;case '2':Display();break;
case'3':

break;default:

printf("\nWrongchoice!Tryagain:");

voidCreatetree(intx)

int i;node[0].info=x;

node[0].used=TRUE;for(i=1;i<TREENODES;i++)

node[i].used=FALSE;

voidInsert(intx)

int p,q;p=q=0;

while(q<TREENODES&&node[q].used&&x!=node[p].info)

p=q;if(x<node[p].info)q=2*p+1;

elseq=2*p+2;

if(x==node[p].info)

printf("\n %d is a duplicate number\n",x);else

if(x<node[p].info)Setleft(p,x);
elseSetright(p,x);

voidSetleft(intpos,int x)

int q;q=2*pos+1;

if(q>TREENODES)

printf("\n Array overflow.");elseif(node[q].used==TRUE)printf("\nInvalidinsertion.");else

node[q].info=x;

node[q].used=TRUE;

voidSetright(intpos,intx)

int q;q=2*pos+2;

if(q>TREENODES)

printf("\n Array overflow.");elseif(node[q].used==TRUE)

printf("\n Invalid insertion.\n");else

node[q].info=x;node[q].used=TRUE;

voidDisplay()
{

int i;for(i=0;i<TREENODES;i++)

if(node[i].used==TRUE)printf("%d ",node[i].info);printf("\n");

output

6. REPRESENTATION OF GRAPH

Aim To represent graph using adjacency list.

Algorithm
• For a graph with |V| vertices, an adjacency matrix is a |V|×∣V∣ matrix of 0s and 1s, where
the entry in row i and column j is 1 if and only if the edge (i,j) in the graph.

1. Consider the graph to be represented

2. Start from an edge

3. If the edge connects the vertices i,j the mark the ith row and jth column of the adjacency
matrix as 1 otherwise store 0

4. Finally print the adjacency matrix

Program

#include<stdio.h>

#define V 5

void init(int arr[][V])

int i,j;

for(i = 0; i < V; i++)

for(j = 0; j < V; j++)

arr[i][j] = 0;

void addEdge(int arr[][V],int src, int dest)

arr[src][dest] = 1;

void printAdjMatrix(int arr[][V])

int i, j;

for(i = 0; i < V; i++)

{
for(j = 0; j < V; j++)

printf("%d ", arr[i][j]);

printf("\n");

int main()

int adjMatrix[V][V];

init(adjMatrix); addEdge(adjMatrix,0,1); addEdge(adjMatrix,0,2); addEdge(adjMatrix,0,3);


addEdge(adjMatrix,1,3); addEdge(adjMatrix,1,4); addEdge(adjMatrix,2,3);
addEdge(adjMatrix,3,4);

printAdjMatrix(adjMatrix);

return 0;

OUTPUT

01110

00011

00010

00001

00000

7. A) Quick Sort

QUICK SORT
Aim:To sort an array of N numbers using Quick sort.

Algorithm

1. Start

2. Read number of array elements n

3. Read array elements Ai

4. Select an pivot element x from Ai

5. Divide the array into 3 sequences: elements < x, x, elements > x

6. Recursively quick sort both sets (Ai < x and Ai > x)

7. Display the sorted array elements

8. Stop

Program

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

void qsort(int arr[20], int fst, int last); main()

int arr[30]; int i, size;

printf("Enter total no. of the elements : "); scanf("%d", &size);

printf("Enter total %d elements : \n", size); for(i=0; i<size; i++)

scanf("%d", &arr[i]); qsort(arr,0,size-1);

printf("\n Quick sorted elements \n"); for(i=0; i<size; i++)

printf("%d\t", arr[i]); getch();

void qsort(int arr[20], int fst, int last)

int i, j, pivot, tmp; if(fst < last)

{
pivot = fst; i = fst;

j = last; while(i < j)

while(arr[i] <=arr[pivot] && i<last)

i++;

while(arr[j] > arr[pivot]) j--;

if(i <j )

tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;

tmp = arr[pivot]; arr[pivot] = arr[j]; arr[j] = tmp; qsort(arr, fst, j-1); qsort(arr, j+1, last);

Output

Enter total no. of the elements : 8 Enter total 8 elements :

127

-1

04

-2

Quick sorted elements


-2 -1 0 1 2 3 4 7

Result

Thus the array was sorted using quick sort‟s divide and conquers method.

b) Selection Sort

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

voidSelectionsort(int[],int);voidmain()

int x[20],i,n;clrscr();

printf("\n Enter the no of element to be sorted:");scanf("%d",&n);

printf("\n Enter %d elements:",n);for(i=0;i<n;i++)scanf("%d",&x[i]);Selectionsort(x,n);

printf("\n The sorted array is:\n");for(i=0;i<n;i++)printf("%4d",x[i]);

getch();

voidSelectionsort(inta[],intn)

int i,j,pos,large;for(i=n-1;i>0;i--)

large=a[0];pos=0;for(j=1;j<=i;j++)

if(a[i]>large)

large=a[j];pos=j;

a[pos]=a[i];a[i]=large;
}

Output:

8. A. Sequential Search

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

intSequentialsearch(int[],int,int);voidmain()

intx[20],i,n,p,key;clrscr();

printf("\n Enter the no of element:");scanf("%d",&n);

printf("\n Enter %d elements:",n);for(i=0;i<n;i++)scanf("%d",&x[i]);


printf("\n Enter the element to be
search:");scanf("%d",&key);p=Sequentialsearch(x,n,key);

if(p==-1)

printf("\n The searchis unsuccessful:\n");else

printf("\n%d is found at location %d",key,p);getch();

intSequentialsearch(inta[],intn,intk)

int i;for(i=0;i<n;i++)

if(k==a[i])return(i);

return(-1);

Output
8.B. Binary Search

BINARY SEARCH

Aim

To locate an element in a sorted array using Binary search method

Algorithm

1. Start

2. Read number of array elements, say n

3. Create an array arr consisting n sorted elements

4. Get element, say key to be located

5. Assign 0 to lower and n to upper

6. While (lower < upper)

a. Determine middle element mid = (upper+lower)/2

b. If key = arr[mid] then


i. Print mid

ii. Stop

c. Else if key > arr[mid] then

i. lower = mid + 1

d. else

i. upper = mid – 1

7. Print "Element not found"

8. Stop

Program

#include <stdio.h>

void main()

int a[50],i, n, upper, lower, mid, val, found, att=0; printf("Enter array size : ");

scanf("%d", &n); for(i=0; i<n; i++) a[i] = 2 * i;

printf("\n Elements in Sorted Order \n"); for(i=0; i<n; i++)

printf("%4d", a[i]);

printf("\n Enter element to locate : "); scanf("%d", &val);

upper = n; lower = 0;

found = -1;

while (lower <= upper)

mid = (upper + lower)/2; att++;

if (a[mid] == val)

printf("Found at index %d in %d attempts", mid, att); found = 1;


break;

else if(a[mid] > val) upper = mid - 1; else

lower = mid + 1;

if (found == -1) printf("Element not found");

Output

Enter array size : 10 Elements in Sorted Order 0 2 4 6 8 10 12 14 16 18

Enter element to locate : 16 Found at index 8 in 2 attempts

Result

Thus an element is located quickly using binary search method.

9. A) Heap Sort

#include<stdio.h>

#include<conio.h>

void Heapsort(int[],int);int Parent(int);

int Left(int);intRight(int);

void Heapify(int[],int,int);void Buildheap(int[],int);voidmain()

int x[20],i,n;clrscr();

printf("\n Enter the no of element to be sorted:");scanf("%d",&n);

printf("\nEnter%delements:",n);
for(i=0;i<n;i++)scanf("%d",&x[i]);Heapsort(x,n);

printf("\n The sorted array is:\n");for(i=0;i<n;i++)printf("%4d",x[i]);

getch();

intParent(inti)

return(i/2);

int Left(inti)

return(2*i+1);

intRight(inti)

return(2*i+2);

voidHeapify(inta[],inti,intn)

int l,r,large,temp ;l=Left(i);r=Right(i);

if((l<=n-1)&&(a[l]>a[i]))large=l;

elselarge=i;

if((r<=n-1)&&(a[r]>a[large]))large=r;

if(large!=i)
{

temp=a[i];a[i]=a[large];a[large]=temp;Heapify(a,large,n);

voidBuildheap(inta[],intn)

inti;

for(i=(n-1)/2;i>=0;i--)Heapify(a,i,n);

voidHeapsort(inta[],intn)

int i,m,temp;Buildheap(a,n);m=n;

for(i=n-1;i>=1;i--)

temp=a[0];a[0]=a[i];a[i]=temp;m=m-1;

Heapify(a,0,m);

Output
9. B. Radix sort

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

voidRadixsort(int[],int);voidmain()

int x[20],i,n;clrscr();

printf("\n Enter the no of element to be sorted:");scanf("%d",&n);

printf("\n Enter %d elements:",n);for(i=0;i<n;i++)scanf("%d",&x[i]);Radixsort(x,n);

printf("\n The sorted array is:\n");for(i=0;i<n;i++)printf("%4d",x[i]);

getch();

voidRadixsort(inta[],intn)

{
int bucket[10][10],buck[10];int i,j,k,l,num,div,large,pass;div=1;

num=0;large=a[0];for(i=0;i<n;i++)

if(a[i]>large)large=a[i];

while(large>0)

num=num+1;large=large/10;

for(pass=0;pass<num;pass++)

for(k=0;k<10;k++)buck[k]=0;for(i=0;i<n;i++)

l=(a[i]/div)%10;bucket[l][buck[l]++]=a[i];

}i=0;

for(k=0;k<10;k++)

for(j=0;j<buck[k];j++)a[i++]=bucket[k][j];

div=div*10;

Output:

You might also like