Vous êtes sur la page 1sur 29

AIM:

To implement Stack using arrays.

OBJECTIVE:

To represent Stack using arrays and to perform operations like push, pop.

HARDWARE REQUIREMENTS:

PC (Pentium IV)

SOFTWARE REQUIREMENTS:

Turbo C Compiler

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define stack_size 5
void push (int value);
void pop();
void peek();
int size();
void view();
int stack[stack_size], top=-1;
void main()
{
int x, data, item;
clrscr();
printf("\n Representation of Stack:");
printf("\n1. Push, 2. Pop, 3. Peek, 4. Size, 5.View, 6. Exit");
while(1)
{
printf("\n Enter the choice:");
scanf("%d", &x);
switch(x)
{
case 1:
printf("\nEnter the element:");
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
printf("\n stack size=%d", stack_size);
printf("\n Current stack size=%d", size());
break;
case 5:
view();
break;
default:
printf("\n End of the program");
exit(0);
}
}
getch();
}
int isfull()
{
extern int stack[], top;
if(top==stack_size-1)
return(1);
else
return(0);
}
int isempty()
{
extern int stack[], top;
if(top==-1)
return(1);
else
return(0);
}
void push(int value)
{
extern int stack[], top;
if(isfull())
printf("\n Stack is full");
else
{
top++;
stack[top]=value;
}
}
void pop()
{
int value;
extern int stack[], top;
if(isempty())
printf("\n Stack is empty");
else
{
value=stack[top];
printf("\n The popped value is %d", value);
top--;
}
}
void peek()
{
int item;
extern int stack[], top;
if(isempty(1))
printf("\n Stack is empty");
else
{
item=stack[top];
printf("\n The peek of the stack is %d", item);
}
}
int size()
{
extern int stack[], top;
if(isempty())
return(0);
else
return(top+1);
}
void view()
{
extern int stack[], top;
int f;
if(isempty())
printf("\n Stack is empty");
else
{
printf("\n Content of the stack is .... \n top-->");
for(f=top;f>=0;f--)
{
printf("%d-->", stack[f]);
}
}
if(isfull())
printf("\n Stack is full");
}
OUTPUT:

Representation of Stack:
1. Push, 2. Pop, 3. Peek, 4. Size, 5. View, 6. Exit

Enter the choice: 2


Stack is empty

Enter the choice: 1


Enter the element: 10

Enter the choice: 1


Enter the element: 20

Enter the choice: 1


Enter the element: 30

Enter the choice: 1


Enter the element: 40
Enter the choice: 1
Enter the element: 50

Enter the choice: 1


Enter the element: 60

Stack is full.
Enter the choice: 3
Peek of the stack is: 50

Enter the choice: 4


Stack size is 5
Current stack size 5

Enter the choice: 5


Content of the stack is…..
Top-->50-->40-->30-->20-->10-->
Stack is full.

Enter the choice: 2


The popped value is 50.

Enter the choice: 6

RESULT:
The implementation of stack using array program is executed and the output
is verified.

IMPLEMENTATION OF QUEUE USING ARRAYS

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define qsize 5
void enqueue(int value);
void dequeue();
void peek();
int size();
void view();
int queue[qsize], front=-1, rear=-1;
void main()
{
int choice, data, item;
clrscr();
printf("\n Representation of Linear Queue:");
printf("\n 1. Enqueue, 2. Dequeue, 3. Peek, 4. Size, 5. View, 6. Exit");
while(1)
{
printf("\n Enter the choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n Enter the element:");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
printf("\n queue size = %d", qsize);
printf("\n Current queue size = %d", size());
break;
case 5:
view();
break;
default:
printf("\n End of the programme");
exit(0);
getch();
}
}
}
//getch();
int isfull()
{
extern int queue[], front, rear;
if(rear==(qsize-1))
return(1);
else
return(0);
}
int isempty()
{
extern int queue[], front, rear;
if(front==-1&&rear==-1)
return(1);
else
return(0);
}
void enqueue(int value)
{
extern int queue[], front, rear;
if(isfull())
printf("\n queue is full");
else
{
if(isempty())
front=rear=0;
else
rear=rear+1;
queue[rear]=value;
}
}
void dequeue()
{
int value;
extern int queue[], front, rear;
if (isempty())
printf("\n queue is empty");
else
{
value=queue[front];
printf("\n The dequeued value is %d", value);
}
if(front==rear)
front=rear=-1;
else
front=front+1;
}
void peek()
{
int item;
extern int queue[], front, rear;
if(isempty())
printf("\n queue is empty");
else
{
item=queue[front];
printf("\n The peek of the queue is %d", item);
}
}
int size()
{
extern int queue[], front, rear;
if(isempty())
return(0);
else
return(rear-front+1);
}
void view()
{
extern int queue[], front, rear;
int f;
if(isempty())
{
printf("\n queue is empty");
}
else
{
printf("\n content of the queue is .......\n Front-->");
for(f=front;f!=rear+1;f=f+1)
{
printf("%d-->", queue[f]);
}
printf("Rear");
}
if(isfull())
printf("\n queue is full");

OUTPUT:

Representation of Linear Queue:


1. Enqueue, 2. Dequeue, 3. Peek, 4. Size, 5. View, 6. Exit

Enter the choice: 2


Queue is empty

Enter the choice: 1


Enter the element: 10

Enter the choice: 1


Enter the element: 20

Enter the choice: 1


Enter the element: 30

Enter the choice: 1


Enter the element: 40

Enter the choice: 1


Enter the element: 50

Enter the choice: 1


Enter the element: 60

Queue is full.
Enter the choice: 3
Peek of the Queue is: 50

Enter the choice: 4


Queue size is 5
Current Queue size 5

Enter the choice: 5


Content of the Queue is…..
Front-->50-->40-->30-->20-->10-->Rear
Queue is full.

Enter the choice: 2


The dequeued value is 50.

Enter the choice: 6


IMPLEMENTATION OF STACK USING LINKED LIST

PROGRAM:

#include<stdio.h>
#include<conio.h>
typedef struct stack
{
int data;
struct stack *next;
}node;
void push(int value);
void pop();
void display();
void peek();
void size();
int isempty();
node *get_node();
node *top;

void main()
{
int x,data,value;
top=NULL;
clrscr();
printf("\n Representation of stack using Linked List:");
printf("\n 1.push, 2.pop, 3.peek, 4.size, 5.view, 6.exit");
while(1)
{
printf("\n Enter your choice:");
scanf("%d", &x);
switch(x)
{
case 1:
printf("\n Enter the element to push:");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
size();
break;
case 5:
display();
break;
default:
printf("\n End of the program");
exit(0);
getch();
}
}
}

void push(int value)


{
extern node *top;
node *New;
New=get_node();
if(New==NULL)
{
printf("\n Memory not created");
return;
}
New->data=value;
New->next=top;
top=New;
}

node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return(temp);
}

void pop()
{
extern node *top;
node *temp;
temp=top;
if(isempty())
printf("\n The stack underflow on pop");
else
{
printf("\n The popped value is %d", top->data);
top=top->next;
temp->next=NULL;
free(temp);
}
}

void peek()
{
extern node *top;
if(isempty())
printf("\n The stack is empty");
else
printf("\n The top most element is=%d", top->data);
}

void size()
{
extern node *top;
node *temp;
int count=0;
temp=top;
if(isempty())
printf("\n The stack is empty");
else
{
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("\n The current size is=%d", count);
}
}

void display()
{
extern node *top;
int i;
node *temp;
temp=top;
if(isempty())
printf("\n The stack is empty");
else
{
printf("\n The stack is:\nTop");
while(temp!=NULL)
{
printf("-->%d", temp->data);
temp=temp->next;
}
}
}

int isempty()
{
extern node *top;
if(top==NULL)
return 1;
else
return 0;
}

OUTPUT:

Representation of stack using linked list:


1.push, 2. pop, 3. peek, 4. size, 5. view, 6. exit
Enter the choice: 1
Enter the element to push:10

Enter the choice: 1


Enter the element to push:20

Enter the choice: 1


Enter the element to push:30

Enter the choice: 2


The popped value is 30

Enter the choice: 1


Enter the element to push:35

Enter the choice: 3


The top most element is=35

Enter the choice: 4


The current size is=4

Enter the choice: 5


The stack is:
Top-->35-->20-->10

Enter the choice: 2


The popped value is 35

Enter the choice: 2


The popped value is 20

Enter the choice: 2


The popped value is 10

Enter the choice: 2


The stack underflow on pop

Enter the choice: 6

IMPLEMENTATION OF QUEUE USING LINKED LIST

PROGRAM:

#include<stdio.h>
#include<conio.h>
typedef struct queue
{
int data;
struct queue *next;
}node;
void enqueue();
void dequeue();
void display();
void size();
void peek();
node *get_node();
node *front,*rear;
void main()
{
int ch,data;
front=NULL;
rear=NULL;
clrscr();
printf("Linear Queue implementation using Linked List:");
printf("\n1.ENQUEUE\n2.DEQUEUE\n3.VIEW\n4.SIZE\n5.PEEK\n6.EXIT");
while(1)
{
printf("\nEnter your choice...");
scanf("%d",&ch);
switch(ch)
{
case 1:enqueue();
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:size();
break;
case 5:peek();
break;
default:printf("\nEnd of program");
exit(0);
}
getch();
}
}
void enqueue()
{
extern node *front,*rear;
node *New;
int val;
New=get_node();
if(New==NULL)
{
printf("\nMemory not created");
return;
}
printf("\nEnter the element to Enqueue:");
scanf("%d",&val);
New->data=val;
New->next=NULL;
if(rear==NULL)
{
rear=New;
front=New;
}
else
{
rear->next=New;
rear=New;
}
}
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return(temp);
}
void dequeue()
{
extern node *front,*rear;
node *temp;
int value;
temp=front;
if(isempty())
printf("\nThe QUEUE is empty");
else
{
value=front->data;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
temp->next=NULL;
free(temp);
}
printf("\nThe Dequeued Elelement of the QUEUE is =%d",value);
}
}
void size()
{
extern node *front,*rear;
int count=0;
node *temp;
temp=front;
if(isempty())
printf("\nThe QUEUE is empty");
else
{
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("\nThe size of the QUEUE is: %d",count);
}
}
void peek()
{
extern node *rear,*front;
if(isempty())
printf("\nThe QUEUE is empty");
else
printf("\nThe Front element of the QUEUE is =%d",front->data);
}
void display()
{
extern node *front,*rear;
int i;
node *temp;
temp=front;
if(isempty())
printf("\nThe QUEUE is empty");
else
{
printf("\nFRONT-->");
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("REAR");
}
}
int isempty()
{
extern node *front,*rear;
if(front==NULL)
return 1;
else
return 0;
}

OUTPUT:

Linear Queue implementation using Linked List:


1.ENQUEUE
2.DEQUEUE
3.VIEW
4.SIZE
5.PEEK
6.EXIT
Enter your choice...1
Enter the element to Enqueue:25

Enter your choice...1


Enter the element to Enqueue:30

Enter your choice...1


Enter the element to Enqueue:35

Enter your choice...1


Enter the element to Enqueue:40
Enter your choice...3
FRONT-->25-->30-->35-->40-->REAR

Enter your choice...4


The size of the QUEUE is: 4

Enter your choice...5


The Front element of the QUEUE is =25

Enter your choice...2


The Dequeued Elelement of the QUEUE is =25

Enter your choice...4


The size of the QUEUE is: 3

Enter your choice...3


FRONT-->30-->35-->40-->REAR

Enter your choice...2


The Dequeued Elelement of the QUEUE is =30

Enter your choice...2


The Dequeued Elelement of the QUEUE is =35

Enter your choice...2


The Dequeued Elelement of the QUEUE is =40

Enter your choice...2


The QUEUE is empty

Enter your choice...4


The QUEUE is empty

Enter your choice...6


End of program

IMPLEMENTATION OF SINGLY LINEAR LINKED LIST

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
typedef struct sll
{
int data;
struct sll *next;
}node;
node *create(node *);
node *insert(node *);
node *delet(node **);
node *search(node *,int);
void count(node *);
void display(node *);
node *get_node();

void main()
{
int choice,val;
node *head;
clrscr();
printf("\nRepresentation of singly linked list:");
printf("\n1.create\n2.insert\n3.delete\n4.search\n5.display\n6.count\n7.exit");
while(1)
{
printf("\n\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: head=create(head);
break;
case 2: head=insert(head);
break;
case 3: head=delet(&head);
break;
case 4: printf("Enter the element to search");
scanf("%d",&val);
search(head,val);
break;
case 5: display(head);
break;
case 6: count(head);
break;
default:printf("\nEnd of program");
exit(0);
}
}
}

node *create(node *head)


{
node *temp,*New;
int val,flag;
char ans;
temp=NULL;
flag=TRUE;
do
{
printf("\nEnter the element: ");
scanf("%d",&val);
New=get_node();
if(New==NULL)
printf("\nMemory is not allocated");
New->data=val;
if(flag)
{
head=New;
temp=head;
flag=FALSE;
}
else
{
temp->next=New;
temp=New;
}
printf("\nDo u want to continue:(y/n)");
ans=getche();
}while((ans=='Y')||(ans=='y'));
printf("\n The singly linked list is created");
return(head);
}

node *insert(node *head)


{
node *temp,*New;
int val,ch;
char ans;
temp=head;
if(temp==NULL)
{printf("\n insertion not possible");
return(0);
}
New=get_node();
if(New==NULL)
printf("\n Memory not allocated");
printf("\n Enter the element to insert:");
scanf("%d",&val);
New->data=val;
New->next=NULL;
printf("\n Where do u want insert:\n");
printf("b.Beginning\nm.Middle\nl.Last");
printf("\n Enter your choice:");
ans=getche();
switch(ans)
{
case 'b':New->next=temp;
head=New;
printf("\n Inserted at beginning");
break;
case 'm':printf("\n Enter the element after which u want to insert");
scanf("%d",&val);
temp=search(head,val);
if(temp!=NULL)
{
New->next=temp->next;
temp->next=New;
temp=New;
printf("\n Element is inserted");
}
break;
case 'l':while(temp->next!=NULL)
temp=temp->next;
temp->next=New;
New->next=NULL;
printf("\n Element inserted at last");
break;
default:printf("\n Entered wrong choice");
break;
}
return(head);
}

node *delet(node **head)


{
int val;
node *temp,*prev;
temp=*head;
if(temp==NULL)
{
printf("\n The list is empty");
return(0);
}
printf("\n Enter the value u want to delete");
scanf("%d",&val);
temp=search(*head,val);
if(temp!=NULL)
{
if(temp==*head)
*head=temp->next;
else
{
prev=*head;
while(prev->next!=temp)
prev=prev->next;
prev->next=temp->next;
}
free(temp);
printf("\n The element is deleted");
}
return(*head);
}

node *search(node *head,int val)


{
node *temp;
int found;
temp=head;
if(temp==NULL)
{
printf("\n The list is empty");
return(NULL);
}
found=FALSE;
while(temp!=NULL&&!found)
{
if(temp->data!=val)
temp=temp->next;
else
found=TRUE;
}
if(found)
{
printf("\n The element is present in the list");
return(temp);
}
else
{
printf("\n The element is not present in the list");
return(NULL);
}
}

void count(node *head)


{
node *temp;
int count=0; temp=head;
if(temp==NULL)
printf("\n The list is empty");
else
{
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("\nThe size of list is= %d",count);
}
}

void display(node *head)


{
node *temp;
temp=head;
if(temp==NULL)
{
printf("\nThe list is empty");
return;
}
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("NULL");
}

node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return(temp);
}
OUTPUT:

Representation of singly linked list:


1.create
2.insert
3.delete
4.search
5.display
6.count
7.exit

Enter your choice: 1


Enter the element: 10

Do u want to continue:(y/n)y
Enter the element: 20

Do u want to continue:(y/n)y
Enter the element: 30

Do u want to continue:(y/n)n
The singly linked list is created

Enter your choice: 5


10-->20-->30-->NULL

Enter your choice: 4


Enter the element to search100
The element is not present in the list

Enter your choice: 4


Enter the element to search20
The element is present in the list

Enter your choice: 2


Enter the element to insert:100
Where do u want insert:
b.Beginning
m.Middle
l.Last
Enter your choice:m
Enter the element after which u want to insert20
The element is present in the list
Element is inserted

Enter your choice: 5


10-->20-->100-->30-->NULL
Enter your choice: 3
Enter the value u want to delete20
The element is present in the list
The element is deleted

Enter your choice: 5


10-->100-->30-->NULL

Enter your choice:7

SHELL SORT

AIM:
To sorting the given numbers using Shell sort.

OBJECTIVE:

To sorting given numbers in ascending order using Shell sort.

HARDWARE REQUIREMENTS:

PC (Pentium IV)

SOFTWARE REQUIREMENTS:

Turbo C Compiler

PROGRAM:
#include<stdio.h>
#include<conio.h>
void shell_sort(int a[],int n);
void main()
{
int n,i,a[100];
clrscr();
printf("\t\t\n enter the size of the elements");
scanf("%d",&n);
printf("\t\t\n enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
shell_sort(a,n);
printf("\t\t\n the sorted list is:");
for(i=0;i<n;i++)
{
printf("\t\n %d",a[i]);
}
getch();
}
void shell_sort(int a[],int n)
{
int i,j,k,temp;
for(k=n/2;k>0;k=k/2)
{
for(i=k;i<n;i++)
{
temp=a[i];
for(j=i;j>=k&&a[j-k]>temp;j=j-k)
{
a[j]=a[j-k];
}
a[j]=temp;
getch();
}
}
}

OUTPUT:

Enter the number of elements: 5


Enter the array elements one by one
50 20 60 35 40
The sorted list is : 20
35
40
50
60

Enter the number of elements: 6


Enter the array elements one by one
53 26 34 12 9 45
The sorted list is : 9
12
26
34
45
53
RESULT:
Thus the Shell sort program is executed and the output is verified.
QUICK SORT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void quicksort(int a[],int,int);
void main()
{
int a[10],i;
clrscr();
printf("/n Enter the number of elements to sort\n");
for(i=0; i<10; i++)
{
scanf("%d",&a[i]);
}
quicksort(a,0,9);
printf("\n The sorted list is");
for(i=0; i<10;i++)
{
printf("\n %d",a[i]);
}
getch();
}
void quicksort(int a[], int left, int right)
{
int i, pivot, temp, j;
if (left<right)
{
pivot=left;
i=left + 1;
j=right;
while(i<j)
{
while(a[pivot]>=a[i])
{
i = i + 1;
}
while(a[pivot] < a[j])
{
j=j-1;
}
if (i<j)
{
temp=a[i];
a[i] = a[j];
a[j]=temp;
}
else
{
temp=a[pivot];
a[pivot] = a[j];
a[j]=temp;
quicksort(a,left,i-1);
quicksort(a,j+1,right);
}
}
}

OUTPUT:

Enter the number of content: 5


Enter the numbers one by one
50 30 40 80 10
The sorted list is : 10
30
40
50
80

Enter the number of content: 6


Enter the numbers one by one
95 32 14 58 45 12
The sorted list is: 12
14
32
45
58
95
HEAP SORT

PROGRAM:

#include<stdio.h>
#include<conio.h>
void heap(int a[],int n);
void create(int a[],int n);
void main()
{
int a[50],i,n;
clrscr();
printf("enter the limit:");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
heap(a,n);
printf("The sorted list using HEAP is ...\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void create(int a[],int n)
{
int i,j,q,key;
for(q=i;q<n;q++)
{
i=q;
key=a[q];
j=(int)(i/2);
while((i>0)&&key>a[j])
{
a[i]=a[j];
i=j;
j=(int)(i/2);
if(j<0)
j=0;
}
a[i]=key;
}
}
void heap(int a[],int n)
{
int i,j,q,key,temp;
create(a,n);
for(q=n-1;q>=1;q--)
{
temp=a[0];
a[0]=a[q];
a[q]=temp;
i=0;
key=a[0];
j=1;
if(a[j+1]<q)
if(a[j+1]>a[j])
j=j+1;
while(j<=(q-1)&&(a[j]>key))
{
a[i]=a[j];
i=j;
j=2*i;
if((j+1)<q)
if(a[j+1]>a[j])
j=j+1;
else if(j>n-1)
j=n-1;
a[i]=key;
}
}
}

OUTPUT:

Enter the number of content: 5


Enter the elements:
16
18
11
43
40

The sort list using HEAP is:

11 16 18 40 43

Enter the number of content: 6


Enter the elements:
18
19
10
42
41
12

The sort list using HEAP is:

10 12 18 19 41 42

MERGE SORT

PROGRAM:

#include<stdio.h>
#include<conio.h>
void mergesplit(int a[],int first,int last);
void merge(int a[],int f1,int l1,int f2,int l2);
int a[25],b[25];
void main()
{
int i,n;
clrscr();
printf("enter the limit:");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesplit(a,0,n-1);
printf("The sorted list using MERGE sort is ...\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void mergesplit(int a[],int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
mergesplit(a,first,mid);
mergesplit(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}

void merge(int a[],int f1,int l1,int f2,int l2)


{
int i,j,k=0;
i=f1;
j=f2;
while(i<=l1&&j<=l2)
{
if(a[i]<a[j])
b[k]=a[i++];
else
b[k]=a[j++];
k++;
}
while(i<=l1)
b[k++]=a[i++];
while(j<=l2)
b[k++]=a[j++];
i=f1;
j=0;
while(i<=l2 && j<k)
a[i++]=b[j++];
}
OUTPUT:

Enter the number of content: 5


Enter the elements:
15
17
10
42
39

The sort list using MERGE is:

10 15 17 39 42

Enter the number of content: 6


Enter the elements:
21
18
43
15
40
28

The sort list using MERGE is:

15 18 21 28 40 43

LINEAR SEARCH:
#include<stdio.h>
#include<conio.h>
svoid main()
{
int i,j,k,n,a[100],f=1;
clrscr();
printf("\t\t\t ENTER THE SIZE OF SORTING:");
scanf("%d",&n);
printf("\t\t\t ENTER THE ELEMENTS OF THE ARRAY:\n\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\t\t\t ENTER THE ELEMENTS TO BE SEARCHED:");
scanf("%d",&k);
for(i=0;i<=n;i++)
{
if(k==a[i])
{
printf("\n\n ELEMENTS %d IS IN THE POSITION %d",k,i+1);
f=2;
break;
}
}
if(f==1)
printf("\n THE ENTERED ELEMENT IS NOT IN THE ARRAY");
getch();
}
OUTPUT:

ENTER THE SIZE OF SORTING:5

ENTER THE ELEMENTS OF THE ARRAY:

11 19 18 12 14

ENTER THE ELEMENTS TO BE SEARCHED:18

ELEMENT 18 IS IN THE POSITION 3

BINARY SEARCH :

#include<stdio.h>

#include<conio.h>

void main()

int a[100],m,i,n,l,h,t,f=1;

clrscr();
printf("\t\t\n ENTER THE SIZE OF ARRAY");

scanf("%d",&n);

printf("\t\t\n ENTER THE ELEMENTS");

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

scanf("%d",&a[i]);

printf("\t\t\n ENTER THE ELEMENT TO BE SEARCHED");

scanf("%d",&t);

l=0;

h=n-1;

while(l<=h)

m=(l+h)/2;

if(t<a[m])

h=m-1;

else if(t>a[m])

l=m+1;

else

printf("\n\n ENTERED %d IS IN THE POSITION %d",t,m+1);

f=2;

break;

} if(f==1)

printf("\t\t ENTERED ELEMENT IS NOT IN ARRAY");

getch();

Vous aimerez peut-être aussi