Professional Documents
Culture Documents
Design & Analysis of Algorithms: Practical File
Design & Analysis of Algorithms: Practical File
ANALYSIS OF
ALGORITHMS
PRACTICAL
FILE
AATMASHRI SANYAL
2022UPD9503
QUESTION 1
ALGORITHMS
Step 3: Add the remainder (as a string) to the binary string (binary=str(remainder) + binary)
Step 1: Calculate the sum of all ages in the list (total age=sum(players age))
Step 2: Divide the total age by the number of players (average=total age/len(players age))
Step 2: Run a for loop with the following conditions (int i=0, i<=99;i++)
Step 3: If i%2==0, which checks if and when i is divided by 2, the remainder is 0, that will only
happen if it's an even number
QUESTION 2
FLOWCHART
Start
N = 1
Sum = 0
Sum = Sum + N
N = N + 1
Yes
If N<=100
No
Print Sum
Stop
2)To find the largest of three numbers x, y and z.
Start
Read
A, B, C
No Is
Yes
A>B
Yes Is
No No Is
Yes
B>C A>B
Stop
3) To generate first 50 items of the Fibonacci series
Start
i=0
j=0
k=0
fib=0
No
i<50?
Yes
Output end
“Fibonacci(i)=fib”
Output carriage
return and linefeed
fib=j+k
j=k
k=fib
i=i+1
4)To convert temperature Fahrenheit to Celsius.
Start
Read F
C=(5+(F-32))/9
Print C
Stop
5)To print even numbers between 9 and 100
Start
N=2
Yes
N>100? Stop
No
Print N
N=N+2
QUESTION 3
PROGRAM
CODE
OUTPUT
QUESTION 4
PROGRAM
CODE
OUTPUT
QUESTION 5
PSEUDOCODE
1)Linear Search
end if
end for
end procedure
2)Binary Search
left = 0
right = length(list) - 1
if list[mid] == target:
return mid
left = mid + 1
else:
right = mid - 1
return -1
QUESTION 6
PROGRAM
CODE
OUTPUT
QUESTION 7
PROGRAM
CODE
OUTPUT
QUESTION 8
PSEUDOCODE
1)Bubble Sort
function bubbleSort(arr)
n <- length(arr)
swapped := true
while swapped do
swapped := false
for i := 1 to n - 1 do
if arr[i] > arr[i + 1] then
swap(arr[i], arr[i + 1])
swapped := true
end if
end for
end while
end function
function swap(a, b)
temp := a
a := b
b := temp
end function
2)Selection Sort
function selectionSort(arr)
n <- length(arr)
for i := 0 to n-1 do
minIndex := i // Initialize minimum index as current position
for j := i + 1 to n-1 do // Loop through the unsorted part
if arr[j] < arr[minIndex] then
minIndex := j // Update minimum index if a smaller element is found
end if
end for
swap(arr[i], arr[minIndex]) // Swap the found minimum element with the current element
end for
end function
function swap(a, b)
temp := a
a := b
b := temp
end function
3)Merge Sort
end if
end function
n1 := mid - left + 1
n2 := right - mid
leftArr[1..n1] := copy(arr[left..mid])
i := 1
j := 1
k := left
arr[k] := leftArr[i]
i := i + 1
else
arr[k] := rightArr[j]
j := j + 1
end if
k := k + 1
end while
arr[k] := leftArr[i]
i := i + 1
k := k + 1
end while
arr[k] := rightArr[j]
j := j + 1
k := k + 1
end while
end function
4)Quick Sort
QUESTION 9
PROGRAM
OUTPUT
QUESTION 10
PROGRAM
CODE
OUTPUT
QUESTION 11
PROGRAM
CODE
OUTPUT
QUESTION 12
PROGRAM
CODE
OUTPUT
QUESTION 13
Kruskal’s algorithm
ALGORITHM
Step 3: Iterate through each edge in the sorted order: a. If adding the edge to the minimum
spanning tree does not form a cycle, add it to the minimum spanning tree. b. Otherwise, discard
the edge.
Step 4: Repeat step 3 until there are (V - 1) edges in the minimum spanning tree, where V is the
number of vertices in the graph.
PSEUDOCODE
KRUSKAL(G):
A = ∅ ϴ
MAKE-SET(v)
For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
if FIND-SET(u) ≠ FIND-SET(v):
A = A ∪ {(u, v)}
UNION(u, v)
return A
PRIM’S ALGORITHM
ALGORITHM
Step 3: While there are still vertices not in the spanning tree:
Select the edge with the minimum weight that connects a vertex in the spanning tree to a
vertex, not in the spanning tree.
Add the selected edge and the connected vertex to the spanning tree.
Step 4: Repeat step 3 until all vertices are in the spanning tree.
PSEUDOCODE
prim(graph):
mst = empty set
startVertex = first vertex in graph
mst.add(startVertex)
edges = edges connected to startVertex
mst.add(minEdge)
for edge in edges connected to minEdge:
if edge is not in mst:
edges.add(edge)
edges.remove(minEdge)
return mst as an array
QUESTION 14
PROGRAM
CODE
OUTPUT
QUESTION 15
TURTLE PROGRAMMING
TURTLE PROGRAMMING
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* Inline CSS */
.bold {
font-weight: bold;
.italic {
font-style: italic;
.underline {
text-decoration: underline;
.superscript {
vertical-align: super;
.subscript {
vertical-align: sub;
.emphasized {
font-style: italic;
font-weight: bold;
.strong {
font-weight: bolder;
</style>
</head>
<body>
class="subscript">subscript</sub>.</p>
strong> text.</p>
<br>
<hr>
<font color="red" face="Arial" size="4">This is a text with custom font, color, and size</font>
<pre>
</pre>
<h3>List Examples</h3>
<h4>Ordered List</h4>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
<h4>Unordered List</h4>
<ul>
<li>Item A</li>
<li>Item B</li>
<li>Item C</li>
</ul>
<h3>Colors</h3>
<h3>Images</h3>
<h3>Tables</h3>
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
<h3>Hyperlinks</h3>
</body>
</html>
QUESTION 18
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* Changing Colors */
.text-red {
color: red;
.bg-green {
background-color: green;
/* Changing Borders */
.border-example {
/* Changing Margins */
.margin-example {
margin: 20px;
/* Changing Padding */
.padding-example {
padding: 10px;
.box-example {
padding: 10px;
margin: 20px;
/* Changing Outline */
.outline-example {
/* Changing Text */
.text-example {
font-size: 20px;
font-weight: bold;
text-decoration: underline;
/* Changing Fonts */
.font-example {
</style>
</head>
<body>
</body>
</html>
QUESTION 18
CASE STUDIES
Social media platforms have become an undeniable force in our lives, and the invisible
hand shaping them is the power of algorithms. Let's delve deeper into how algorithms
influence every aspect of a social media platform, from the content you see to the way you
interact.
Imagine walking into a bookstore where every shelf is stocked with your favorite
genres. That's the power of personalized feeds. Social media platforms employ sophisticated
algorithms that analyze your past interactions, likes, comments, and even demographic
information. By understanding what piques your interest, the algorithm prioritizes content that
resonates most with you. This could be news articles on topics you follow, funny cat videos
that tickle your fancy, or even content from friends you haven't interacted with in a while but
share similar interests.
These algorithms aren't static. They leverage machine learning, a branch of artificial
intelligence, to continuously learn and refine their recommendations based on your evolving
behavior. The more you engage, the more the algorithm tailors your feed, potentially creating
an "echo chamber" where you're primarily exposed to information that confirms your existing
beliefs. This can be beneficial for surfacing content you enjoy, but it also raises concerns
about exposure to diverse viewpoints.
Image and video recognition algorithms play a similar role, scanning for harmful content
like violence or graphic imagery. These algorithms are constantly evolving, adapting to new
trends and emerging threats. However, the line between filtering harmful content and stifling
free speech can be blurry. Striking the right balance remains a challenge for social media
platforms and their algorithms.
Have you ever noticed a surge of ads for hiking gear after browsing travel websites for
a weekend getaway? That's not a coincidence. Social media platforms leverage algorithms to
analyze your user data, including demographics, interests, and browsing history, to identify
potential customers for advertisers. By delivering targeted ads that align with your recent
interests, the platform creates a win-win situation. Users experience less intrusive advertising,
and advertisers reach the most relevant audience, maximizing their return on investment.
Social media is all about connecting with like-minded people and discovering new
content. Recommendation systems powered by algorithms play a key role in facilitating this.
These algorithms analyze your preferences and interactions within your network to suggest
new connections, groups, or content that you might find interesting.
Algorithms are widely used to generate intricate and visually appealing patterns across
various domains, including art, design, and technology. Let's explore the application of
algorithms in generating patterns through the example of generative art:
Generative Art: Generative art refers to artwork created using algorithms, often with the goal
of exploring randomness, complexity, and emergent behavior. Algorithms play a fundamental
role in generating patterns, shapes, and compositions, enabling artists and designers to
produce unique and visually captivating artworks
Algorithmic Composition
Music composition algorithms use mathematical models, such as Markov chains or
fractal geometry, to generate melodies, harmonies, and rhythms. These algorithms
introduce elements of randomness and variation, resulting in diverse and evolving
musical patterns
Artists can experiment with different parameters and algorithms to create compositions
that range from structured and melodic to abstract and experimental
Procedural Generation
Procedural generation algorithms are used in video game development, digital art, and
virtual environments to generate terrain, landscapes, and textures. By defining rules and
parameters, these algorithms can create complex and realistic patterns that mimic
natural phenomena
Fractal algorithms, such as the Mandelbrot set or Perlin noise, are commonly used to
generate intricate and self-similar patterns with varying levels of detail
Generative Design
Generative design algorithms are employed in architecture, product design, and
manufacturing to explore and optimize design solutions. By specifying design goals and
constraints, these algorithms can generate a range of alternative designs that meet
predefined criteria
Evolutionary algorithms, genetic algorithms, and artificial neural networks are used to
iteratively generate, evaluate, and refine design solutions, leading to innovative and
efficient outcomes
Computational Art
Computational artists use programming languages and algorithms to create interactive
and dynamic artworks that respond to user input or environmental stimuli. These
artworks often explore concepts of interactivity, generativity, and emergence
Artists combine code, data, and algorithms to produce interactive installations, digital
sculptures, and immersive experiences that challenge traditional notions of art and
creativity.
In conclusion, algorithms enable artists, designers, and technologists to explore new forms of
expression, creativity, and innovation in generating patterns and artworks. By embracing
algorithms as creative tools, practitioners can push the boundaries of art and design, creating
compelling and thought-provoking experiences for audiences worldwide.