0% found this document useful (0 votes)
7 views

DS

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DS

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD

Department of BCA

Assignment No : 1 Date : Roll No :


Title : Write a program to implement stack using static method.

Program:

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

int main()

top=-1;

printf("\nEnter the size of stack: ");

scanf("%d",&n);

printf("\n\tStack operation using array");

printf("\n\t------------------------");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do

printf("\n Enter the choice:");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

{
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

pop();

break;

case 3:

display();

break;

case 4:

printf("\n\tExist point");

break;

default:

printf("\n\t Please Enter a valid choice(1/2/3/4)");

while(choice=4);

return 0;

void push()

if(top>=n-1)

printf("\n\t Stack is over flow");


Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

else

printf("Enter a value to pushed:");

scanf("%d",&x);

top++;

stack[top]=x;

void pop()

if(top<=-1)

printf("\n\t Stack is under flow");

else

printf("\n\tThe popped element is%d",stack[top]);

top--;

void display()

if(top>=0)

printf("\nThe elements in stack\n");

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

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

printf("\nPress next choice");


Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

else

printf("\n The stack is empty");

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Assignment No : 2 Date : Roll No :


Title : Programs to implement applications of stack.

Program:

a) Write a program to reverse a string using stack.

#include<stdio.h>

#include<string.h>

#define size 20

int top= -1;

char stack[size];

char push(char ch)

if(top==(size-1))

printf("Stack is Overflow\n");

else

stack[++top]=ch;

char pop()

if(top==-1)

printf("Stack is Underflow\n");

else

return stack[top--];

int main()

char str[20];

int i;

printf("Enter the string:\n");


Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

gets(str);

for(i=0;i<strlen(str);i++)

push(str[i]);

for(i=0;i<strlen(str);i++)

str[i]=pop();

printf("Reversed string is:");

puts(str);

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Program:

(b)Write a program to convert infix to postfix expression using stack.

#include<stdio.h>
char stack[100];
int top= -1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
if(top== -1)
return-1;
else
return stack[top--];
}
int priority(char x)
{
if(x=='(')
return 0;
if(x=='+'|| x=='-')
return 1;
if(x=='*'|| x=='/')
return 2;
return 0;
}
int main()
{
char exp[100];
char*e,x;
printf("Enter the expression:");
scanf("%s",exp);
printf("\n");
e=exp;

while(*e !='\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e =='(')
push(*e);
else if(*e==')')
{
while((x=pop()!='('))
printf("%c",x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

e++;
}
while(top!=-1)
{
printf("%c",pop());
}
return 0;
}

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Assignment No : 3 Date : Roll No :


Title : Write a program to implement Queue using static method.

#include<stdio.h>
int main()
{
int queue[100],ch=1,front=0,rear=0,i,j=1,n;
printf("Enter size of the Queue:");
scanf("%d",&n);
printf("Queue using Array");
printf("\n 1.Insertion\n 2.Deletion\n 3.Display\n 4.Exit");
while(ch)
{
printf("\n Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==n)
printf("\nQueue is full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
n++;
}
break;
case 3:
printf("\n Queue Elements are:\n");
if(front==rear)
printf("\n Queue is empty");
else
{
for(i=front;i<rear;i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

printf("Wrong Choice:please see the options");


}
}
}
return 0;
}

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Assignment No : 4 Date : Roll No :


Title : Write a program to create linked list , add node to linked list, and remove node from
linked list.

Program:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n 1.Insert in beginning\n 2.Insert at last\n 3.Insert at any random location\n 4.Delete
from Beginning\n 5.Delete from last\n 6.Delete node after specified location\n 7.Search for an
element\n 8.Show\n 9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}

}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");

}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}

}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

struct node *ptr;


if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}

else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}

void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

else
{
printf("\ n printing values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Assignment No : 5 Date : Roll No :


Title : Write a program to implement types of linked list.

a) Singular Linked List

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

};

struct node *head = NULL;

struct node *current = NULL;

//display the list

void printList() {

struct node *ptr = head;

printf("\n[head] =>");

//start from the beginning

while(ptr != NULL) {

printf(" %d =>",ptr->data);

ptr = ptr->next;

printf(" [null]\n");

//insert link at the first location

void insert(int data) {

//create a link

struct node *link = (struct node*) malloc(sizeof(struct node));

//link->key = key;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

link->data = data;

//point it to old first node

link->next = head;

//point first to new first node

head = link;

int main() {

insert(10);

insert(20);

insert(30);

insert(1);

insert(40);

insert(56);

printList();

return 0;

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

b) Circular Linked List

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

struct node {
int data;
struct node *next;
};

struct node *head = NULL;


struct node *current = NULL;

//insert link at the first location


void insert(int data) {
// Allocate memory for new node;
struct node *link = (struct node*) malloc(sizeof(struct node));

link->data = data;
link->next = NULL;

// If head is empty, create new list


if(head==NULL) {
head = link;
head->next = link;
return;
}

current = head;

// move to the end of the list


while(current->next != head)
current = current->next;

// Insert link at the end of the list


current->next = link;

// Link the last node back to head


link->next = head;

//display the list


void printList() {
struct node *ptr = head;

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

printf("\n[head] =>");

//start from the beginning


while(ptr->next != head) {
printf(" %d =>",ptr->data);
ptr = ptr->next;
}

printf(" %d =>",ptr->data);
printf(" [head]\n");
}

int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);

printList();
return 0;
}

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

c) Doubly Linked List

// C Program to Implement Doubly Linked List

#include <stdio.h>

#include <stdlib.h>

// defining a node

typedef struct Node {

int data;

struct Node* next;

struct Node* prev;

} Node;

// Function to create a new node with given value as data

Node* createNode(int data)

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

newNode->data = data;

newNode->next = NULL;

newNode->prev = NULL;

return newNode;

// Function to insert a node at the beginning

void insertAtBeginning(Node** head, int data)

// creating new node

Node* newNode = createNode(data);

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

// check if DLL is empty

if (*head == NULL) {

*head = newNode;

return;

newNode->next = *head;

(*head)->prev = newNode;

*head = newNode;

// Function to insert a node at the end

void insertAtEnd(Node** head, int data)

// creating new node

Node* newNode = createNode(data);

// check if DLL is empty

if (*head == NULL) {

*head = newNode;

return;

Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

// Function to insert a node at a specified position

void insertAtPosition(Node** head, int data, int position)

if (position < 1) {

printf("Position should be >= 1.\n");

return;

// if we are inserting at head

if (position == 1) {

insertAtBeginning(head, data);

return;

Node* newNode = createNode(data);

Node* temp = *head;

for (int i = 1; temp != NULL && i < position - 1; i++) {

temp = temp->next;

if (temp == NULL) {

printf(

"Position greater than the number of nodes.\n");

return;

newNode->next = temp->next;

newNode->prev = temp;

if (temp->next != NULL) {

temp->next->prev = newNode;

}
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

temp->next = newNode;

// Function to delete a node from the beginning

void deleteAtBeginning(Node** head)

// checking if the DLL is empty

if (*head == NULL) {

printf("The list is already empty.\n");

return;

Node* temp = *head;

*head = (*head)->next;

if (*head != NULL) {

(*head)->prev = NULL;

free(temp);

// Function to delete a node from the end

void deleteAtEnd(Node** head)

// checking if DLL is empty

if (*head == NULL) {

printf("The list is already empty.\n");

return;

Node* temp = *head;


Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

if (temp->next == NULL) {

*head = NULL;

free(temp);

return;

while (temp->next != NULL) {

temp = temp->next;

temp->prev->next = NULL;

free(temp);

// Function to delete a node from a specified position

void deleteAtPosition(Node** head, int position)

if (*head == NULL) {

printf("The list is already empty.\n");

return;

Node* temp = *head;

if (position == 1) {

deleteAtBeginning(head);

return;

for (int i = 1; temp != NULL && i < position; i++) {

temp = temp->next;

if (temp == NULL) {

printf("Position is greater than the number of "


Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

"nodes.\n");

return;

if (temp->next != NULL) {

temp->next->prev = temp->prev;

if (temp->prev != NULL) {

temp->prev->next = temp->next;

free(temp);

// Function to print the list in forward direction

void printListForward(Node* head)

Node* temp = head;

printf("Forward List: ");

while (temp != NULL) {

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

temp = temp->next;

printf("\n");

// Function to print the list in reverse direction

void printListReverse(Node* head)

Node* temp = head;

if (temp == NULL) {
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

printf("The list is empty.\n");

return;

// Move to the end of the list

while (temp->next != NULL) {

temp = temp->next;

// Traverse backwards

printf("Reverse List: ");

while (temp != NULL) {

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

temp = temp->prev;

printf("\n");

int main()

Node* head = NULL;

// Demonstrating various operations

insertAtEnd(&head, 10);

insertAtEnd(&head, 20);

insertAtBeginning(&head, 5);

insertAtPosition(&head, 15, 2); // List: 5 15 10 20

printf("After Insertions:\n");

printListForward(head);

printListReverse(head);
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

deleteAtBeginning(&head); // List: 15 10 20

deleteAtEnd(&head); // List: 15 10

deleteAtPosition(&head, 2); // List: 15

printf("After Deletions:\n");

printListForward(head);

return 0;

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Assignment No : 6 Date : Roll No :


Title : Write a program to implement stack and queue dynamically.

Program:

a) Write a program to implement stack using dynamic method.

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n 1.Push\n 2.Pop\n 3.Show\n 4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value:");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

}
printf("Item pushed");

}
}

void pop()
{

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

b) Write a program to implement queue using dynamic method.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\
n=================================================================\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice :");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\n printing values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

}
}

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Assignment No : 7 Date : Roll No :


Title : Write a program to sort given element using bubble sort , insertion sort, selection sort.

(a) Write a program to implement bubble sort

#include<stdio.h>

#include<conio.h>

void main()

int A[]={34,15,29,8,1};

int i;

bubble_sort(A,5);

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

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

getch();

bubble_sort(int A[],int N)

int round,i,temp;

for(round=1;round<=N-1;round++)

for(i=0;i<=N-1-round;i++)

if(A[i]>A[i+1])

temp=A[i];

A[i]=A[i+1];

A[i+1]=temp;

return 0;

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Output:

(b) Write a program to implement insertion sort.

#include<math.h>

#include<stdio.h>

void insertion_sort(int arr[],int n)

int i,key,j;

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

key= arr[i];

j=i-1;

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

arr[j+1]= arr[j];

j=j-1;

arr[j+1]=key;

void printarray(int arr[],int n)

int i;

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

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

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

printf("\n");

int main()

int arr[]={12,11,13,5,6};

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

insertion_sort(arr,n);

printarray(arr,n);

return 0;

Output:

(c) Write a program to implement selection sort.

#include<stdio.h>

void selection(int arr[],int n)

int i,j,small;

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

small=i;

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

if(arr[j]<arr[small])

small=j;

int temp=arr[small];

arr[small]=arr[i];

arr[i]=temp;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

void printarr(int a[],int n)

int i;

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

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

int main()

int a[]={12,31,25,8,32,17};

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

printf("before sorting array elements are- \n");

printarr(a,n);

selection(a,n);

printf("\n after sorting array elements are- \n");

printarr(a,n);

return 0;

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Assignment No : 8 Date : Roll No :


Title : Write a program to search given element using linear search.

#include <stdio.h>

int main()

int array[100], search, c, number;

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

scanf("%d",&number);

printf("Enter %d numbers\n", number);

for ( c = 0 ; c < number ; c++ )

scanf("%d",&array[c]);

printf("Enter the number to search\n");

scanf("%d",&search);

for ( c = 0 ; c < number ; c++ )

if ( array[c] == search ) /* if required element found */

printf("%d is present at location %d.\n", search, c+1);

break;

if ( c == number )

printf("%d is not present in array.\n", search);

return 0;

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Output:

Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

Assignment No : 9 Date : Roll No :


Title : Write a program to search given element using binary search.

#include <stdio.h>

int main()

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

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

scanf("%d", &array[c]);

printf("Enter value to find\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < search)

first = middle + 1;

else if (array[middle] == search) {

printf("%d found at location %d.\n", search, middle+1);

break;
Page No :
ARTS, COMMERCE AND SCIENCE COLLEGE, KOWAD
Department of BCA

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)

printf("Not found! %d isn't present in the list.\n", search);

return 0;

Output:

Page No :

You might also like