0% found this document useful (0 votes)
0 views8 pages

Data Structures and Algorithms

This document outlines a lab project focused on measuring the performance of five sorting algorithms: Bubble Sort, Selection Sort, Merge Sort, Insertion Sort, and Quick Sort. It includes an introduction to the importance of sorting algorithms, specifications and pseudo-code for each algorithm, and a testing results section that presents performance data across varying input sizes. The project aims to analyze and compare the time complexity of these algorithms to determine their efficiency.
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)
0 views8 pages

Data Structures and Algorithms

This document outlines a lab project focused on measuring the performance of five sorting algorithms: Bubble Sort, Selection Sort, Merge Sort, Insertion Sort, and Quick Sort. It includes an introduction to the importance of sorting algorithms, specifications and pseudo-code for each algorithm, and a testing results section that presents performance data across varying input sizes. The project aims to analyze and compare the time complexity of these algorithms to determine their efficiency.
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/ 8

Data Structures and Algorithms

Lab Project 1: To measure the performance of a function


NAME: SAYED MIZANUR RAHMAN (英雄)
STUDENT ID: 238801235
Major: Software Engineering
Submission Date: 16 October 2024

Introduction
In this section, describe the problem you're trying to solve with sorting algorithms. Provide some
context about sorting algorithms, highlighting the importance of comparing their performance for
various input sizes.

Example:

Sorting algorithms play a crucial role in computer science and software development. Efficient
sorting is critical for optimizing the performance of programs, especially for large datasets. This
project aims to analyze and compare the performance of five popular sorting algorithms: Bubble
Sort, Selection Sort, Merge Sort, Insertion Sort, and Quick Sort, with varying input sizes ranging
from log⁡10N=1\log_{10}N = 1log10N=1 to log⁡10N=6\log_{10}N = 6log10N=6. The goal is to
measure the time complexity of each algorithm and determine their efficiency.

Algorithm Specification
Here, you will describe the sorting algorithms used in your project. Include pseudo-code for
each algorithm and any specifications for the data structures they manipulate.

Example Pseudo-code:

1.Bubble Sort:

for i = 0 to n-1

for j = 0 to n-i-2

if arr[j] > arr[j+1]

swap arr[j] and arr[j+1]


2.Selection Sort:
for i = 0 to n-1

minIndex = i

for j = i+1 to n

if arr[j] < arr[minIndex]

minIndex = j

swap arr[i] and arr[minIndex]

3.Merge Sort:
mergeSort(arr[], l, r)

if l < r

mid = (l+r)/2

mergeSort(arr, l, mid)

mergeSort(arr, mid+1, r)

merge(arr, l, mid, r)

4.Insertion Sort:
for i = 1 to n

key = arr[i]

j = i-1

while j >= 0 and arr[j] > key

arr[j+1] = arr[j]

j = j-1

arr[j+1] = key

5.Quick Sort:
quickSort(arr[], low, high)

if low < high

pivot = partition(arr, low, high)

quickSort(arr, low, pivot-1)

quickSort(arr, pivot+1, high)


Testing Results
Here, you will present the results from running the algorithms. Each test case should include:

1. Purpose: Explain the reason for the test (e.g., to measure performance for large inputs).
2. Expected Results: What you expect based on time complexity.
3. Actual Results: Time taken in nanoseconds, as seen in the filled table.
4. Bug Analysis (if any): If there are any issues or errors, describe them and how you resolved
them.
5. Status: Indicate if the test passed or if further corrections were made.

Example Table:

Log10N
1(N=1) 2(N=5) 3(N=10) 4(N=15) 5(N=20) 6(N=25)
Algorithms
Bubble sort 1000 ns 1000 ns 1600 ns 2400 ns 3800 ns 4600 ns
Selection 900 ns 1000 ns 1500 ns 1800 ns 2600 ns 3100 ns
Sort
Merge Sort 1000ns 2600 ns 4600 ns 7400 ns 9700 ns 12300 ns
Insertion 1100 ns 1000 ns 1200 ns 1700 ns 2100 ns 2400 ns
Sort
Quick Sort 900 ns 1100 ns 1600 ns 1700 ns 2300 ns 2500 ns

Line Chart :
Source Code :

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib> // For rand() and srand()
#include <algorithm> // For generate()

using namespace std;

// Function declarations for sorting algorithms


void bubbleSort(vector<int>& arr);
void selectionSort(vector<int>& arr);
void mergeSort(vector<int>& arr, int l, int r);
void merge(vector<int>& arr, int l, int m, int r);
void insertionSort(vector<int>& arr);
void quickSort(vector<int>& arr, int low, int high);
int partition(vector<int>& arr, int low, int high);

// Time measurement helper function


double measureTime(void (*sortFunc)(vector<int>&), vector<int> arr, int
repetitions) {
clock_t start, end;
double total_time = 0;

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


vector<int> tempArr = arr; // Copy the array for each sort
start = clock();
sortFunc(tempArr);
end = clock();
total_time += double(end - start) / CLOCKS_PER_SEC;
}

// Return average time in seconds, convert to nanoseconds (multiply


by 1e9)
return (total_time / repetitions) * 1e9;
}

int main() {
// Input sizes corresponding to log10N = 1, 2, 3, 4, 5,
vector<int> sizes = {1, 5,10, 15,20,25};

// Number of repetitions for accurate time measurement


int repetitions = 10;

for (int size : sizes) {


cout << "\nTesting with input size: " << size << endl;

// Generate random data


vector<int> arr(size);
srand(time(0)); // Seed for random numbers
generate(arr.begin(), arr.end(), rand);

// Measure performance for each sorting algorithm


double bubbleTime = measureTime(bubbleSort, arr, repetitions);
double selectionTime = measureTime(selectionSort, arr,
repetitions);
double mergeTime = measureTime([](vector<int>& a){ mergeSort(a,
0, a.size() - 1); }, arr, repetitions);
double insertionTime = measureTime(insertionSort, arr,
repetitions);
double quickTime = measureTime([](vector<int>& a){ quickSort(a,
0, a.size() - 1); }, arr, repetitions);

// Print times in nanoseconds


cout << "Bubble Sort: " << bubbleTime << " ns" << endl;
cout << "Selection Sort: " << selectionTime << " ns" << endl;
cout << "Merge Sort: " << mergeTime << " ns" << endl;
cout << "Insertion Sort: " << insertionTime << " ns" << endl;
cout << "Quick Sort: " << quickTime << " ns" << endl;
}

return 0;
}

// Bubble Sort
void bubbleSort(vector<int>& arr) {
int n = arr.size();
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]);
}
}
}
}

// Selection Sort
void selectionSort(vector<int>& arr) {
int n = arr.size();
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[minIdx], arr[i]);
}
}

// Merge Sort
void mergeSort(vector<int>& arr, int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

// Merging function for Merge Sort


void merge(vector<int>& arr, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
vector<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) {
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++;
}
}

// Insertion Sort
void insertionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

// Quick Sort
void quickSort(vector<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);
}
}

// Partition function for Quick Sort


int partition(vector<int>& arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

You might also like