You are on page 1of 10

Lab Report 03

Rajshahi University of Engineering & Technology

Department of Computer Science and Engineering

CSE 2202

Sessional Based on CSE 2201

Lab Report 03

Name of the Experiment: Complexity analysis of Finding Maximum and Minimum Value
[Max-Min Algorithm] and various sorting algorithms [Quick Sort and Merge Sort].
Date of the Experiment: 10/10/2022
Date of Submission: 17/10/2022

Submitted to: Submitted by:


Name: Rizoan Toufiq, Name: Rukaiya Haque
Assistant Professor, Roll: 1903120
Department of CSE, Section: B
RUET 2nd Year Odd Semester

1|P age
Lab Report 03

INDEX

Sl.No Topic Page No.

1. 3

Title

2. Objective 3

3. Theory 3-5

4. Table 6

5. Graph 6

6. Discussion 7

2|P age
Lab Report 03

MACHINE CONFIGURATION:

Processor Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz 1.19 GHz

Installed RAM 12.0 GB (11.8 GB usable)

System type 64-bit operating system, x64-based processor

Title: Complexity analysis of Finding Maximum and Minimum Value [Max-Min Algorithm] and various
sorting algorithms [Quick Sort and Merge Sort].

Objective:

• Complexity analysis of Merge Sort Algorithm.


• Complexity analysis of Quick Sort Algorithm.

Theory:

Merge Sort:

The Merge Sort algorithm is a sorting algorithm that is based on the Divide and Conquer paradigm. In this
algorithm, the array is initially divided into two equal halves and then they are combined in a sorted manner.

Algorithm:

step 1: start

step 2: declare array and left, right, mid variable

step 3: perform merge function.


if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)

step 4: Stop

Follow the steps below to solve the problem:

3|P age
Lab Report 03

MergeSort(arr[], l, r)
If r > l

• Find the middle point to divide the array into two halves:

• middle m = l + (r – l)/2

• Call mergeSort for first half:

• Call mergeSort(arr, l, m)

• Call mergeSort for second half:

• Call mergeSort(arr, m + 1, r)

• Merge the two halves sorted in steps 2 and 3:

• Call merge(arr, l, m, r)

Complexity: Best Case Time Complexity: O(n*log n) .

Worst Case Time Complexity: O(n*log n).

Average Time Complexity: O(n*log n) .

The time complexity of MergeSort is O(n*Log n).

Quick Sort:

QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array
around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

• Always pick the first element as a pivot.

• Always pick the last element as a pivot (implemented below)

• Pick a random element as a pivot.

• Pick median as the pivot.

The key process in quickSort is a partition(). The target of partitions is, given an array and an element x of an
array as the pivot, put x at its correct position in a sorted array and put all smaller elements (smaller than x)
before x, and put all greater elements (greater than x) after x. All this should be done in linear time.

Pseudo Code for recursive QuickSort function:

/* low –> Starting index, high –> Ending index */


4|P age
Lab Report 03

quickSort(arr[], low, high) {

if (low < high) {

/* pi is partitioning index, arr[pi] is now at right place */

pi = partition(arr, low, high);

quickSort(arr, low, pi – 1); // Before pi

quickSort(arr, pi + 1, high); // After pi

Pseudo code for partition()

/* This function takes last element as pivot, places the pivot element at its correct position in sorted array, and
places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */

partition (arr[], low, high)


{
// pivot (Element to be placed at right position)
pivot = arr[high];

i = (low – 1) // Index of smaller element and indicates the


// right position of pivot found so far

for (j = low; j <= high- 1; j++){

// If current element is smaller than the pivot


if (arr[j] < pivot){
i++; // increment index of smaller element
swap arr[i] and arr[j]
}
}
swap arr[i + 1] and arr[high])
return (i + 1)
}

Complexity:

5|P age
Lab Report 03

Best Case Complexity: Quicksort’s best-case time complexity is O (n*logn)

Average Case Complexity: Quicksort’s average case time complexity is O(n*logn)

Worst Case Complexity: The worst-case time complexity of quicksort is O (n2)

Data Table:

Quick Sort:

No of Data. Quick Sort


25000 0.008078
50000 0.008027
75000 0.007905
100000 0.016054
125000 0.015929
150000 0.015986
175000 0.024066
200000 0.031974
225000 0.031987

6|P age
Lab Report 03

Merge Sort:

No of Data. Merge Sort


25000 0.00399
50000 0.009044
75000 0.007905
100000 0.016928
125000 0.021853
150000 0.025139
175000 0.032816
200000 0.035881
225000 0.030448

7|P age
Lab Report 03

Graph:

Quick Sort:

8|P age
Lab Report 03

Merge Sort:

9|P age
Lab Report 03

Discussion:

In this lab, we have analysis the complexity of quick sort and merge sort. The main difference between
quicksort and merge sort is that the quicksort sorts the elements by comparing each element with an element
called a pivot while merge sort divides the array into two subarrays again and again until one element is left.
Sorting is the method of arranging data in a particular order.

Merge sort and quick sort are suitable for larger input sizes but quick sort perform slow .

From our graphs and experiment Quick sort algorithm is slightly faster than Merge sort algorithm and largely
faster than the Insertion sort algorithm.

10 | P a g e

You might also like