Define Algorithm Ans Assig1
Define Algorithm Ans Assig1
Properties of Algorithm
➢ Non Ambiguity
Each step in an algorithm should be non-ambiguous. That means each instruction should be
clear and precise. The instruction in any algorithm should not denote any conflicting meaning.
This property also indicates the effectiveness of algorithm.
➢ Range of Input
The range of input should be specified. This is because normally the algorithm is input driven
and if the range of input is not being specified then algorithm can go in an infinite state.
➢ Multiplicity
The same algorithm can be represented into several different ways. That means we can write
in simple English the sequence of instruction or we can write it in form of pseudo code.
Similarly, for solving the same problem we can write several different algorithms.
➢ Speed
The algorithmis written using some specified ideas. Bus such algorithm should be efficient
and should produce the output with fast speed.
➢ Finiteness
The algorithm should be finite. That means after performing required operations it should be
terminate.
Characteristics of an Algorithm
•
Input: An
algorithm requires some input values. An algorithm can be given a value other
than 0 as input.
• Output: At the end of an algorithm, you will have one or more outcomes.
Moving on in this What is an Algorithm tutorial, you will look at why you need an algorithm.
1. Divide and Conquer Approach: It is a top-down approach. The algorithms which follow
the divide & conquer techniques involve three steps:
o Greedy Algorithm always makes the choice (greedy criteria) looks best at the moment,
to optimize a given objective.
o The greedy algorithm doesn't always guarantee the optimal solution however it
generally produces a solution that is very close in value to the optimal.
This is particularly helpful when the number of copying subproblems is exponentially large.
Dynamic Programming is frequently related to Optimization Problems.
4. Branch and Bound: In Branch & Bound algorithm a given subproblem, which cannot be
bounded, has to be divided into at least two new restricted subproblems. Branch and Bound
algorithm are methods for global optimization in non-convex problems. Branch and Bound
algorithms can be slow, however in the worst case they require effort that grows exponentially
with problem size, but in some cases we are lucky, and the method coverage with much less
effort.
6. Backtracking Algorithm: Backtracking Algorithm tries each possibility until they find the
right one. It is a depth-first search of the set of possible solution. During the search, if an
alternative doesn't work, then backtrack to the choice point, the place which presented different
alternatives, and tries the next alternative.
We all know, human nature aspires to seek an efficient way to assemble their daily tasks. The
predominant thought process behind innovation and technology is to make life easier for
people by providing ways to solve problems they may encounter. The same thing happens
within the world of computer science and digital products. We write algorithms that are
Time complexity is the time taken by the algorithm to execute each set of instructions. It is
always better to select the most efficient algorithm when a simple problem can solve with
different methods.
Space complexity is usually referred to as the amount of memory consumed by the algorithm.
Bubble Sort is a simple sorting algorithm that sorts an array of elements by repeatedly
comparing adjacent elements and swapping them if they are in the wrong order. The algorithm
gets its name from the way smaller elements "bubble" to the top of the array.
for j = 0 to n-1-i
The outer loop iterates n-1 times, where n is the length of the array. The inner loop iterates n-
1-i times, where i is the current iteration of the outer loop. The if statement checks if the current
element is greater than the next element, and if so, it swaps them.
The time complexity of Bubble Sort is O(n^2), which means that the time it takes to sort an
array of n elements grows quadratically as the size of the array increases. This makes it an
inefficient algorithm for sorting large arrays, as the number of comparisons and swaps
increases quickly with the size of the array.
However, Bubble Sort has the advantage of being easy to understand and implement, and it
can be useful for small arrays or for educational purposes. It also has the property of being
stable, which means that elements with equal values remain in their original order after sorting.
Linear search algorithm is a simple search algorithm for finding the position of an element in
an array. It works by sequentially checking each element of the array until a match is found or
the entire array has been searched.
for i = 0 to n-1
if arr[i] == x
return i
return -1
The algorithm iterates through each element of the array and compares it to the search key, x.
If a match is found, the algorithm returns the index of the element containing the match. If the
entire array is searched and no match is found, the algorithm returns a value of -1.
The time complexity of Linear Search is O(n), where n is the length of the array. This means
that the time it takes to locate an element in an array increases linearly with the size of the
array. This makes it an inefficient algorithm for large arrays, as the number of comparisons and
iterations increases quickly with the size of the array.
However, Linear Search has the advantage of being simple and easy to implement, and it can
be useful for small arrays or when the array is not sorted. Additionally, Linear Search can easily
be modified to search for elements based on some condition other than equality, such as
searching for elements greater than a certain value.
6)Give note on recurrence relation for general divide and conquer
technique.
In computer science, the divide and conquer technique is a problem-solving strategy that
involves breaking down a complex problem into smaller subproblems, solving these
subproblems recursively, and then combining the results to produce the final solution.
Recurrence relation is a common way to express the time complexity of a divide and conquer
algorithm.
In a divide and conquer algorithm, the running time can be expressed using a recurrence
relation, which describes the time complexity of the algorithm in terms of the size of the
input. Generally, recurrence relation has three parts:
1. Base case: This is the smallest input size for which the solution is known. For example, in
a binary search algorithm, the base case is when the input array has only one element.
2. Divide/split: This is the step in which the input is divided into smaller subproblems. For
example, in a merge sort algorithm, the divide step involves dividing the array into two
halves.
3. Conquer/combine: This is the step in which each subproblem is recursively solved and the
solutions are combined to produce the final solution.
For example, in a merge sort algorithm, the conquer step involves recursively sorting each
half of the array and then combining the sorted halves.
7)Explain quick sort algorithm and give its worst case analysis.
Quick Sort is a popular sorting algorithm that uses the divide and conquer strategy to sort an
array of elements. It works by selecting a pivot element from the array, partitioning the array
around the pivot, and recursively sorting the subarrays on either side of the pivot.
pivot = arr[high]
i = low - 1
i=i+1
return i+1
The Quick Sort algorithm starts by recursively partitioning the array into two halves, where
one half contains elements smaller than the pivot and the other half contains elements greater
than the pivot. This process repeats until each subarray has only one element, at which point
the entire array is sorted.
8)Explain merge sort algorithm and give its worst case analysis.
Merge Sort is a classic algorithm that is used for sorting an array of elements. It
is a divide and conquer algorithm that works by dividing the array into halves,
sorting the halves recursively, and then merging the sorted halves to produce the
final sorted array.
The Merge Sort algorithm divides the array into two halves, sorts the two
subarrays recursively, and then merges the sorted subarrays using the merge
function. The merge function iteratively compares and merges elements from
the two sorted subarrays to produce the final sorted array.
The worst-case time complexity of Merge Sort is O(n log n), where n is the
length of the array. This is because the algorithm divides the array into halves
recursively until each subarray has only one element, and then merges the
subarrays in a bottom-up fashion, taking O(n) time for merging each level of the
tree. The total number of levels in the tree is log(n), so the worst-case time
complexity is O(n log n).