Professional Documents
Culture Documents
Algorithm
This branching and repetition depend on the problem for which Algorithm is
developed.
All the steps of Algorithm during the definition should be written in a human-
understandable language which does not depend on any programming language.
1. Input: An algorithm should have zero or more but should be a finite number of
inputs. We can also say that it is essential for any algorithm before starting. Input
should be given to it initially before the Algorithm begins.
2. Output: An algorithm must give at least one required result from the given set of
input values. These output values are known as the solution to a problem.
Recursion reduces the size of the Iteration increases the size of the code.
code.
A recursive algorithm is one that Iterative algorithms use constructs like loops
calls itself repeatedly until a base and sometimes other data structures like
condition is satisfied. stacks and queues to solve the problems.
Each recursive call requires extra Each iteration does not require extra space
space on the stack frame (memory).
If we get infinite recursion, the An infinite loop could loop forever since there
program may run out of memory is no extra memory being created.
and result in stack overflow.
Q.2 What is recursion? And explain different application of recursion with following
programmers (1) Fibonacci Series (2) factorial of number
Sorting
1) Sorting is an algorithm that arranges the elements of a list in a certain order [either
ascending or descending]. The output is a permutation or reordering of the input.
2) Sorting is one of the important categories of algorithms in computer science and a
lot of research has gone into this category.
3) Sorting can significantly reduce the complexity of a problem.
4) It is often used for database algorithms and searches.
5) Sorting algorithms are generally classified based on the following parameters.
a) By Number of Comparisons
b) By Number of Swaps
c) By Memory Usage
d) By Recursion
e) By Stability
Bubble sort
Selection sort
Insertion sort
1) Every repetition of insertion sort removes the element from the input data.
2) inserts it into the correct position in the already-sorted list until no input elements
remain.
3) Sorting is typically done in – place
4) The resulting array after k iterations has the property where the first k + I entries
are sorted.
5) Each clement greater than x is copied to the right as it is compared against x .
6) Algorithm takes O(n2)
7) Space complexity : 0(1) and o(1)
8) Example :
9) Given an array: 6 8 l 4 5 3 7 2 and the goal is to put them in ascending order.
6 8 l 4 5 3 7 2 (Consider index 0)
6 8 I 4 5 3 7 2 (Consider indices 0 - I )
1 6 8 4 5 3 7 2 (Consider indices~ 0 - 2: Insertion sort places 1 in front of 6 and 8)
1 4 6 8 5 3 7 2 (Process some as above is repeated until array is sorted)
14568 372
13456782
1 2 3 4 5 6 7 8 (The array is sorted!)
1) Sorting is an algorithm that arranges the elements of a list in a certain order [either
ascending or descending]. The output is a permutation or reordering of the input.
2) different sorting techniques including their complexity, stability and memory
constraints.
3) Time complexity Analysis : the best, average and worst case complexity of
different sorting techniques with possible scenarios.
4) Bubble sort and Insertion sort –
a) Average and worst case time complexity: n^2
b) Best case time complexity: n when array is already sorted.
c) Worst case: when the array is reverse sorted.
5) Selection sort – Best, average and worst case time complexity: n^2 which is
independent of distribution of data.
6) Merge sort – Best, average and worst case time complexity: nlogn which is
independent of distribution of data.
1) The simplest solution to the sequence search problem is the sequential or linear
search algorithm.
2) This technique iterates over the sequence, one item at a time, until the specific item
is found.
3) Example
a) The use of the in operator makes our code simple and easy to read but it hides
the inner workings. Underneath, the in operator is implemented as a linear
search.
b) Consider the unsorted 1-D array of integer values shown in Figure(a). To
determine if value 31 is in the array, the search begins with the value in the first
element.
c) Since the first element does not contain the target value, the next element in
sequential order is compared to value 31.
d) This process is repeated until the item is found in the sixth position.
e) What if the item is not in the array? For example, suppose we want to search for
value 8 in the sample array. The search begins at the first entry as before, but
this time every item in the array is compared to the target value. It cannot be
determined that the value is not in the sequence until the entire array has been
traversed, as illustrated in Figure 5.1(b).
Binary search
Q. 10 compare and contrast linear and binary search with respect to time and space
complexity
Q. 11 What is selection technique explain selection by sorting with example
1) Classification There are many ways of classifying algorithms and a few of them are
shown below
a) Implementation Method
b) Design Method
2) Classification by Implementation Method
a) Recursion : A recursive algorithm is one that calls itself repeatedly until a base
condition is satisfied.
b) Iteration : Iterative algorithms use constructs like loops and sometimes other
data structures like stacks and queues to solve the problems.
c) Serial : the algorithms we assume that computers execute one instruction at a
time. These are called serial algorithms.
d) Parallel : Parallel algorithms take advantage of computer architectures to
process many instructions at a time.
e) Deterministic or Non-Deterministic : Deterministic algorithms solve the problem
with a predefined process, whereas non – deterministic algorithms guess the best
solution at each step through the use of heuristics.
3) Classification by Design Method
a) Greedy Method :
i) Method Greedy algorithms work in stages.
ii) A decision is made that is good at that point, without bothering about the
future consequences. Generally, this means that some local best is chosen.
iii) It assumes that the local best selection also makes for the global optimal
solution.
b) Divide and Conquer :
i) Divide : Breaking the problem into sub problems that are themselves smaller
instances of the same type of problem.
ii) Recursion: Recursively solving these sub problems
iii) Conquer: Appropriately combining their answers.
c) Linear Programming : In linear programming, there are inequalities in terms of
inputs and maximizing (or minimizing) some linear function of the inputs.
4) Classification by Complexity : In this classification, algorithms are classified by the
time they take to find a solution based on their input size.
5) Randomized Algorithms A few algorithms make choices randomly.
Q.3 Explain the concept of divide and concure? State its advantages and disadvantages?