Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views57 pages

A. Add An Element at The Beginning of An Array B. Insert An Element at Given Index of Array

Uploaded by

nagasaisri0915
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views57 pages

A. Add An Element at The Beginning of An Array B. Insert An Element at Given Index of Array

Uploaded by

nagasaisri0915
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 57

1.

Write a program to read ‘N’ numbers of elements into an array and also perform
the following operation on an array

a. Add an element at the beginning of an array


b. Insert an element at given index of array

#include<stdio.h>

void insertElement(int arr[],int*n,int x,int pos,int capacity)

if(pos<0||pos>*n||*n>=capacity)

printf(“Error:Invalid position or array is full.\n”);

return;

for(int i=*n;i>pos;i--)

arr[i]=arr[i-1];

arr[pos]=x;

(*n)++;

int main()

int capacity=10;
int arr[capacity]={10,20,30,40,50};

int n=5;

int x=25;

int pos=2;

insertElement(arr,&n,x,pos,capacity);

printf(“Array after insertion:\n”);

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

printf(“%d”,arr[i]);

printf(“\n”);

return 0;

OUTPUT:

c. Update an element using a values and index


d. Delete an existing element.

#include<stdio.h>

void deleteElement(int arr[],int*n,int pos)


{

if(pos<0||pos>=*n)

printf(“Error:Invalid position.\n”);

return;

for(int i=pos;i<*n-1;i++)

arr[i]=arr[i+1];

(*n)--;

int main()

int arr[]={10,20,30,40,50};

int n=sizeof(arr)/sizeof(arr[0]);

int pos=2;

deleteElement(arr,&n,pos);

printf(“Array after deletion:\n”);

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

printf(“%d”,arr[i]);

}
printf(“\n”);

return 0;

OUTPUT:
2. Write Program to implement Single Linked List with insertion,
deletion and traversal operations

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*next;

};

struct node*head;

void insert_beg (int value)

struct node*newNode;

newNode=malloc (sizeof (struct node));

newNode->data=value;

newNode->next=head;

head=newNode;

void insert_end(int value)

struct node*newNode;

newNode=malloc(sizeof(struct node));
newNode->data=value;

newNode->next=NULL;

while(temp->next!=NULL)

temp=temp->next;

temp->next=newNode;

void insert_pos(int value,int position)

int i;

struct node*newNode;

newNode=malloc(sizeof(struct node));

newNode->data=value;

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

if(temp->next!=NULL)

temp=temp->next;

newNode->next=temp->next;

temp->next=newNode;
}

void print_list()

struct node*temp=headprintf("\n\nlist elements are_\n");

while(temp->next!=NULL)

printf("%d--->",temp->data);

temp=temp->next;

printf("%d",temp->data);

void main()

insert_beg(12);

insert_beg(22);

insert_end(30);

insert_pos(33,2);

printf("Linked list:");

print_list();

OUTPUT:

Linked list:
List elements are-

22 123033

DELETION:
#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*next;

};

struct node*head;

void insert(int value)

struct node*newNode;

newNode=malloc(sizeof(struct node));

newNode->data=value;

newNode->next=head;

head=newNode;

void delete_begin()

head=head->next;
}

void delete_end()

struct node*temp=head;

while(temp->next->next!=NULL)

temp=temp->next;

temp->next=NULL;

void delete_pos(int position)

int i;

struct node*temp=head;

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

if(temp->next!=NULL)

temp=temp->next;

temp->next=temp->next->next;
}

void print_list()

struct node*temp=head;

printf(Linked List elements are-\n);

while(temp->next!=NULL)

printf(“%d->”,temp->data);

temp=temp->next;

printf(“%d”,temp->data);

void main()

int i;

for(i=11;i<20;i++)

insert(i);

printf(“\n\nBefore Deletion:\n”);

print_list();

delete_begin();

printf”\n\nAfter Deletion at Beginning..\n”);

print_list();
delete_end();

printf(“\n\nAfter Deletion at end..\n”);

print_list();

delete_pos(3);

printf(“\n\nAfter Deletion at position3..\n”);

print_list();

OUTPUT:

Before Deletion:

Linked list elements are-

19--- >18--- >17--- >16--- >15--- >14--- >13--- >12--- >11

After Deletion at Beginning:

Linked list elements are-

18--- >17--- >16--- >15--- >14--- >13--- >12--- >11

After Deletion at End :

Linked list elements are-

18--- >17--- >16--- >15--- >14--- >13--- >12

After Deletion at Position 3:

Linked list elements are:

18--- >17--- >16--- >15--- >13--- >12


3. Write Program to implement Circular doubly Linked List with insertion,
deletion and traversal operations

#include<stdio.h>

#include<stdlib.h>

struct Node

int data;

struct Node*next;

struct Node*prev;

};

struct Node*head=NULL;

void insertAtBeginning (int value)

struct Node*newNode=(struct Node*)malloc(sizeof(struct Node));

newNode->data=value;

if(head==NULL)

newNode->next=newNode;

newNode->prev=newNode;

head=newNode;

else
{

struct Node*tail=head->prev;

newNode->next=head;

newNode->prev=tail;

tail->next=newNode;

head->prev=newNode;

head=newNode;

void deleteNode(int value)

if(head==NULL)

printf("List is empty.\n");

return;

struct Node*temp=head;

struct Node*toDelete=NULL;

do

if(temp->data==value)

toDelete=temp;
break;

temp=temp->next;

while(temp!=head);

if(toDelete==NULL)

printf("Node with value %dnot found.\n",value);

return;

if(toDelete->next==toDelete&&toDelete->prev==toDelete)

head=NULL;

free(toDelete);

return;

if(toDelete==head)

head=head->next;

toDelete->prev->next=toDelete->next;

toDelete->next->prev=toDelete->prev;

free(toDelete);
}

void search(int value)

if(head==NULL)

printf("List is empty.\n");

return;

struct Node*temp=head;

int position=1;

do

if(temp->data==value)

printf("Node with value %d found at position%d.\n",value,position);

return;

temp=temp->next;

position++;

while(temp!=head);

printf("Node with value %d not found.\n",value);

}
void printList()

if(head==NULL)

printf("List is empty.\n");

return;

struct Node*temp=head;

printf("List elements:");

do

printf("%d",temp->data);

temp=temp->next;

while(temp!=head);

printf("\n");

int main()

insertAtBeginning(10);

insertAtBeginning(20);

insertAtBeginning(30);

printList();
search(20);

search(40);

deleteNode(20);

printList();

deleteNode(30);

printList();

deleteNode(10);

printList();

return 0;

OUTPUT:

List elements:30 20 10

Node with value 20 found at position 2

Node with value 40 not found

List elements: 30 10

List elements:10

List is empty

4. Write Programs to implement the Stack operations using an array

#include<stdio.h>

#define MAX_SIZE 100

int stack[MAX_SIZE];

int top=-1;
void push(int item)

if(top==MAX_SIZE-1)

printf("Stack Overflow\n");

return;

top++;

stack[top]=item;

int pop()

int item;

if(top==-1)

printf("Stack Underflow\n");

return-1;

item=stack[top];

top--;

return item;

int peek()
{

if(top==-1)

printf("Stack Underflow\n");

return-1;

return stack[top];

int isFull()

if(top==MAX_SIZE-1)

printf("Stack is full\n");

else

printf("Stack is not full\n");

int isEmpty()

if(top==-1)

printf("Stack is empty\n");

else

printf("Stack is not empty\n");

void printStack()
{

int i;

if(top==-1)

printf("Stack is empty\n");

return;

printf("Stack elements:");

for(i=top;i>=0;i--)

printf("%d",stack[i]);

printf("\n");

int main()

int choice,item;

while(1)

printf("\n1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit\n");

printf("Enter your choice:");

scanf("%d",&choice);

switch(choice)
{

case1:

printf("Enter item to push:");

scanf("%d",&item);

push(item);

break;

case2:

item=pop();

if(item!=-1)

printf("Popped item:%d\n",item);

break;

case3:

item=peek();

if(item!=-1)

printf("Top element:%d\n",item);

break;

case4:

isFull();

break;
case5:

isEmpty();

break;

case6:

printStack();

break;

case7:

printf("Exiting...\n");

return 0;

default:

printf("Invalid choice\n");

return 0;

OUTPUT:

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:1

Enter item to Push:10

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:1

Enter item to Push:20

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Enter your choice:4

Stack is not full

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:5

Stack is not empty

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:6

Stack elements:20 10

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:1

Enter item to Push:30

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:1

Enter item to Push:40

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:4

Stack is not full

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:2

Popped item:40

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:3

Top element:30
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:6

Stack elements:30 20 10

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:7

Exiting

5. Write a program using stacks to convert a given infix expression


to postfix

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int prec(char c)

if(c=='^')

return 3;

else if(c=='/'||c=='*')

return 2;

else if(c=='+'||c=='-')

return 1;

else

return-1;
}

char associativity(char c)

if(c=='^')

return'R';

return'L';

void infixToPostfix(char s[])

int i;

char result[1000];

int resultIndex=0;

int len=strlen(s);

char stack[1000];

int stackIndex=-1;

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

char c=s[i];

if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))

result[resultIndex++]=c;

}
else if(c=='(')

stack[++stackIndex]=c;

else if(c==')')

while(stackIndex>=0 &&stack[stackIndex]!='(')

result[resultIndex++]==stack[stackIndex--];

stackIndex--;

else

while(stackIndex>=0&&(prec(s[i])<prec(stack[stackIndex]) ||

prec(s[i])==prec(stack[stackIndex])&&associativity(s[i])=='L'))

result[resultIndex++]=stack[stackIndex--];

stack[++stackIndex]=c;

}
while(stackIndex>=0)

result[resultIndex++]=stack[stackIndex--];

result[resultIndex]='\0';

printf("Postfix Notation :%s\n",result);

int main()

char exp[100];

printf("Enter Infix Notation:");

gets(exp);

infixToPostfix(exp);

return 0;

OUTPUT:

Enter infix notation: a+b*c+d

Postfix notation: abc*+d+

6. Write Programs to implement the Stack operations using Linked


List.

#include<stdio.h>

#include<stdlib.h>
struct Node

int data;

struct Node*next;

};

struct Node*top=NULL;

void push(int value)

struct Node*newNode=(struct Node*)malloc(sizeof(struct Node));

if(newNode==NULL)

printf("Memory allocation failed!\n");

return;

newNode->data=value;

newNode->next=top;

top=newNode;

printf("Pushed %d onto the stack.\n",value);

void pop()

if(top==NULL)

{
printf("Stack Underflow!Cannot pop.\n");

return;

struct Node*temp=top;

int value=top->data;

top=top->next;

free(temp);

printf("Popped %d from the stack.\n",value);

int peek()

if(top==NULL)

printf("Stack is empty!\n");

return-1;

return top->data;

int isFull()

printf("For a linked list-based stack,the stack is never full(except for


memory limits)\n");

}
int isEmpty()

if(top==NULL)

printf("Stack is empty!\n");

else

printf("Stack is not empty!\n");

void print_stack()

struct Node*temp=top;

if(temp==NULL)

printf("Stack is empty.\n");

return;

printf("Stack elements:\n");

while(temp!=NULL)

printf("%d\n",temp->data);

temp=temp->next;

int main()
{

int choice,item;

while(1)

printf("\n1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit\
n");

printf("Enter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("Enter item to push:");

scanf("%d",&item);

push(item);

break;

case 2:

pop();

break;

case 3:

printf("Top element after popping:%d.\n",peek());

break;

case 4:

isFull();
break;

case 5:

isEmpty();

break;

case 6:

print_stack();

break;

case 7:

printf("Exiting...\n");

return 0;

default:

printf("Invalid choice\n");

return 0;

OUTPUT:

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:5

Stack is empty!

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:1


Enter item to push:11

Pushed 11 onto Stack

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:1

Enter item to push:22

Pushed 22 onto Stack

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:5

Stack is not empty!

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:4

For a Linked list-based Stack,the Stack is never full

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:6

Stack elements:

22

11

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:1

Enter item to push:33

Pushed 33 onto Stack

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:3


Top element after popping:33

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:2

Popped 33 from the stack

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:6

Stack elements

22

11

1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit

Enter your choice:7

Exiting…

7. Write Programs to implement the Queue operations using an


array.

#include<stdio.h>

#include<stdlib.h>

#define SIZE 5

int queue[SIZE];

int front=-1;

int rear=-1;

void isFull()

{
if(rear == SIZE -1)

printf(“Queue is full\n”);

Void is Empty()

If(front==-1||front>rear)

Printf(“queue is empty \n”);

else

printf(“Queue is not empty\n”);

void enqueue(int value)

If(rear==SIZE -1)

Printf(“Queue is full! Cannot enqueue %d\n”,value);

Return;

if(front == -1)

front=0;

rear++;

queue[rear]=value;
printf(“Enqueued:%d\n”,value);

void dequeue()

if(front==-1||front>rear)

printf(“Queue is empty!Cannot dequeue\n”);

return;

printf(“Dequeued:%d\n”,queue[front]);

front++;

if(front>rear)

front=rear=-1;

void peek()

if(front==-1 || front >rear)

printf(“Queue is empty!Cannot peek\n:”);

return;

}
printf(“Front item:%d\n”,queue[front]);

void print_queue()

int i;

f(front==-1 || front>rear)

printf(“Queue is empty\n “);

return;

printf(“Queue elements:”);

for(i=front;i<=rear;i++)

printf(“%d”,queue[i]);

printf(“\n”);

int main()

int choice,value;

do

printf(“\nQueue operations:\n”);
printf(“1.Enqueue 2.Dequeue 3.Peek 4.Check if full 5.Check if Empty
6.print_queue 7.Exit\n”);

Printf(“Enter your choice:”);

Scanf(“%d”,&choice);

switch(choice)

case 1:

printf("Enter value to enqueue:");

scanf("%d",&value);

push(value);

break;

case 2:

dequeue();

break;

case 3:

peek();

break;

case 4:

isFull();

break;

case 5:

isEmpty();

break;
case 6:

print_queue();

break;

case 7:

printf("Exiting...\n");

return 0;

default:

printf("Invalid choice! Please try again.\n");

while(choice !=7);

return 0;

OUTPUT:

Queue operations:

1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue


7.Exit

Enter your choice:1

Enter value to enqueue:11

Enqueued:11

Queue operations:
1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue
7.Exit

Enter your choice:1

Enter value to enqueue:22

Enqueued:22

Queue operations:

1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue


7.Exit

Enter your choice:1

Enter value to enqueue:33

Enqueued:33

Queue operations:

1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue


7.Exit

Enter your choice:6

Queue elements:11 22 33

Queue operations:

1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue


7.Exit

Enter your choice:3

Front item:11

Queue operations:
1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue
7.Exit

Enter your choice:4

Queue is not full

Queue operations:

1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue


7.Exit

Enter your choice:5

Queue is not empty

Queue operations:

1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue


7.Exit

Enter your choice:2

Dequeued:11

Queue operations:

1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue


7.Exit

Enter your choice:2

Queue elements:22 33

Queue operations:

1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue


7.Exit

Enter your choice:7

Exiting…
8. Write Programs to implement the Queue operations using Liked
List.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* front=NULL;
struct Node* rear=NULL;
int isFull()
{
printf(" A linked list is dynamically allocated,\n so
its never full unless memory allocation fails\n");
}
int isEmpty()
{
if(front==NULL)
printf("Queue is empty.\n");
else
printf("Queue is not empty.\n");
}
void insert(int value)
{
struct Node*
newNode=(structNode*)malloc(sizeof(structNode));
if(newNode==NULL)
{
printf(" Memory allocation failed! Queue is
full.\n");
return;
}
newNode->data=value;
newNode->next=NULL;
if(front==NULL && rear==NULL)
{
front=rear=newNode;
}
else
{
rear->next=newNode;
rear=newNode;
}
printf("Inserted %d into the queue.\
n",value);
}
void delete()
{
if(front==NULL)
{
printf("Queue is empty! cannot delete.\
n");
return;
}
struct Node* temp=front;
int value=temp->data;
front=front->next;
if(front==NULL)
rear=NULL;
free(temp);
printf("Deleted %d from the queue.\
n",value);
}
void peek()
{
if(front==NULL)
printf("Queue is empty! cannot peek.\n");
else
printf("Front element is:%d\n",front->data);
}
void print()
{
if(front==NULL)
{
printf("Queue is empty.\n");
return;
}
struct Node*temp=front;
printf("Queue elements:");
while(temp !=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
int choice,value;
do
{
printf("1.Insert 2.Delete 3.Peek 4.Print 5.Check
if Empty 6. Check if full 7.Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter value to insert:");
scanf("%d",&value);
insert(value);
break;
case 2:
delete();
break;
case 3:
peek();
break;
case 4:
print();
break;
case 5:
isEmpty();
break;
case 6:
isFull();
break;
case 7:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
}
while(choice!=7);
return 0;
}
OUTPUT:
1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty
6.Check if Full 7.Exit
Enter your choice:5
Queue is empty.

1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty


6.Check if Full 7.Exit
Enter your choice:1
Enter value to insert:11
Inserted 11 into the queue.

1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty


6.Check if Full 7.Exit
Enter your choice:1
Enter value to insert:22
Inserted 22 into the queue.

1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty


6.Check if Full 7.Exit
Enter your choice:3
Front element is:11

1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty


6.Check if Full 7.Exit
Enter your choice:1
Enter value to insert:33
Inserted 33 into the queue
1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty
6.Check if Full 7.Exit
Enter your choice:4
Queue elements:11 22 33

1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty


6.Check if Full 7.Exit
Enter your choice:5
Queue is not empty

1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty


6.Check if Full 7.Exit
Enter your choice:6
A Linked list is dynamically allocated,
So its never full unless memory allocation fails

1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty


6.Check if Full 7.Exit
Enter your choice:7
Exiting…

10. Write a program to search an item in a given list using the


following Searching Algorithms
a. Linear Search

#include<stdio.h>

int main()

int arr[100],n,target,i,found=0;

printf(“Enter the number of elements in the array:”);

scanf(“%d”,&n);
printf(“Enter %d elements :\n”,n);

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

scanf(“%d”,&arr[i]);

printf(“Enter the element to search:”);

scanf(“%d,&target”);

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

if(arr[i]==target)

printf(“Element found at index %d\n”,n);

found=1;

break;

if(!found)

printf(“Element not found in the array\n”);

return 0;

OUTPUT 1:

Enter the number of elements in the array:7

Enter 7 Elements:
11 16 21 13 19 9 4

Enter the element to search:13

Element found at index 3

OUTPUT 2:

Enter the number of elements in the array:7

Enter 7 Elements:

11 16 21 13 19 9 4

Enter the element to search:10

Element not found in the array

b. Binary Search.

#include<stdio.h>

int main()

int arr[100];

int n,i,target,low,high,mid,found=0;

printf("Enter the number of elements in the array:");

scanf("%d",&n);

printf("Enter %dsorted elements:\n",n);

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

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

printf("Enter the element to search for:");


scanf("%d",&target);

low=0;

high=n-1;

while(low<=high)

mid=(low+high)/2;

if(arr[mid]==target)

printf("Element %d found at index%d.\n",target,mid);

found=1;

break;

else if(arr[mid]<target)

low=mid+1;

} else

high=mid-1;

if(!found)

printf("Element %d not found in the array.\n",target);

return 0;
}

OUTPUT 1:

Enter the number of elements in the array:10

Enter 10 sorted elements:

1 3 5 7 9 11 13 15 17 19

Enter the element to search for :7

Element 7 found at index 3

OUTPUT 2:

Enter the number of elements in the array:10

Enter 10 sorted elements:

1 3 5 7 9 11 13 15 17 19

Enter the element to search for :12

Element 12 not found in the array

11. Write a program for implementation of the following Sorting Algorithms

a. Bubble Sort

b. Insertion Sort

c. Quick Sort

a. Bubble sort:

#include<stdio.h>

int main()

int n,i,j,temp;
int A[100];

printf("Enter the number of elements:");

scanf("%d",&n);

printf("Enter the elements of the array:\n");

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

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

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("Sorted array:\n");

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

printf("%d",A[i]);

printf("\n");

return 0;
}

OUTPUT:

Enter the number of elements:6

Enter the elements of the array:

4 1 9 5 0 6

Sorted array:

0 1 4 5 6 9

b. Insertion sort:

#include<stdio.h>

int main()

int A[100],n,i,j,key;

printf("Enter the number of elements:");

scanf("%d",&n);

printf("Enter the elements of the array:\n");

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

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

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

key=A[i];

j=i-1;

while(j>=0&&A[j]>key)
{

A[j+1]=A[j];

j=j-1;

A[j+1]=key;

printf("Sorted array:\n");

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

printf("%d",A[i]);

printf("\n");

return 0;

OUTPUT:

Enter the number of elements:7

Enter the elements of the array:

9 1 5 3 7 2 6

Sorted array:

1 2 3 5 6 7 9

c. Quick sort:

#include<stdio.h>

int partition(int arr[],int low,int high)

int pivot=arr[high];
int i=low-1;

int j;

for(j=low;j<high;j++)

if(arr[j]<=pivot)

i++;

int temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

int temp=arr[i+1];

arr[i+1]=arr[high];

arr[high]=temp;

return i+1;

void quicksort (int arr[],int low,int high)

if(low<high)

int pi=partition(arr,low,high);

quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);

int main()

int arr[100],n,i;

printf("Enter the number of elements:");

scanf("%d",&n);

printf("Enter the elements of the array:\n");

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

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

quicksort(arr,0,n-1);

printf("Sorted array:");

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

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

printf("\n");

return 0;

OUTPUT:

Enter the number of elements:7

Enter the elements of the array:


3 9 5 2 7 1 8

Sorted array:

1 2 3 5 7 8 9

You might also like