Data Structures and Algorithms
Data Structures and Algorithms
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 log10N=1\log_{10}N = 1log10N=1 to log10N=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
minIndex = i
for j = i+1 to n
minIndex = j
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
arr[j+1] = arr[j]
j = j-1
arr[j+1] = key
5.Quick Sort:
quickSort(arr[], low, high)
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()
int main() {
// Input sizes corresponding to log10N = 1, 2, 3, 4, 5,
vector<int> sizes = {1, 5,10, 15,20,25};
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);
}
}
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++;
}
// 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);
}
}