0% found this document useful (0 votes)
14 views10 pages

Experiment 7

The document presents an implementation of Quick Sort and Merge Sort algorithms in C++, including functions for swapping elements, partitioning, merging, and measuring execution time. It tests the performance of both sorting algorithms on arrays of varying sizes (1000, 5000, 10000) and provides observations on their efficiency, noting that Quick Sort may perform better on average but can degrade in the worst case, while Merge Sort maintains consistent performance with additional memory overhead. The results indicate that both algorithms exhibit similar performance for smaller inputs, but Quick Sort generally outperforms Merge Sort as input size increases.

Uploaded by

resoce3697
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

Experiment 7

The document presents an implementation of Quick Sort and Merge Sort algorithms in C++, including functions for swapping elements, partitioning, merging, and measuring execution time. It tests the performance of both sorting algorithms on arrays of varying sizes (1000, 5000, 10000) and provides observations on their efficiency, noting that Quick Sort may perform better on average but can degrade in the worst case, while Merge Sort maintains consistent performance with additional memory overhead. The results indicate that both algorithms exhibit similar performance for smaller inputs, but Quick Sort generally outperforms Merge Sort as input size increases.

Uploaded by

resoce3697
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Experiment-7:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <chrono>

// Function to swap two elements (used in Quick Sort)


void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

// Partition function for Quick Sort


int partition(std::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;
}

// Quick Sort function


void quickSort(std::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);
}
}
// Function to merge two halves of an array (used in Merge
Sort)
void merge(std::vector<int>& arr, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;

std::vector<int> L(n1);
std::vector<int> 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, 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;
}
}

// Merge Sort function


void mergeSort(std::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);
}
}

// Utility function to print an array


void printArray(const std::vector<int>& arr) {
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}

// Function to fill a vector with random integers


void fillRandom(std::vector<int>& arr) {
for (auto &val : arr) {
val = rand() % 1000; // Random numbers between 0 and
999
}
}

// Function to measure execution time of sorting algorithms


template<typename Func>
void measureTime(Func func, std::vector<int>& arr, const
std::string& sortType) {
auto start = std::chrono::high_resolution_clock::now();
func(arr, 0, arr.size() - 1);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end - start;
std::cout << sortType << " took " << duration.count() << "
seconds\n";
}

int main() {
std::vector<int> sizes = {1000, 5000, 10000}; // Different
sizes for testing

for (int size : sizes) {


std::vector<int> arr(size);
std::cout << "Array size: " << size << std::endl;

// Test Quick Sort


fillRandom(arr);
std::vector<int> quickSortArr = arr; // Make a copy for
Quick Sort
std::cout << "Testing Quick Sort...\n";
measureTime(quickSort, quickSortArr, "Quick Sort");
// Test Merge Sort
fillRandom(arr);
std::vector<int> mergeSortArr = arr; // Make a copy for
Merge Sort
std::cout << "Testing Merge Sort...\n";
measureTime(mergeSort, mergeSortArr, "Merge Sort");

std::cout << std::endl;


}

return 0;
}

Output:
Array size: 1000

Testing Quick Sort...

Quick Sort took 0.00013861 seconds

Testing Merge Sort...

Merge Sort took 0.00052462 seconds


Array size: 5000

Testing Quick Sort...

Quick Sort took 0.00103917 seconds

Testing Merge Sort...

Merge Sort took 0.00337616 seconds

Array size: 10000

Testing Quick Sort...

Quick Sort took 0.00268408 seconds

Testing Merge Sort...

Merge Sort took 0.00680271 seconds

=== Code Execution Successful ===

Observations:
1. Performance for Different Input Sizes:

o For smaller input sizes (e.g., 1000 elements), both


algorithms will likely have similar performance.
o As input size grows, Quick Sort may perform better on
average due to its lower constant factors, but its
performance can degrade in the worst-case scenario.

o Merge Sort will have consistent O(n log n) performance but


might be slower due to additional memory overhead.

2. Best, Average, and Worst Cases:

o Quick Sort performs well on average but can degrade to


O(n²) in the worst case (e.g., when the pivot is always the
smallest or largest element).

o Merge Sort provides stable O(n log n) time complexity, but


it uses extra space proportional to the array size.

By running this program, you will be able to observe the execution


times for different sorting algorithms across various input sizes and
gain insight into their performance characteristics.

You might also like