Lab-Assignment: World University of Bangladesh
Lab-Assignment: World University of Bangladesh
Lab-Assignment
Subject Name : Computer Algorithms Lab
Subject Code : CSE 1002
Submitted To
Ratna Mudi
Lecturer, Department of CSE.
World University of Bangladesh
Submitted By
Ummay Sumaiya
Roll - 2204
Batch - 38(A)
Dept : CSE.
Assignment- Spring Semester- 2020
Computer Algorithms Lab
Batch: 38A
Course Teacher: Ratna Mudi
Submission Date: 03.05.2020
If you are not new to programming, then it is probably familiar and you could skip this chapter.
Recursion is a programming pattern that is useful in situations when a task can be naturally split into
several tasks of the same kind, but simpler. Or when a task can be simplified into an easy action plus
a simpler variant of the same task. Or, as we’ll see soon, to deal with certain data structures.
When a function solves a task, in the process it can call many other functions. A partial case of this is
when a function calls itself. That’s called recursion.
pow(2, 2) = 4
pow(2, 3) = 8
pow(2, 4) = 16
2. function pow(x, n) {
3. let result = 1;
4.
5. // multiply result by x n times in the loop
6. for (let i = 0; i < n; i++) {
7. result *= x;
8. }
9.
10. return result;
11. }
12.
alert( pow(2, 3) ); // 8
if n==1 = x
/
pow(x, n) =
\
else = x * pow(x, n - 1)
1. pow(2, 4) = 2 * pow(2, 3)
2. pow(2, 3) = 2 * pow(2, 2)
3. pow(2, 2) = 2 * pow(2, 1)
4. pow(2, 1) = 2
So, the recursion reduces a function call to a simpler one, and then – to even more simpler, and so
on, until the result becomes obvious.
Ans To The Question No – 4
Sorting is a process of arranging items in ascending or descending order. This process can be
implemented via many different algorithms.
1. Find a “pivot” item in the array. This item is the basis for comparison for a single
round.
2. Start a pointer (the left pointer) at the first item in the array.
3. Start a pointer (the right pointer) at the last item in the array.
4. While the value at the left pointer in the array is less than the pivot value, move
the left pointer to the right (add 1). Continue until the value at the left pointer is
move the right pointer to the left (subtract 1). Continue until the value at the right
6. If the left pointer is less than or equal to the right pointer, then swap the values at
7. Move the left pointer to the right by one and the right pointer to the left by one.
Below is an image of an array, which needs to be sorted. We will use the Quick Sort Algorithm,