DAA Assignment-1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Department of Artificial Intelligence

Assignment Submission Details

AY-2023- 24

Subject: CORE /Open Elective / Discipline Elective / SOC

Subject Code: 20CAI109

Subject Name: DESIGN AND ANALYSIS OF ALGORITHMS

Name of the Student B.Meghana

Roll. No. 22691A3191

Year /Sec II – CAI – B Sec



Assignment No. I // II

Marks( **Max 5 Mark)


Uploaded // Drafted - __________
Assignment Moodleuploaded Date

Faculty Sign with name & Date


ASSIGNMENT-1
1. Design an algorithm that inputs three integers and
outputs them in non-decreasing order?
A. Algorithm:
An algorithm is a step-by-step procedure or a
set of instructions designed to solve a specific
problem or perform a particular task, It’s a well-
defined computational procedure that takes some
input, processes it, and produces an output.

This algorithm takes three integers as input,


stores them in a list, sorts the list in non-decreasing
order using the `sort() ` method, and then returns the
sort list.
This can be achieved by using conditional statements
and swapping technique.

Algorithm:

//Take the three integers as a input from the user


(a, b, c)

Algorithm sort three numbers(a, b, c):


{
numbers = [a, b, c];
numbers . sort ();
return number;
}
Time complexity is : O(1)

Example program for non-decreasing order:

def sort_ three_ numbers(a, b, c):


numbers = [a, b, c]
numbers . sort()
return numbers
a = int(input(“Enter the first number: “))
b = int(input(“Enter the second number: “))
c = int(input(“Enter the third number “))

sorted_ numbers = sort_ three_ numbers(a, b, c)


print(“Number in non-decreasing order”, sorted_
numbers)

2. Solve the following recurrence relation:


T(n) = 1, if n=1 or
T(n) = T(n-1) + n(n-1), if n>=2

A. Recurrence Relation:
A recurrence relation defines the time
complexity of an algorithm recursively. It expresses
the time or space complexity of a problem in terms
of the size of its smaller instances. Recurrence
relations are often used to analyse the time
complexity of recursive algorithms.

The given recurrence relation solved by using the


iteration method
i.e by expanding the recurrence relation iteratively
until you find a pattern that allows you to derive a
closed from solution.

T(n)=T(n-1)+n(n-1) if n>=2

Given,

T(1)=1

Let us take n=2 , 3, 4 and substitute in T(n)


T(2)=T(2-1)+2(2-1)
=T(1)+2
=3

T(3)=T(2)+3(3-1)
=3+6
=9

T(4)=T(3)+4(4-1)
=9+12
=21

Here we can observe the pattern like


T(n)=1+2(2-1)+3(3-1)+……..+n(n-1). We’ve already
seen this sum before when solving the recurrence
relation through the telescoping sums method.

So, the solution to the reccurence relation

T(n)=T(n-1)+n(n-1) is:

T(n)=1 + n(n-1)(2n-1)/6 + n(n+1)/2

Therefore the time complexity of the given


recurrence relation is:

T(n)=O(n)

You might also like