0% found this document useful (0 votes)
11 views18 pages

Lab Manual Dsa

This document is a lab manual for a Data Structure course, detailing various array operations including insertion, deletion, searching (linear and binary), and sorting algorithms (selection, bubble, merge, quick, and heap sort). Each section includes C++ code examples and their expected outputs. The manual serves as a practical guide for students in the Department of Information Technology.
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)
11 views18 pages

Lab Manual Dsa

This document is a lab manual for a Data Structure course, detailing various array operations including insertion, deletion, searching (linear and binary), and sorting algorithms (selection, bubble, merge, quick, and heap sort). Each section includes C++ code examples and their expected outputs. The manual serves as a practical guide for students in the Department of Information Technology.
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/ 18

2024

DATA STRUCTURE
LAB MANUAL

DEPTT. OF INFORMATION TECHNOLOGY


SECTION-A

SAYYADA AYESHA BSIT-2023-009


MALIK AYAN AHMED BSIT-2023-044
SHAHMEER ABBASI BSIT-2023-058
TAYYABA BALOCH BSIT-2023-012

ASSIGNED BY: MA’AM UFFAQUE


DATE: 04-JUNE-2024
Insertion at Beginning:
#include <iostream>

using namespace std;

int main() {

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

int n = 5;

cout << "Original Array: ";

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

cout << arr[i] << " ";

cout << endl;

int element_to_add = 5;

for (int i = n; i > 0; i--) {

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

arr[0] = element_to_add;

cout << "Updated Array: ";

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

cout << arr[i] << " ";

cout << endl;

Output: Original Array: 10 20 30 40 50

Updated Array: 5 10 20 30 40 50
Insertion at Ending:
#include <iostream>

using namespace std;

int* insertAtEnd(int arr[], int& n, int x) {

arr[n] = x; // Insert 'x' at the end

n++; // Increase the size of the array

return arr;

int main() {

int arr[100] = {1, 2, 3, 4, 5};

int n = 5;

cout << "Original array: ";

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

cout << arr[i] << " ";

cout << endl;

int x = 10;

insertAtEnd(arr, n, x);

cout << "Updated array after inserting " << x << " at the end: ";

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

cout << arr[i] << " ";

cout << endl;

return 0;

Output: Original array: 1 2 3 4 5


Updated array: 1 2 3 4 5 10
Insertion at Specific Position:
#include <iostream>

using namespace std;

int* insertX(int n, int arr[], int x, int pos) {

n++;

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

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

arr[pos - 1] = x;

return arr;

int main() {

int arr[100] = {0}; // Initial array of size 10

int n = 10; // Size of initial array

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

arr[i] = i + 1;

int x = 50;

int pos = 5;

insertX(n, arr, x, pos);

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

cout << arr[i] << " ";

cout << endl;

return 0;

Output: Original array: 1 2 3 4 5 6 7 8 9 10


Updated array: 1 2 3 4 50 5 6 7 8 9 10
Traversal of Array:
#include <iostream>
using namespace std;
int main() {
int arr[] = {2, -1, 5, 6, 0, -3};
cout << "Traverse using array's data type: ";
for (int x : arr) {
cout << x << " ";
}
cout << "\nTraverse using auto keyword: ";
for (auto x : arr) {
cout << x << " ";
}
return 0;
}

Output:

Traverse using array's data type: 2 -1 5 6 0 -3


Traverse using auto keyword: 2 -1 5 6 0 -3
Deletation in Array:
#include <iostream>
using namespace std;
void deleteElement(int arr[], int& n, int pos) {
if (pos < 0 || pos >= n) {
cout << "Invalid index. Cannot delete element.\n";
return;
}
for (int i = pos; i < n - 1; ++i) {
arr[i] = arr[i + 1];
}
--n;
cout << "Element at index " << pos << " deleted successfully.\n";
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int posToDelete = 2;
deleteElement(arr, n, posToDelete);
cout << "Updated array: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Output: Element at index 2 deleted successfully. Updated Array: 1 2 4
Linear Search:
#include <iostream>

using namespace std;

int search(int arr[], int n, int x) {

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

if (arr[i] == x)

return i;

return -1;

int main() {

int arr[] = {2, 3, 4, 10, 40};

int x = 10;

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

int result = search(arr, n, x);

if (result == -1)

cout << "Element is not present in the array";

else

cout << "Element is present at index " << result;

return 0;

Output: Element is present at index 3


Binary Search:
#include <iostream>

using namespace std;

int binarySearch(int arr[], int l, int r, int x) {

if (r >= l) {

int mid = l + (r - l) / 2;

if (arr[mid] == x) return mid;

if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);

return -1;

int main(void) {

int arr[] = {2, 3, 4, 10, 40};

int x = 10;

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

int result = binarySearch(arr, 0, n - 1, x);

(result == -1)

? cout << "Element is not present in array"

cout << "Element is present at index " << result;

return 0;

Output: Element is present at index 3


Sorting (Ascending order):
#include <iostream>

#include <vector>

using namespace std;

void sortArr(int arr[], int n, int minVal, int maxVal) {

int m = maxVal - minVal + 1;

vector<int> count(m, 0);

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

count[arr[i] - minVal]++;

for (int i = 0; i < m; i++) {

for (int j = 0; j < count[i]; j++) {

cout << (i + minVal) << " ";

int main() {

int arr[] = {10, 10, 1, 4, 4, 100, 0};

int minVal = 0, maxVal = 100;

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

cout << "Before sorting, the array is: ";

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

cout << arr[i] << " ";

cout << endl;

cout << "After sorting, the array is: ";

sortArr(arr, n, minVal, maxVal);

cout << endl;

return 0;

Output: Before sorting, the array is: a d r b c e

After sorting, the array is: a b c d e r


Sorting (Descending order):
#include <iostream>

using namespace std;

void swap(int& a, int& b) {

int temp = a;

a = b;

b = temp;

void descending_sort(int arr[], size_t len) {

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

for (size_t j = 0; j < len - i - 1; ++j) {

if (arr[j] < arr[j + 1]) {

swap(arr[j], arr[j + 1]);

int main() {

int arr[] = {8, -901, 89, 0, 18, 791, -87};

size_t len = sizeof(arr) / sizeof(arr[0]);

descending_sort(arr, len);

cout << "Descendingly sorted array: ";

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

cout << arr[i] << " ";

cout << endl;

return 0;

Output: Before sorting, the array is: 8, -901, 89, 0, 18, 791, -87

Descendingly sorted array: 791 89 18 8 0 -87 -901


Selection Sort:
#include <iostream>

void selectionSort(int arr[], int n) {

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

int minIndex = i;

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

if (arr[j] < arr[minIndex]) {

minIndex = j;

std::swap(arr[i], arr[minIndex]);

int main() {

int arr[] = {64, 25, 12, 22, 11};

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

std::cout << "Original array: ";

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

std::cout << arr[i] << " ";

selectionSort(arr, n);

std::cout << "\nSorted array: ";

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

std::cout << arr[i] << " ";

return 0;

Output: Original array: 64 25 12 22 11

Sorted array: 11 12 22 25 64
Bubble Sort:
#include <iostream>

void bubbleSort(int arr[], int n) {

bool swapped;

do {

swapped = false;

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

if (arr[i] > arr[i + 1]) {

std::swap(arr[i], arr[i + 1]);

swapped = true;

} }

n - -;

} while (swapped);

void printArray(int arr[], int size) {

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

std::cout << arr[i] << " ";

std::cout << std::endl;}

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

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

std::cout << "Original array: ";

printArray(arr, n);

bubbleSort(arr, n);

std::cout << "Sorted array: ";

printArray(arr, n);

return 0;

Output: 64 34 25 12 22 11 90

11 12 22 25 34 64 90
Merge Sort:
#include <iostream>

using namespace std;

void merge(int arr[], int l, int m, int r) {

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

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

L[i] = arr[l + i];

for (int j = 0; j < n2; j++)

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

int i = 0;

int j = 0;

int k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++; }

k++; }

while (i < n1) {

arr[k] = L[i];

i++;

k++; }

while (j < n2) {

arr[k] = R[j];

j++;

k++; } }
void mergeSort(int arr[], int l, int r) {

if (l >= r) {

return;

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

void printArray(int A[], int size) {

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

cout << A[i] << " ";

cout << endl;

int main() {

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

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

cout << "Given array is \n";

printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

cout << "\nSorted array is \n";

printArray(arr, arr_size);

return 0;

Output: Given array is


12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
Quick Sort:
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
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);
}
}
void printArray(int arr[], int size) {
int i;
for (i=0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

Output:
Given array is
{10, 7, 8, 9, 1, 5}

Sorted array is
1 5 7 8 9 10
Heap Sort:
#include <iostream>

void heapify(int arr[], int n, int i) {

int largest = i;

int l = 2 * i + 1;

int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest])

largest = l;

if (r < n && arr[r] > arr[largest])

largest = r;

if (largest != i) {

std::swap(arr[i], arr[largest]);

heapify(arr, n, largest);

void heapSort(int arr[], int n) {

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);

for (int i = n - 1; i >= 0; i--) {

std::swap(arr[0], arr[i]);

heapify(arr, i, 0);

}
void printArray(int arr[], int n) {

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

std::cout << arr[i] << " ";

std::cout << "\\n";

int main() {

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

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

heapSort(arr, n);

std::cout << "Sorted array is ";

printArray(arr, n);

Output: Given array is


12, 11, 13, 5, 6, 7}
Sorted array is
5 6 7 11 12 13

You might also like