You are on page 1of 6

AIM:

To write a C++ program to design and implement an algorithm that will find the
top and the least score of students from an online quiz.

ALGORITHM:
1) Find the starting time.
2) Get the elements from the user.
3) Sort it using Merge Sort.
a. MERGE_SORT(arr, beg, end)
i. IF BEG < END
1. SET MID = (BEG + END)/2
2. MERGE_SORT(ARR, BEG, MID)
3. MERGE_SORT(ARR, MID + 1, END)
4. MERGE (ARR, BEG, MID, END)
ii. END OF IF
b. END MERGE_SORT

c. MERGE(INT A[], INT BEG, INT MID, INT END)


i. INT N1 = MID - BEG + 1
ii. INT N2 = END - MID
iii. INT LEFTARRAY[N1], RIGHTARRAY[N2]
iv. FOR (INT I = 0; I < N1; I++)
1. LEFTARRAY[I] = A[BEG + I]
v. FOR (INT J = 0; J < N2; J++)
1. RIGHTARRAY[J] = A[MID + 1 + J]
vi. INT I = 0
vii. INT J = 0
viii. INT K = BEG
ix. WHILE (I < N1 && J < N2)
1. IF(LEFTARRAY[I] <= RIGHTARRAY[J])
a. A[K] = LEFTARRAY[I]
b. I++
2. ELSE
a. A[K] = RIGHTARRAY[J]
b. J++
3. K++
x. WHILE (I<N1)
1. A[K] = LEFTARRAY[I]
2. I++
3. K++
xi. WHILE (J<N2)
1. A[K] = RIGHTARRAY[J]
2. J++
3. K++
d. END MERGE
4) Print the minimum – First element of the sorted array and maximum – Last
element of the sorted array.
5) Find the end time.
6) Calculate the time taken.
7) Repeat the above steps for different types of array input and plot the
variations in the execution time.
8) Also, plot a graph – input vs primary operations.

COMPLEXITY ANALYSIS:
Merge Sort is quite fast and has a time complexity of O(n*log n). Time complexity of
Merge Sort is O(n*Log n) in all the 3 cases (worst, average and best) as merge sort always
divides the array in two halves and takes linear time to merge two halves.

Worst Case Time Complexity [ Big-O ]: O(n*log n) Best


Case Time Complexity [ Big-Ω ]: O(n*log n) Average Time
Complexity [ Big-θ ]: O(n*log n) Space Complexity:

O(n)

CODE:
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;

void merge(int arr[], int p, int q, int r)


{
int n1 = q - p + 1;
int n2 = r - q;

int L[n1], M[n2];

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


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

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

while (i < n1 && j < n2)


{
if (L[i] <= M[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = M[j];
j++;
}
k++;
}

while (i < n1)


{
arr[k] = L[i];
i++;
k++;
}

while (j < n2)


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

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

void func()
{
int n, i;
cout<<"Enter number of elements : ";
cin>>n;
int arr[n];
cout<<"Enter the elements : ";
for(i = 0; i < n; i++)
{
cin>>arr[i];
}
mergeSort(arr, 0, n-1); cout<<"Minimum
= "<<arr[0]<<endl; cout<<"Maximum =
"<<arr[n-1]<<endl;
}

int main()
{
struct timespec start, end;

clock_gettime(CLOCK_MONOTONIC, &start);
ios_base::sync_with_stdio(false);

func(); clock_gettime(CLOCK_MONOTONIC,
&end);
double time_taken;
time_taken = (end.tv_sec - start.tv_sec) * 1e9;
time_taken = (time_taken + (end.tv_nsec - start.tv_nsec)) * 1e-9;

cout << "Time taken by program is : " << fixed


<< time_taken << setprecision(9);
cout << " sec" << endl;
return 0;
}
OUTPUT:
BEST CASE – ALREADY SORTED LIST:

WORST CASE – REVERSED SORTED LIST:

AVERAGE CASE 1:

AVERAGE CASE 2:
AVERAGE CASE 3:

GRAPHICAL ANALYSIS:

INPUT VS EXECUTION TIME


4
3.672736

3.5 3.268562
3.1419168

3
2.689996

2.5

1.5 1.345753

0.5
EXECUTION TIME

0
BEST CASE – ALREADY WORST CASE – AVERAGE CASE 1 AVERAGE CASE 2 AVERAGE CASE 3
SORTED LIST REVERSED SORTED LIST
INPUT
INPUT VS PRIMARY OPERATIONS
350
343 343
339
340

330
PRIMARY OPERATIONS

319
320

310 307

300

290

280
BEST CASE – ALREADY WORST CASE – AVERAGE CASE 1 AVERAGE CASE 2 AVERAGE CASE 3
SORTED LIST REVERSED SORTED LIST
INPUT

EXECUTION PRIMARY
CASE INPUT
TIME OPERATION
BEST CASE - ALREADY [12, 15, 23, 26, 34, 37, 45, 48, 56,
3.141916 319
SORTED 59, 8
LIST 60, 67, 71, 78, 82, 89, 93, 100]
WORST CASE - REVERSED SORTED [100, 93, 89, 82, 78, 71, 67, 60, 59,
3.672736 307
LIST 56, 48, 45, 37, 34, 26, 23, 15, 12]
[82, 34, 89, 37, 93, 45, 100, 48, 12,
AVERAGE CASE 1 2.689996 343
56, 15, 59, 23, 60, 26, 67, 78, 71]
[48, 60, 56, 59, 45, 67, 37, 71, 34,
AVERAGE CASE 2 3.268562 343
78,
26, 82, 23, 89, 15, 93, 12, 100]
[100, 12, 93, 15, 89, 23, 82, 26, 78,
AVERAGE CASE 3 1.345753 339
34, 71, 37, 67, 45, 59, 56, 60, 48]
SUMMARY:
There are many ways and methods to solve the above problem, and this is one of the ways. The
algorithm can find the maximum and minimum scores in the students’ scores and also sort the list of the scores.

You might also like