Group Assignment - Theory of Algorithms
Group Assignment - Theory of Algorithms
GROUP MEMBERS
S.No NAME ID SECTION YEAR DEPT.
Stay Safe!
QUESTIONS:
1. Briefly discuss the different types of methods and needs for algorithm analysis?
2. Briefly discuss the different types of algorithm complexity with example?
3. Briefly discuss the different sorting techniques with example?
4. Briefly explain the different types of linked lists with example?
5. Briefly discuss about tree traversals and the different applications?
1. Briefly discuss the different types of methods and
needs for algorithm analysis
Analysis of Algorithm
• is the process of analyzing the problem-solving capability of the
algorithm in terms of the time and size required (the size of memory for
storage while implementation)? However, the main concern of analysis of
algorithms is the required time or performance. Types of algorithm
Types of Analysis:
Space Complexities
➢ Space complexity of an algorithm represents the amount of memory
space required by the algorithm in its life cycle. Space required by an
algorithm is equal to the sum of the following two components
▪ A fixed part that is a space required to store certain data
and variables that are independent of the size of the
problem. For example, simple variables & constant used,
program size etc.
▪ A variable part is a space required by variables; whose size
depends on the size of the problem. For example, dynamic
memory allocation, recursion stacks space etc. Space
complexity S(P) of any algorithm P is S(P) = C + SP(I)
Where C is the fixed part and S(I) is the variable part of the
algorithm which depends on instance characteristic I
For example
✓ Algorithm: SUM (A, B)
✓ Step 1 - START
✓ Step 2 - C ← A + B + 10
✓ Step 3 - Stop
3. Briefly discuss the different sorting techniques with
example?
Sorting Techniques
➢ Sorting refers to arranging data in a particular format. Sorting algorithm
specifies the way to arrange data in a particular order. Most common
orders are in numerical or lexicographical order. The importance of
sorting lies in the fact that data searching can be optimized to a very
high level, if data is stored in a sorted manner. Sorting is also used to
represent data in more readable formats.
Sorting Techniques with example
➢ Selection sort
▪ sorting algorithm which works as follows.
o Find the minimum value in the list
o Swap it with the value in the first position
o Repeat the steps above for remainder of the list (starting at the
second position)
✓ Examples
• 26 33 43 100 46 88 52 17 53 77
• 17 | 33 43 100 46 88 52 26 53 77
• 17 26 | 43 100 46 88 52 33 53 77
• 17 26 33 | 100 46 88 52 43 53 77
• 17 26 33 43 | 46 88 52 100 53 77
• 17 26 33 43 46 | 88 52 100 53 77
• 17 26 33 43 46 52 | 88 100 53 77
• 17 26 33 43 46 52 53 | 100 88 77
• 17 26 33 43 46 52 53 77 | 88 100
• 17 26 33 43 46 52 53 77 88 | 100
➢ Insertion Sort
▪ each successive element in the array to be sorted is inserted into its proper
place with respect to the other, already sorted elements.
o We divide our array in a sorted and an unsorted array
o Initially the sorted portion contains only one element: the first
element in the array.
o We take the second element in the array, and put it into its
correct place
✓ Examples
• 99 | 55 4 66 28 31 36 52 38 72
• 55 99 | 4 66 28 31 36 52 38 72
• 4 55 99 | 66 28 31 36 52 38 72
• 4 55 66 99 | 28 31 36 52 38 72
• 4 28 55 66 99 | 31 36 52 38 72
• 4 28 31 55 66 99 | 36 52 38 72
• 4 28 31 36 55 66 99 | 52 38 72
• 4 28 31 36 52 55 66 99 | 38 72
• 4 28 31 36 38 52 55 66 99 | 72
• 4 28 31 36 38 52 55 66 72 99 |
➢ Bubble sort
▪ is similar to selection sort in the sense that it repeatedly finds the
largest/smallest value in the unprocessed portion of the array and puts it back.
o However, finding the largest value is not done by selection this
time.
o We "bubble" up the largest value instead.
o Compares adjacent items and exchanges them if they are out of
order.
o Comprises of several passes.
o In one pass, the largest value has been “bubbled” to its proper
position.
o In second pass, the last value does not need to be compared.
✓ Examples
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
➢ Merge sort
▪ Merge-sort applies the divide-and-conquer technique to the sorting problem,
where, for the sake of generality, let us consider the sorting problem to take a
sequence, S, of objects as input, which could be represented with either a list or
an array, and returns S in sorted order. For the problem of sorting a sequence S
with n elements, the three divide-and conquer steps are as follows:
o Divide: If S has zero or one element, return S immediately; it is already
sorted. Otherwise (S has at least two elements), put the elements of S into
two sequences, S1 and S2, each containing about half of the elements of S;
that is, S1 contains the first [n/2] elements of S, and S2 contains the
remaining n/2 elements.
o Recur: Recursively sort the sequences S1 and S2.
o Conquer: Put back the elements into S by merging the sorted sequences S1
and S2 into a sorted sequence.
We can visualize an execution of the merge-sort algorithm using a binary tree T, called
the merge-sort tree.
✓ Examples
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
➢ Quick sort
▪ is a divide and conquer algorithm.
▪ Quick sort first divides a large list into two smaller sub-lists: the low elements
and the high elements. Quick sort can then recursively sort the sub-lists.
• 549718326
• 413259786
• 132457869
• 132456789
• 123456789
• 123456789
➢ Example
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
❖ In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right
sub-tree. We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order.
We start from A, and following in-order traversal, we move to its left subtree B. B is
also traversed in-order. The process goes on until all the nodes are visited. The output
of inorder traversal of this tree will be −
D→B→E→A→F→C→G
❖ Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally
the right subtree.
We start from A, and following pre-order traversal, we first visit A itself and then move
to its left subtree B. B is also traversed pre-order. The process goes on until all the
nodes are visited. The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
❖ Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse
the left subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all the nodes are
visited. The output of post-order traversal of this tree will be −
D→E→B→F→G→C→A