0% found this document useful (0 votes)
5 views24 pages

Rescued document 2

The document is a practical file for a Data Structure course at Amity School of Engineering & Technology, detailing various programming tasks using C. It includes code implementations for operations on arrays, stacks, queues, linked lists, and sorting algorithms. Each task is accompanied by example code and expected output demonstrating the functionality of the implemented data structures.

Uploaded by

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

Rescued document 2

The document is a practical file for a Data Structure course at Amity School of Engineering & Technology, detailing various programming tasks using C. It includes code implementations for operations on arrays, stacks, queues, linked lists, and sorting algorithms. Each task is accompanied by example code and expected output demonstrating the functionality of the implemented data structures.

Uploaded by

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

/

SESSION: 2023-2027
AMITY SCHOOL OF ENGINEERING &
TECHNOLOGY
DATA STRUCTURE USING C
[CSIT124]
PRACTICAL FILE

Submitted by Submitted to
NAME: - KUSHAGRA KAPOOR Ms. NEHA
TYAGI
ENROLLMENT NUMBER: - A411119823002
BRANCH: - BTECH AI (3rd Semester)
SECTION: - SECTION E

INDEX
Write a program to insert a new element in the given unsorted array at kth position.
Write a program to delete an element from a given sorted array.
Write a program to merge two given sorted arrays.
Write a program to implement Stack using array, also show overflow and
underflow in respective push and pop operations.
Write a program to implement Queue using array, which shows insertion and
deletion operations.
Write a program to implement Circular Queue using an array, which shows
insertion and deletion operations.
Write a program to implement Linear Linked List, showing all the operations, like
creation, display, insertion, deletion and searching.
Write a program to implement Stack, using Linked List. Implement Push, Pop and
display operations.
Write a program to implement Queue, using Linked List. Implement Insertion,
deletion and display operations.
Write a program to count the number of times an item is present in a linked list.
Write a program to increment the data part of every node present in a linked list by
10. Display the data both before incrimination and after.
Write a program to implement Doubly Linked List, showing all the operations, like
creation, display, insertion, deletion and searching.
Write a program to search for an element using Linear Search.
Write a program to search for an element using Binary Search.
Write a program to sort the given array using Bubble Sort.

Question 1: - Write a program to insert a new element in the given unsorted array at kth
position.

Code: -
#include <stdio.h>
void insertElement(int arr[], int n, int k, int element) {
// Move elements to the right from the k-th position
for (int i = n; i > k; i--) {
arr[i] = arr[i - 1];
}
// Insert the new element at the k-th position
arr[k] = element;
}
int main() {
int n, k, element;
// Input: size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n+1]; // Declare an array of size n+1 to hold the new element
// Input: elements of the array
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input: position (k) and the element to be inserted
printf("Enter the position (index) where the element needs to be inserted: ");
scanf("%d", &k);
// Ensure valid position
if (k < 0 || k > n) {
printf("Invalid position! Please enter a position between 0 and %d.\n", n);
return 1;
}
printf("Enter the element to be inserted: ");
scanf("%d", &element);
// Call function to insert the element
insertElement(arr, n, k, element);
// Output: array after insertion
printf("Array after insertion: ");
for (int i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

OUTPUT: -
Enter the number of elements in the array: 5
Enter the elements of the array: 1 2 3 4 5
Enter the position (index) where the element needs to be inserted: 2
Enter the element to be inserted: 6
Array after insertion: 1 2 6 3 4 5

Question 2: - Write a program to delete an element from a given sorted array.

Code: -
#include <stdio.h>
void deleteElement(int arr[], int *n, int element) {
int pos = -1;
// Find the position of the element to delete
for (int i = 0; i < *n; i++) {
if (arr[i] == element) {
pos = i;
break;
}
}
// If element not found
if (pos == -1) {
printf("Element not found in the array.\n");
return;
}
// Shift elements to the left to fill the gap
for (int i = pos; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
// Decrease the size of the array
(*n)--;
}
int main() {
int n, element;
// Input: size of the array
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &n);

int arr[n]; // Declare an array


// Input: elements of the array
printf("Enter the elements of the sorted array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input: element to be deleted
printf("Enter the element to be deleted: ");
scanf("%d", &element);
// Call function to delete the element
deleteElement(arr, &n, element);
// Output: array after deletion
printf("Array after deletion: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Output: -
Enter the number of elements in the sorted array: 5
Enter the elements of the sorted array: 10 20 30 40 50
Enter the element to be deleted: 20
Array after deletion: 10 30 40 50

Question 3: - Write a program to merge two given sorted arrays.

Code: -
#include <stdio.h>

void mergeArrays(int arr1[], int n1, int arr2[], int n2, int mergedArr[]) {
int i = 0, j = 0, k = 0;

// Merge the two arrays until one of them is fully traversed


while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
mergedArr[k++] = arr1[i++];
} else {
mergedArr[k++] = arr2[j++];
}
}

// Copy the remaining elements of arr1, if any


while (i < n1) {
mergedArr[k++] = arr1[i++];
}

// Copy the remaining elements of arr2, if any


while (j < n2) {
mergedArr[k++] = arr2[j++];
}
}

int main() {
int n1, n2;

// Input: size of the first sorted array


printf("Enter the number of elements in the first sorted array: ");
scanf("%d", &n1);

int arr1[n1]; // Declare the first sorted array

// Input: elements of the first sorted array


printf("Enter the elements of the first sorted array: ");
for (int i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}

// Input: size of the second sorted array


printf("Enter the number of elements in the second sorted array: ");
scanf("%d", &n2);

int arr2[n2]; // Declare the second sorted array

// Input: elements of the second sorted array


printf("Enter the elements of the second sorted array: ");
for (int i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}

// Declare an array to hold the merged result


int mergedArr[n1 + n2];

// Call function to merge the arrays


mergeArrays(arr1, n1, arr2, n2, mergedArr);

// Output: merged sorted array


printf("Merged sorted array: ");
for (int i = 0; i < n1 + n2; i++) {
printf("%d ", mergedArr[i]);
}

return 0;
}

Output: -
Enter the number of elements in the first sorted array: 5
Enter the elements of the first sorted array: 1 3 5 7 9
Enter the number of elements in the second sorted array: 5
Enter the elements of the second sorted array: 2 4 6 8 10
Merged sorted array: 1 2 3 4 5 6 7 8 9 10
Question 4: - Write a program to implement Stack using array, also show overflow and
underflow in respective push and pop operations.

Code: -
#include <stdio.h>
#define MAX 5

int stack[MAX], top = -1;

// Push operation
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
} else {
stack[++top] = value;
printf("Pushed %d into the stack.\n", value);
}
}

// Pop operation
void pop() {
if (top == -1) {
printf("Stack Underflow!\n");
} else {
printf("Popped %d from the stack.\n", stack[top--]);
}
}

// Display stack
void display() {
if (top == -1) {
printf("Stack is empty!\n");
} else {
printf("Stack elements are: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}

int main() {
push(10);
push(20);
push(30);
push(40);
push(50);
push(60); // Overflow case

display();

pop();
pop();
pop();
pop();
pop();
pop(); // Underflow case

return 0;
}

Output: -
Pushed 10 into the stack.
Pushed 20 into the stack.
Pushed 30 into the stack.
Pushed 40 into the stack.
Pushed 50 into the stack.
Stack Overflow!
Stack elements are: 50 40 30 20 10
Popped 50 from the stack.
Popped 40 from the stack.
Popped 30 from the stack.
Popped 20 from the stack.
Popped 10 from the stack.
Stack Underflow!

Question 5: - Write a program to implement Queue using array, which shows insertion and
deletion operations.

Code: -
#include <stdio.h>
#define MAX 5

int queue[MAX], front = -1, rear = -1;

// Insertion (Enqueue)
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue Overflow!\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("Inserted %d into the queue.\n", value);
}
}

// Deletion (Dequeue)
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow!\n");
} else {
printf("Deleted %d from the queue.\n", queue[front++]);
}
}

// Display Queue
void display() {
if (front == -1 || front > rear) {
printf("Queue is empty!\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
enqueue(60); // Overflow case

display();

dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
dequeue(); // Underflow case

return 0;
}

Output: -
Inserted 10 into the queue.
Inserted 20 into the queue.
Inserted 30 into the queue.
Inserted 40 into the queue.
Inserted 50 into the queue.
Queue Overflow!
Queue elements are: 10 20 30 40 50
Deleted 10 from the queue.
Deleted 20 from the queue.
Deleted 30 from the queue.
Deleted 40 from the queue.
Deleted 50 from the queue.
Queue Underflow!
Question 6: - Write a program to implement Circular Queue using an array, which shows
insertion and deletion operations.

Code: -
#include <stdio.h>
#define MAX 5

int circularQueue[MAX], front = -1, rear = -1;

// Insertion (Enqueue)
void enqueue(int value) {
if ((front == 0 && rear == MAX - 1) || (rear == (front - 1) % (MAX - 1))) {
printf("Circular Queue Overflow!\n");
} else {
if (front == -1) front = rear = 0;
else if (rear == MAX - 1 && front != 0) rear = 0;
else rear++;
circularQueue[rear] = value;
printf("Inserted %d into the circular queue.\n", value);
}
}

// Deletion (Dequeue)
void dequeue() {
if (front == -1) {
printf("Circular Queue Underflow!\n");
} else {
printf("Deleted %d from the circular queue.\n", circularQueue[front]);
if (front == rear) front = rear = -1;
else if (front == MAX - 1) front = 0;
else front++;
}
}

// Display Circular Queue


void display() {
if (front == -1) {
printf("Circular Queue is empty!\n");
} else {
printf("Circular Queue elements are: ");
if (rear >= front) {
for (int i = front; i <= rear; i++) {
printf("%d ", circularQueue[i]);
}
} else {
for (int i = front; i < MAX; i++) {
printf("%d ", circularQueue[i]);
}
for (int i = 0; i <= rear; i++) {
printf("%d ", circularQueue[i]);
}
}
printf("\n");
}
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
enqueue(60); // Overflow case

display();

dequeue();
dequeue();
display();

return 0;
}

Output: -
Inserted 10 into the circular queue.
Inserted 20 into the circular queue.
Inserted 30 into the circular queue.
Inserted 40 into the circular queue.
Inserted 50 into the circular queue.
Circular Queue Overflow!
Circular Queue elements are: 10 20 30 40 50
Deleted 10 from the circular queue.
Deleted 20 from the circular queue.
Circular Queue elements are: 30 40 50

Question 7: - Write a program to implement Linear Linked List, showing all the
operations, like creation, display, insertion, deletion and searching.

Code: -
#include <stdio.h>
#include <stdlib.h>

// Define a node structure


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

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}

// Function to display the linked list


void displayList(struct Node* head) {
struct Node* temp = head;
if (!head) {
printf("List is empty!\n");
return;
}
printf("Linked list: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Function to insert at the beginning


void insertAtBeginning(struct Node** head, int value) {
struct Node* newNode = createNode(value);
newNode->next = *head;
*head = newNode;
printf("Inserted %d at the beginning.\n", value);
}

// Function to insert at the end


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
printf("Inserted %d at the end.\n", value);
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
printf("Inserted %d at the end.\n", value);
}

// Function to delete a node


void deleteNode(struct Node** head, int key) {
struct Node* temp = *head;
struct Node* prev = NULL;
if (temp != NULL && temp->data == key) {
*head = temp->next;
free(temp);
printf("Deleted node with value %d.\n", key);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Node with value %d not found!\n", key);
return;
}
prev->next = temp->next;
free(temp);
printf("Deleted node with value %d.\n", key);
}

// Function to search a node


void searchNode(struct Node* head, int key) {
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == key) {
printf("Node with value %d found.\n", key);
return;
}
temp = temp->next;
}
printf("Node with value %d not found.\n", key);
}

int main() {
struct Node* head = NULL;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtBeginning(&head, 5);
insertAtEnd(&head, 30);

displayList(head);

deleteNode(&head, 20);
displayList(head);

searchNode(head, 10);
searchNode(head, 40);

return 0;
}

Output: -
Inserted 10 at the end.
Inserted 20 at the end.
Inserted 5 at the beginning.
Inserted 30 at the end.
Linked list: 5 -> 10 -> 20 -> 30 -> NULL
Deleted node with value 20.
Linked list: 5 -> 10 -> 30 -> NULL
Node with value 10 found.
Node with value 40 not found.
Question 8: - Write a program to implement Stack using Linked List. Implement Push,
Pop, and display operations.

Code: -
#include <stdio.h>
#include <stdlib.h>

// Define the structure for the node


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

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}

// Function to push an element onto the stack


void push(struct Node** top, int value) {
struct Node* newNode = createNode(value);
newNode->next = *top;
*top = newNode;
printf("Pushed %d onto the stack.\n", value);
}

// Function to pop an element from the stack


void pop(struct Node** top) {
if (*top == NULL) {
printf("Stack Underflow! Stack is empty.\n");
return;
}
struct Node* temp = *top;
*top = (*top)->next;
printf("Popped %d from the stack.\n", temp->data);
free(temp);
}

// Function to display the stack


void displayStack(struct Node* top) {
if (top == NULL) {
printf("Stack is empty!\n");
return;
}
struct Node* temp = top;
printf("Stack elements: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node* top = NULL;

push(&top, 10);
push(&top, 20);
push(&top, 30);

displayStack(top);

pop(&top);
displayStack(top);

return 0;
}

Output: -
Pushed 10 onto the stack.
Pushed 20 onto the stack.
Pushed 30 onto the stack.
Stack elements: 30 -> 20 -> 10 -> NULL
Popped 30 from the stack.
Stack elements: 20 -> 10 -> NULL

Question 9: - Write a program to implement Queue using Linked List. Implement


Insertion, Deletion, and display operations.
Code: -
#include <stdio.h>
#include <stdlib.h>
// Define the structure for the node
struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}

// Enqueue (Insertion)
void enqueue(struct Node** front, struct Node** rear, int value) {
struct Node* newNode = createNode(value);
if (*rear == NULL) {
*front = *rear = newNode;
printf("Inserted %d into the queue.\n", value);
return;
}
(*rear)->next = newNode;
*rear = newNode;
printf("Inserted %d into the queue.\n", value);
}

// Dequeue (Deletion)
void dequeue(struct Node** front, struct Node** rear) {
if (*front == NULL) {
printf("Queue Underflow! Queue is empty.\n");
return;
}
struct Node* temp = *front;
*front = (*front)->next;
printf("Deleted %d from the queue.\n", temp->data);
free(temp);
if (*front == NULL) {
*rear = NULL;
}
}

// Display Queue
void displayQueue(struct Node* front) {
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("NULL\n");
}
int main() {
struct Node* front = NULL;
struct Node* rear = NULL;

enqueue(&front, &rear, 10);


enqueue(&front, &rear, 20);
enqueue(&front, &rear, 30);

displayQueue(front);

dequeue(&front, &rear);
displayQueue(front);

return 0;
}

Output: -
Inserted 10 into the queue.
Inserted 20 into the queue.
Inserted 30 into the queue.
Queue elements: 10 -> 20 -> 30 -> NULL
Deleted 10 from the queue.
Queue elements: 20 -> 30 -> NULL
Question 10: - Write a program to count the number of times an item is present in a linked
list.

Code: -
#include <stdio.h>
#include <stdlib.h>

// Define the structure for the node


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

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the end of the list


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to count occurrences of an item


int countOccurrences(struct Node* head, int key) {
int count = 0;
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == key) {
count++;
}
temp = temp->next;
}
return count;
}

// Display the linked list


void displayList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 10);
insertAtEnd(&head, 30);
insertAtEnd(&head, 10);

displayList(head);

int key = 10;


printf("Element %d occurs %d times in the list.\n", key, countOccurrences(head, key));

return 0;
}

Output: -
10 -> 20 -> 10 -> 30 -> 10 -> NULL
Element 10 occurs 3 times in the list.
Question 11: - Write a program to increment the data part of every node present in a
linked list by 10. Display the data both before incrementation and after.

Code: -
#include <stdio.h>
#include <stdlib.h>

// Define the structure for the node


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

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the end of the list


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to increment each node's data by 10


void incrementData(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
temp->data += 10;
temp = temp->next;
}
}

// Display the linked list


void displayList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);

printf("Before incrementation:\n");
displayList(head);

incrementData(head);

printf("After incrementation:\n");
displayList(head);

return 0;
}

Output: -
Before incrementation:
10 -> 20 -> 30 -> NULL
After incrementation:
20 -> 30 -> 40 -> NULL

Question 12: - Write a program to implement Doubly Linked List, showing all the
operations, like creation, display, insertion, deletion, and searching.

Code: -
#include <stdio.h>
#include <stdlib.h>

// Define the structure for the node


struct Node {
int data;
struct Node* prev;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the end of the list


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}

// Function to display the doubly linked list


void displayList(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty!\n");
return;
}
printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Function to delete a node from the doubly linked list


void deleteNode(struct Node** head, int key) {
struct Node* temp = *head;
if (temp != NULL && temp->data == key) {
*head = temp->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
printf("Deleted node with value %d.\n", key);
return;
}
while (temp != NULL && temp->data != key) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with value %d not found!\n", key);
return;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
printf("Deleted node with value %d.\n", key);
}

// Function to search for an element in the doubly linked list


void searchNode(struct Node* head, int key) {
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == key) {
printf("Node with value %d found.\n", key);
return;
}
temp = temp->next;
}
printf("Node with value %d not found!\n", key);
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
displayList(head);
deleteNode(&head, 20);
displayList(head);
searchNode(head, 30);
searchNode(head, 40);
return 0;
}

Output: -
Doubly Linked List: 10 <-> 20 <-> 30 <-> NULL
Deleted node with value 20.
Doubly Linked List: 10 <-> 30 <-> NULL
Node with value 30 found.
Node with value 40 not found!

Question 13: - Write a program to search for an element using Linear Search.

Code: -
#include <stdio.h>
// Function for Linear Search
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return the index of the found element
}
}
return -1; // Return -1 if the element is not found
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int result = linearSearch(arr, size, key);
if (result != -1) {
printf("Element %d found at index %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}

Output: -
Element 30 found at index 2.
Question 14: - Write a program to search for an element using Binary Search.

Code: -
#include <stdio.h>

// Function for Binary Search


int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid; // Return the index of the found element
}
if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Return -1 if the element is not found
}

int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 40;

int result = binarySearch(arr, size, key);


if (result != -1) {
printf("Element %d found at index %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}

return 0;
}
Output: -
Element 40 found at index 3.
Question 15: - Write a program to sort the given array using Bubble Sort.
Code: -
#include <stdio.h>

// Function to perform Bubble Sort


void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

// Function to display the array


void displayArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Array before sorting:\n");


displayArray(arr, size);

bubbleSort(arr, size);

printf("Array after sorting:\n");


displayArray(arr, size);

return 0;
}

Output: -
Array before sorting:
64 34 25 12 22 11 90
Array after sorting:
11 12 22 25 34 64 90

You might also like