Daa Lab-2
Daa Lab-2
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
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.
O(n)
CODE:
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
int i, j, k;
i = 0;
j = 0;
k = p;
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;
AVERAGE CASE 1:
AVERAGE CASE 2:
AVERAGE CASE 3:
GRAPHICAL ANALYSIS:
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.