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

Lab - 3 (Fouzia)

lab task

Uploaded by

imransarker.web
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)
30 views8 pages

Lab - 3 (Fouzia)

lab task

Uploaded by

imransarker.web
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/ 8

Lab no: 02

Lab Name: Compare time complexity of merge sort and quick sort.

Merge Sort: Merge sort is a sorting algorithm that follows the divide-and-conquer
strategy. It breaks down the unsorted list into smaller sublists, sorts each sublist
individually, and then merges them back together to produce a sorted list. It repeatedly
divides the list into halves until each sublist contains only one element, then merges
adjacent sublists while maintaining sorted order. This process continues until the entire
list is sorted.
Algorithm: MERGE-SORT(A, p, r)
if p < r
then q ← (p + r)/2
MERGE-SORT(A, p, q)
MERGE-SORT(A, q + 1, r)
MERGE(A, p, q, r)

Merge algorithm:
MERGE(A, p, q, r)
1. Compute n1 and n2
2. Copy the first n1 elements into
L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]
3. L[n1 + 1] ← ; R[n2 + 1] ←
4. i ← 1; j ← 1
5. for k ← p to r
6. do if L[ i ] ≤ R[ j ]
7. then A[k] ← L[ i ]
8. i ←i + 1
9. else A[k] ← R[ j ]
10. j←j+1

Code:
#include <iostream>
#include <chrono> // For timing

using namespace std;


using namespace std::chrono;

// Merge two subarrays L and R into arr


void merge(int arr[], int p, int q, int r)
{
// Create L ← A[p..q] and R ← A[q+1..r]
int n1 = q - p + 1;
int n2 = r - q;

int L[n1], R[n2];

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


L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
R[j] = arr[q + 1 + j];

// Maintain current index of sub-arrays and main array


int i, j, k;
i = 0;
j = 0;
k = p;

// Until we reach either end of either L or R, pick larger among


// elements L and R and place them in the correct position at A[p..r]
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

// When we run out of elements in either L or R,


// pick up the remaining elements and put in A[p..r]
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}

while (j < n2)


{
arr[k] = R[j];
j++;
k++;
}
}

// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int p, int r)
{
if (p < r)
{
// m is the point where the array is divided into two subarrays
int m = (p + r) / 2;

mergeSort(arr, p, m);
mergeSort(arr, m + 1, r);

// Merge the sorted subarrays


merge(arr, p, m, r);
}
}

// Print the array


void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

// main method
int main()
{

const int n = 100000; // Change this for different array sizes


int arr[n];
for (int i = 0; i < n; ++i)
arr[i] =(rand() % 10000);

// Measure execution time


auto start = high_resolution_clock::now();

//Call mersort

mergeSort(arr, 0, n - 1);

auto stop = high_resolution_clock::now();


auto duration = duration_cast<nanoseconds>(stop - start);

//cout << "Sorted array: \n";


//printArray(arr, n);

cout << "Time taken by merge sort: "


<< duration.count() << " nanoseconds" << endl;
return 0;
}

Output for above code:


Quick Sort: Quicksort is a sorting algorithm that also employs the divide-and-conquer
approach. It selects a 'pivot' element from the list and partitions the other elements into
two sublists: one with elements less than the pivot and one with elements greater than
the pivot. It then recursively sorts the sublists. The key operation is the partitioning step,
where elements are rearranged such that all elements less than the pivot are on one
side and all elements greater than the pivot are on the other. This process repeats until
the entire list is sorted. Quicksort is known for its efficiency and is widely used in
practice.

Algorithm:
QUICKSORT(A, p, r)
if p < r
then q  PARTITION(A, p, r)
QUICKSORT (A, p, q)
QUICKSORT (A, q+1, r)

Partition algorithm:
PARTITION (A, p, r)
1. x  A[p]
2. i  p – 1
3. j  r + 1
4. while TRUE
5. do repeat j  j – 1
6. until A[j] ≤ x
7. do repeat i  i + 1
8. until A[i] ≥ x
9. if i < j
10. then exchange A[i]  A[j]
11. else return j

Quick sort code:

#include <iostream>
#include <chrono> // For timing

using namespace std;


using namespace std::chrono;

// function to swap elements


void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}

// function to print the array


void printArray(int array[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}

// function to rearrange array (find the partition point)


int partition(int array[], int low, int high)
{

// select the rightmost element as pivot


int pivot = array[high];

// pointer for greater element


int i = (low - 1);

// traverse each element of the array


// compare them with the pivot
for (int j = low; j < high; j++)
{
if (array[j] <= pivot)
{

// if element smaller than pivot is found


// swap it with the greater element pointed by i
i++;

// swap element at i with element at j


swap(&array[i], &array[j]);
}
}

// swap pivot with the greater element at i


swap(&array[i + 1], &array[high]);
// return the partition point
return (i + 1);
}

void quickSort(int array[], int low, int high)


{
if (low < high)
{
// find the pivot element such that
// elements smaller than pivot are on left of pivot
// elements greater than pivot are on righ of pivot
int pi = partition(array, low, high);

// recursive call on the left of pivot


quickSort(array, low, pi - 1);

// recursive call on the right of pivot


quickSort(array, pi + 1, high);
}
}

// Main method
int main()
{
const int n = 100000; // Change this for different array sizes
int data[n];
for (int i = 0; i < n; ++i)
data[i] =(rand() % 100000);

// Measure execution time


auto start = high_resolution_clock::now();

// perform quicksort on data


quickSort(data, 0, n - 1);

auto stop = high_resolution_clock::now();


auto duration = duration_cast<nanoseconds>(stop - start);

//cout << "Sorted array in ascending order: \n";


//printArray(data, n);

cout << "Time taken by Quick sort: "


<< duration.count() << " nanoseconds" << endl;
}Output for above code:

Analyzing the merge sort and quick sort based on different input size:

Size Merge sort (ns) Quick Sort (ns)


100 0 0
1000 0 0
10000 0 0
100000 46938200 31539200
1000000 overflow overflow
10000000 overflow overflow
100000000 overflow overflow

Conclusion:
Merge sort has consistent performance with a time complexity of O(n log n) but requires extra
space. It's stable and suitable for large datasets. Quicksort, with an average time complexity of
O(n log n), is faster in practice due to its in-place sorting but can degrade to O(n^2) in the worst
case. It's better for smaller datasets and when space efficiency matters.

You might also like