You are on page 1of 4

I.A.

Measuring the complexity of a function oralgorithm


1. Write down a function that you might be able to solve in a minute or so. Also, write
down a function that takes you much longer to solve (i.e. more than 5minutes).
2. Make a prediction on how much longer it would take you to calculate the same
function if the numbers were ten or one hundred timesbigger.
3. Run the following program for different values of LOOP and check if the elapsed
time changes. Run the program for larger numbers of aandband check if the elapsed
timechanges.

#include <stdio.h>
#include<time.h>
#define LOOP 2

int main()
{

clock_t start, end;


int elapsed;
longint prod;
int a, b;

a =10;
b =50;
start = clock();
for(inti = 0; i<LOOP; i++)
prod = a * b;
end = clock();

elapsed = end - start;

printf("%ld %ld %d",start, end, elapsed);


return 0; }

4. In the above code, replace multiplication with addition and see how the timechanges.

5. Repeat 3 and 4 for the following code:


#include<stdio.h>
#include<time.h>#defi
ne LOOP 2

int main()
{

time_t start, end;


int elapsed;
longint prod;
int a, b;

a =10;
b =50;
start = time(NULL);
for(inti = 0; i<LOOP; i++)
prod = a * b;

end = time(NULL);

elapsed = end - start;

printf("%ld %ld %d",start, end, elapsed);


return 0;
}
6. Print the variable “prod” inside the for loop in the above two programs. See how
the time varies.Why?

II. A)
1. write down an algorithm to sort 5 numbers (say, 5 8,1,2,7) and answer the following
questions.
a. How many steps does the sortingtake?
b. Will the number of steps change if the numbers are in the order8,7,5,2,1?
c. What will happen if the count of the numbers increases( that is, 10 numbers
instead of 5, 100 numbers instead of5)
2. Consider the following code (called insertion sort) to sort ‘n’ numbers. Include
clock() in the following code in appropriate places to find the time taken for sorting.
Run the following program for different values of ‘n’ and see how the time varies.
Draw a graph to show how the running time increases as ‘n’ increases. To
understand how insertion sort works, use the link“https://visualgo.net/bn/sorting”
// C program for insertion sort
#include <stdio.h>
#include <math.h>

/* Function to sort an array using insertion sort*/


voidinsertionSort(intarr[], intn)
{
inti, key, j;
for(i = 1; i< n; i++)
{
key = arr[i];
j = i-1;

/* Move elements of arr[0..i-1], that are


greater than key, to one position ahead
of their current position */
while(j >= 0 &&arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
// A utility function to print an array of size n
voidprintArray(intarr[], intn)
{
inti;
for(i=0; i< n; i++)
printf("%d ", arr[i]);
printf("\n");
}

/* Driver program to test insertion sort */


intmain()
{
intarr[] = {12, 11, 13, 5, 6};
intn = sizeof(arr)/sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return0;
}

II.B)

1. The following is the C code for another sorting algorithm (heap sort). Include clock() in
the following code in appropriate places to find the time taken for sorting. Run the
following program for different values of ‘n’ and see how the time varies. Draw a graph
to show how the running time increases as ‘n’ increases. To understand how heap sort
works, use the link https://commons.wikimedia.org/wiki/File:Heapsort-example.gif

/*
* C Program to sort an array based on heap sort algorithm(MAX heap)
*/
#include <stdio.h>

voidmain()
{
intheap[10], no,i, j, c, root, temp;

printf("\n Enter no of elements:"); scanf("%d",&no);


printf("\n Enter the nos : "); for(i=0;i<no;i++) scanf("%d",&heap[i]); for(i=1;i<no;i++)
{
c =i;
do
{
root=(c -1)/2;
if(heap[root]<heap[c])/* to create MAX heap array */
{
temp= heap[root]; heap[root]= heap[c]; heap[c]= temp;
}
c = root;
}while(c !=0);
}

printf("Heap array : "); for(i=0;i<no;i++)


printf("%d\t ", heap[i]);
for(j = no -1; j >=0; j--)
{
temp= heap[0];
heap[0]= heap[j];/* swap max element with rightmost leaf element */
heap[j]= temp; root=0;
do
{
c =2* root +1;/* left node of root element */
if((heap[c]<heap[c +1])&&c <j-1) c++;
if(heap[root]<heap[c]&&c<j)/* again rearrange to max heap array */
{
temp= heap[root]; heap[root]= heap[c]; heap[c]= temp;
}
root= c;
}while(c <j);
}
printf("\n The sorted array is : "); for(i=0;i<no;i++)
printf("\t %d", heap[i]);
}

III.
a. Write short notes of Big Onotation
b. List the complexities for various searching algorithms

You might also like