data_structure_1_fyit
data_structure_1_fyit
A : Create Array and perform opera ons like searching, sor ng and reversing the elements.
Program
#include <iostream>
arr[i] = arr[j];
arr[j] = temp;
arr[size - 1 - i] = temp;
if (arr[i] == key) {
return i;
return -1;
int main() {
int size;
int arr[size];
cout << "Enter " << size << " elements: ";
displayArray(arr, size);
int key;
if (index != -1) {
cout << "Element " << key << " found at index " << index << ".\n";
} else {
cout << "Element " << key << " not found.\n";
sortArray(arr, size);
displayArray(arr, size);
reverseArray(arr, size);
displayArray(arr, size);
return 0;
Output
Enter 5 elements: 1
Original Array: 1 2 3 4 5
1.b Read the two arrays from the user and merge them and display the elements in sorted order.
Program
#include <iostream>
using namespace std;
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int merged[], int &mergedSize) {
merged[i] = arr1[i];
merged[size1 + i] = arr2[i];
arr[i] = arr[j];
arr[j] = temp;
}
int main() {
int arr1[size1];
cout << "Enter " << size1 << " elements for the first array: ";
int arr2[size2];
cout << "Enter " << size2 << " elements for the second array: ";
int merged[mergedSize];
sortArray(merged, mergedSize);
displayArray(merged, mergedSize);
return 0;
Output
Enter the size of the first array: 4
34
1.c Write a program to perform the Matrix addi on, Mul plica on and Transpose Opera on
Program
#include <iostream>
}
// Func on to add two matrices
void addMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows, int cols) {
void mul plyMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows1, int cols1, int
cols2) {
result[i][j] = 0;
result[j][i] = matrix[i][j];
}
int main() {
cout << "Enter the number of rows and columns for Matrix 1: ";
int matrix1[10][10];
cout << "Enter the number of rows and columns for Matrix 2: ";
int matrix2[10][10];
// Matrix Addi on
int sum[10][10];
cout << "\nMatrix addi on not possible (dimensions must match)." << endl;
if (cols1 == rows2) {
int product[10][10];
} else {
cout << "\nMatrix mul plica on not possible (columns of Matrix 1 must match rows of Matrix
2)." << endl;
// Transpose of Matrix 1
int transpose1[10][10];
// Transpose of Matrix 2
int transpose2[10][10];
return 0;
2: Searching Techniques
A: Write a program to search the element using sequen al search.
Program
#include <iostream>
if (arr[i] == key) {
int main() {
int arr[size];
cout << "Enter " << size << " elements: ";
if (result != -1) {
cout << "Element " << key << " found at index " << result << "." << endl;
} else {
cout << "Element " << key << " not found in the array." << endl;
return 0;
Output
Program
#include <iostream>
} else {
int main() {
int arr[size];
cout << "Enter " << size << " elements in sorted order: ";
if (result != -1) {
cout << "Element " << key << " found at index " << result << "." << endl;
} else {
cout << "Element " << key << " not found in the array." << endl;
return 0;
Output
3. Sor ng Algorithm
Program
#include <iostream>
arr[j + 1] = temp;
}
int main() {
int size;
int arr[size];
cout << "Enter " << size << " elements: ";
displayArray(arr, size);
bubbleSort(arr, size);
cout << "Sorted Array: ";
displayArray(arr, size);
return 0;
Output
3211
3221
2322
Time Complexity:
Bubble Sort is simple but not very efficient for large datasets.
Introduc on
Selec on Sort is a simple sor ng algorithm that works by repeatedly selec ng the smallest (or
largest) element from the unsorted por on of the array and moving it to its correct posi on. It is
intui ve but not very efficient compared to more advanced sor ng algorithms.
Algorithm
4. Expand the sorted por on by one and repeat un l the array is fully sorted.
Pseudocode
less
Copy code
for i = 0 to n-1:
minIndex = i
minIndex = j
swap(arr[i], arr[minIndex])
Time Complexity
Space Complexity
Advantages
Disadvantages
Implementa on in C++
cpp
Copy code
#include <iostream>
int minIndex = i;
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
int main() {
int size;
cout << "Enter the size of the array: ";
int arr[size];
cout << "Enter " << size << " elements: ";
displayArray(arr, size);
displayArray(arr, size);
return 0;
Example Case
Input:
javascript
Copy code
Execu on:
1. Find the smallest element (11) and swap it with the first element (64).
2. Find the next smallest element (12) and swap it with the second element (25).
Output:
javascript
Copy code
1. Small Datasets: Useful for sor ng small lists where simplicity is preferred over efficiency.
2. Embedded Systems: Suitable for systems with memory constraints due to its in-place
sor ng.
Conclusion
Selec on Sort is an easy-to-understand algorithm suitable for educa onal purposes. While its
O(n2)O(n^2)O(n2) me complexity makes it imprac cal for large datasets, its simplicity makes it a
good candidate for small-scale problems or as an introduc on to sor ng algorithms.
Introduc on
Inser on Sort is a simple sor ng algorithm that builds the final sorted array one element at a me. It
is much like sor ng playing cards in hand: you pick up one card at a me and place it in the correct
posi on rela ve to the already sorted por on. It’s efficient for small data sets or nearly sorted data
but inefficient for large data sets.
Algorithm
1. Start with the second element, since the first element is already considered "sorted."
2. Compare the current element with the elements in the sorted por on of the array.
3. Shi the elements of the sorted por on that are greater than the current element to the
right.
4. Insert the current element into the correct posi on in the sorted por on.
less
Copy code
for i = 1 to n-1:
currentElement = arr[i]
j=i-1
arr[j + 1] = arr[j]
j=j-1
arr[j + 1] = currentElement
Time Complexity
Space Complexity
Advantages
2. Efficient for small datasets: For smaller datasets, inser on sort can be faster than more
complex algorithms like merge sort or quicksort.
3. Adap ve: Inser on sort is adap ve, meaning that it performs be er if the data is already
par ally sorted.
4. Stable: It does not change the rela ve order of elements with equal keys.
Disadvantages
2. More comparisons and shi s: For each element, it may require many shi s and
comparisons, which can be me-consuming.
Implementa on in C++
cpp
Copy code
#include <iostream>
int j = i - 1;
// Shi elements of arr[0..i-1] that are greater than currentElement to one posi on ahead
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = currentElement;
}
int main() {
int size;
int arr[size];
cout << "Enter " << size << " elements: ";
displayArray(arr, size);
displayArray(arr, size);
return 0;
Example Case
Input:
javascript
Copy code
Execu on:
o Compare with 64: 64 > 34, so shi 64 to the right and insert 34 in the first posi on.
o Current element = 25
o Compare with 34: 34 > 25, shi 34 to the right and insert 25 in the first posi on.
Output:
javascript
Copy code
1. Small datasets: When the dataset is small, Inser on Sort can outperform more complex
algorithms.
2. Par ally sorted data: Inser on Sort works efficiently when the data is already nearly sorted.
3. Online sor ng: Inser on Sort is ideal for sor ng data in real- me or when data is arriving in a
sequen al manner.
Conclusion
Inser on Sort is a fundamental sor ng algorithm with a simple approach to sor ng elements. While
it is not suitable for large datasets due to its O(n2)O(n^2)O(n2) me complexity, it has several
advantages:
It is adap ve and performs well on par ally sorted data or small datasets.
For these reasons, Inser on Sort is widely used in prac ce for small data or as a part of more
complex algorithms.
4: Stack
4.a : Write a program to implement the concept of Stack with Push, Pop, Display and Exit
opera ons.
Program
#include <iostream>
class Stack {
private:
public:
Stack() {
cout << "Stack Overflow! Cannot push " << value << endl;
} else {
cout << value << " pushed onto stack." << endl;
void pop() {
} else {
cout << arr[top--] << " popped from stack." << endl; // Return the top element and decrease
top
void display() {
if (top == -1) {
} else {
};
int main() {
while (true) {
switch (choice) {
s.push(value);
break;
s.pop();
break;
s.display();
break;
return 0;
default:
cout << "Invalid choice! Please choose between 1-4." << endl;
return 0;
Output
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
Stack elements: 10 20
1. Push
2. Pop
3. Display
4. Exit
2. Pop
3. Display
4. Exit
Exi ng program.
5: Queue
5.1Write a program to implement the concept of Queue with Insert, Delete, Display and Exit
opera ons.
Program
#include <iostream>
class Queue {
private:
public:
Queue() {
front = -1;
rear = -1;
cout << "Queue Overflow! Cannot insert " << value << endl;
} else {
if (front == -1) {
cout << value << " inserted into the queue." << endl;
void deleteElement() {
} else {
cout << arr[front++] << " deleted from the queue." << endl; // Increment front
void display() {
} else {
}
}
};
int main() {
while (true) {
switch (choice) {
q.insert(value);
break;
q.deleteElement();
break;
q.display();
break;
default:
cout << "Invalid choice! Please choose between 1-4." << endl;
return 0;
Output
1. Insert
2. Delete
3. Display
4. Exit
1. Insert
2. Delete
3. Display
4. Exit
1. Insert
2. Delete
3. Display
4. Exit
Queue elements: 10 20
1. Insert
2. Delete
3. Display
4. Exit
1. Insert
2. Delete
3. Display
4. Exit
Queue elements: 20
1. Insert
2. Delete
3. Display
4. Exit
Exi ng program.
Program
#include <iostream>
using namespace std;
class CircularQueue {
private:
public:
CircularQueue(int size) {
capacity = size;
~CircularQueue() {
delete[] arr;
bool isFull() {
bool isEmpty() {
if (isFull()) {
cout << "Queue Overflow! Cannot insert " << value << endl;
} else {
front = 0;
cout << value << " inserted into the queue." << endl;
void dequeue() {
if (isEmpty()) {
} else {
cout << arr[front] << " deleted from the queue." << endl;
} else {
void display() {
if (isEmpty()) {
int i = front;
while (true) {
if (i == rear) break;
i = (i + 1) % capacity;
};
int main() {
int size;
while (true) {
q.enqueue(value);
break;
q.dequeue();
break;
q.display();
break;
return 0;
default:
cout << "Invalid choice! Please choose between 1-4." << endl;
return 0;
Output
1. Enqueue
2. Dequeue
3. Display
4. Exit
1. Enqueue
2. Dequeue
3. Display
4. Exit
1. Enqueue
2. Dequeue
3. Display
4. Exit
Queue elements: 10 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
1. Enqueue
2. Dequeue
3. Display
4. Exit
Queue elements: 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Exi ng program.
_______________________________***End***_________________________________________