SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
Title: Exp.-1: Sorting and Searching Algorithms
Write C++ program to sort given data elements in ascending order using
Problem bubble sort, quick sort, and merge sort. Search for any element in given data
Statement set using linear and binary search.
Programmer Name : Satyam Vijaykumar Sonawane
Batch : S.Y. 06 [G6]
PROGRAM CODE :
// Array sorting
#include <iostream>
using namespace std;
const int MAX = 100;
void printArray(int arr[], int n) {
cout << "Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << "\n";
}
// ---------------- Bubble Sort ----------------
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
cout << "Bubble Sort applied.\n";
DSA_LAB_2025-26: Program input output 1
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
// ---------------- Insertion Sort ----------------
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key)
arr[j + 1] = arr[j--];
arr[j + 1] = key;
}
cout << "Insertion Sort applied.\n";
}
// ---------------- Selection Sort ----------------
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[minIdx])
minIdx = j;
swap(arr[i], arr[minIdx]);
}
cout << "Selection Sort applied.\n";
}
// ---------------- Quick Sort ----------------
int partition(int arr[], int low, int high) {
int pivot = arr[high], i = low - 1;
for (int j = low; j < high; j++)
if (arr[j] < pivot)
swap(arr[++i], arr[j]);
DSA_LAB_2025-26: Program input output 2
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
swap(arr[i + 1], arr[high]);
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);
}
}
// ---------------- Merge Sort ----------------
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1, n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int i = 0; i < n2; i++) R[i] = arr[m + 1 + i];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
DSA_LAB_2025-26: Program input output 3
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
}
}
// ---------------- Linear Search ----------------
void linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++)
if (arr[i] == key) {
cout << "Element found at index " << i << ".\n";
return;
}
cout << "Element not found.\n";
}
// ---------------- Binary Search ----------------
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
// ---------------- Main Program ----------------
int main() {
int arr[MAX], n, choice, key;
DSA_LAB_2025-26: Program input output 4
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
cout << "Enter size of array: ";
cin >> n;
cout << "Enter array elements:\n";
for (int i = 0; i < n; i++)
cin >> arr[i];
do {
cout << "\n----- MENU -----\n";
cout << "1. Bubble Sort\n";
cout << "2. Insertion Sort\n";
cout << "3. Selection Sort\n";
cout << "4. Quick Sort\n";
cout << "5. Merge Sort\n";
cout << "6. Linear Search\n";
cout << "7. Binary Search\n";
cout << "8. Display Array\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
int tempArr[MAX]; // for sorting without altering original
copy(arr, arr + n, tempArr);
switch (choice) {
case 1:
bubbleSort(tempArr, n);
printArray(tempArr, n);
break;
case 2:
insertionSort(tempArr, n);
printArray(tempArr, n);
break;
DSA_LAB_2025-26: Program input output 5
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
case 3:
selectionSort(tempArr, n);
printArray(tempArr, n);
break;
case 4:
quickSort(tempArr, 0, n - 1);
cout << "Quick Sort applied.\n";
printArray(tempArr, n);
break;
case 5:
mergeSort(tempArr, 0, n - 1);
cout << "Merge Sort applied.\n";
printArray(tempArr, n);
break;
case 6:
cout << "Enter element to search: ";
cin >> key;
linearSearch(arr, n, key);
break;
case 7:
mergeSort(tempArr, 0, n - 1); // Binary search needs sorted array
cout << "Enter element to search: ";
cin >> key;
int index;
index = binarySearch(tempArr, n, key);
if (index != -1)
cout << "Element found at index " << index << " (in sorted array).\n";
else
cout << "Element not found.\n";
DSA_LAB_2025-26: Program input output 6
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
break;
case 8:
printArray(arr, n);
break;
case 0:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 0);
return 0;
}
OUTPUT :
PS C:\C++ Coding\.vscode> cd "c:\C++ Coding\.vscode\" ; if ($?) { g++
program1.cpp -o program1 } ; if ($?) { .\program1 }
Enter size of array: 6
Enter array elements:
1 5 7 19 44 89
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Linear Search
DSA_LAB_2025-26: Program input output 7
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 1
Bubble Sort applied.
Array: 1 5 7 19 44 89
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Linear Search
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 2
Insertion Sort applied.
Array: 1 5 7 19 44 89
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Linear Search
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 3
Selection Sort applied.
Array: 1 5 7 19 44 89
DSA_LAB_2025-26: Program input output 8
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Linear Search
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 4
Quick Sort applied.
Array: 1 5 7 19 44 89
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Linear Search
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 5
Merge Sort applied.
Array: 1 5 7 19 44 89
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
DSA_LAB_2025-26: Program input output 9
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
5. Merge Sort
6. Linear Search
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 6
Enter element to search: 19
Element found at index 3.
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Linear Search
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 7
Enter element to search: 44
Element found at index 4 (in sorted array).
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Linear Search
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 8
DSA_LAB_2025-26: Program input output 10
SCTR’s PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication Engineering
ASSESMENT YEAR: 2025-2026 CLASS: SY
SUBJECT: DATA STRUCTURES AND ALGORITHMS
EXPT No: LAB Ref: SY/2025-26/ Starting date:
Roll No: Submission date:
Array: 1 5 7 19 44 89
----- MENU -----
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Linear Search
7. Binary Search
8. Display Array
0. Exit
Enter your choice: 0
Exiting program.
PS C:\C++ Coding\.vscode>
Note: copy code and then program output (No screen shots) for all cases here
DSA_LAB_2025-26: Program input output 11