0% found this document useful (0 votes)
23 views3 pages

CS506 (2023-I) Lab Exercise 1

The document outlines a lab exercise for CS506 focused on implementing and analyzing the time complexity of various comparison-based sorting algorithms in C. It specifies input requirements, including the number of test cases and parameters for sorting algorithms, as well as constraints on the input data. Additionally, it provides a sample code for timing operations on arrays, with instructions for submitting the assignment and conducting performance analysis.
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)
23 views3 pages

CS506 (2023-I) Lab Exercise 1

The document outlines a lab exercise for CS506 focused on implementing and analyzing the time complexity of various comparison-based sorting algorithms in C. It specifies input requirements, including the number of test cases and parameters for sorting algorithms, as well as constraints on the input data. Additionally, it provides a sample code for timing operations on arrays, with instructions for submitting the assignment and conducting performance analysis.
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/ 3

CS506 (2023-I) Lab Exercise 1 Related

Purpose of this lab is to implement and analyse time complexity of different comparison-based
sorting algorithms.

Prog. Language C

Input:

First line will have positive number T that will tell you the number of each test case. This will be
followed by T lines (or say, rows), each indicating an independent test case.

Currently for today, consider each test case entry to have four non-negative numbers: N, S, k
and p,
where N indicates number of elements to be there in array (i.e. N is considered as input array
size)
S indicates which sorting algorithm to be implemented.
S will be either 0,1,2,3 or 4 (0 Bubble, 1 Selection , 2 Insertion, 3 Merge, 4 Quick)
k and p will be either 0, 1 or 2.
For k, 0=> Array elements are in random order, 1 indicates Input array is already sorted
and 2 indicates that input array is reversely sorted).
For p, 0=> Neither print input array nor output array, 1 indicates that you also print
output array elements (max. 50 elements to be printed if corresponding N is more than
50) and 2 indicates that the code prints both input and output array elements (only at
most first 50 elements of each) You may note is p=1, then there will be 2T output rows.
And if p=2, then there will be 3T output rows.
Else (for p=0), there will be T output rows/ lines where each line indicates the time
taken by the sorting algorithm to sort the elements in non-decreasing order.

Assume the array has N elements in random order.


Elements in the array are unsigned int (and range is 0 to 9999999)

Constraints:
0< N < 10000 (This constraint is for our testing, for your analysis you may use higher N)
S will be either 0,1,2,3 or 4 (0 Bubble, 1 Selection , 2 Insertion, 3 Merge, 4 Quick)
k and p will be either 0, 1 or 2

Note:
- Google form for submitting assignment code and Comparative analysis report will be
provided soon and Deadline for submission is 5th-Aug midnight.
- For analysis, it is preferred that you have graphs showing comparative
performance. Few graphs, if required for some reason, can be for single sorting
algo/case.
=====================================================
Following code might be helpful to you in lab assignment. I have also checked it on
https://www.onlinegdb.com/online_c_compiler
=======================================================

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void array_reverse(int arr[], int start, int end){


int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

void add_constant(int arr[], int length, int num){


int i =0;
for(i=0;i<length;i++)
{
arr[i]=arr[i]+num;
}
}

void mul_constant(int arr[], int length, int num){


int i =0;
for(i=0;i<length;i++)
{
arr[i]=arr[i]*num;
}
}

int main(void){
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime(&rawtime);
printf("Current local time and date: %s", asctime (timeinfo) );

int n,num=5;

clock_t start, end; // For time calculation

double total_time_consumed,t1,t2,t3;

for (n=10000; n<=50000; n=n+20000) {


int arr[n],i;
for(i=0;i<n;i++) {
arr[i] = rand()%10000; // Generate a number between 0 and 9999
}

start = clock(); // start time


array_reverse(arr, 0, n-1);
end = clock(); // end time
total_time_consumed = ((double) (end - start)) / CLOCKS_PER_SEC;
t1= total_time_consumed;

start = clock(); // start time


add_constant(arr,n,num);
end = clock(); // end time
total_time_consumed = ((double) (end - start)) / CLOCKS_PER_SEC;
t2= total_time_consumed;

start = clock(); // start time


mul_constant(arr,n,num*2);
end = clock(); // end time
total_time_consumed = ((double) (end - start)) / CLOCKS_PER_SEC;
t3= total_time_consumed;

printf("\n Time Consumed when n=%d => %f \t %f \t %f",n, t1, t2, t3);
}
return 0;
}

You might also like