Algorithm 1
Algorithm 1
Algorithm 1
Divide and Conquer is an algorithmic pattern. In algorithmic methods, the design is to take a dispute on a
huge input, break the input into minor pieces, decide the problem on each of the small pieces, and then
merge the piecewise solutions into a global solution. This mechanism of solving the problem is called the
Divide & Conquer Strategy.
Divide and Conquer algorithm consists of a dispute using the following three steps.
1. Divide the original problem into a set of subproblems.
2. Conquer: Solve every subproblem individually, recursively.
3. Combine: Put together the solutions of the subproblems to get the solution to the whole problem.
Case 2: item ≠A [mid], then we will split the array into two equal parts of size .
And again find the midpoint of the half-sorted
half sorted array and compare with search element.
Repeat the same process until a search element is found.
At least there will be only one term left that's why that term will compare out, and only one comparison be
Merge Sort
Merge sort is yet another sorting algorithm that falls under the category of Divide and Conquer technique.
It is one of the best sorting techniques that successfully build a recursive algorithm.
Merge( ) Function Explained Step-By-Step
Step
Consider the following example of an unsorted array, which we are going to sort with the help of the
Merge Sort algorithm.
A= (36,25,40,2,7,80,15)
Step1: The merge sort algorithm iteratively divides an array into eq equal
ual halves until we achieve an atomic
value. In case if there are an odd number of elements in an array, then one of the halves will have more
elements than the other half.
Step2: After dividing an array into two subarrays, we will notice that it did not h hamper
amper the order of
elements as they were in the original array. After now, we will further divide these two arrays into other
halves.
Step3: Again, we will divide these arrays until we achieve an atomic value, i.e., a value that cannot be
further divided.
Step4: Next, we will merge them back in the same way as they were broken down.
Step5: For each list, we will first compare the element and then combine them to form a new sorted list.
Step6: In the next iteration, we will compare the lists of two data valu
values
es and merge them back into a list of
found data values, all placed in a sorted manner.
Hence the array is sorted.
Analysis of Merge Sort:
Let T (n) be the total time taken by the Merge Sort algorithm.
But we ignore '-1' because the element will take some time to be copied in merge lists.
So T (n) = 2T + n...equation 1
Note: Stopping Condition T (1) =0 because at last, there will be only 1 element left that need to be copied, and
there will be no comparison.
Best Case Complexity: The merge sort algorithm has a best-case
best case time complexity of O(n*log n) for the
already sorted array.
T (n) = 2 T → Eq (i)
T (2) = 1, time required to compare two elements/items. (Time is measured in units of the number of
comparisons)
→ Eq (ii)
{Use recursion means, we will use some stopping condition to stop the algorithm}
It is evident;
vident; we can reduce the number of comparisons (complexity) by using a proper technique.
Dynamic Programming
Dynamic programming is a technique that breaks the problems into sub sub-problems,
problems, and saves the result
for future purposes so that we do not need to compute the result again. The subproblems are optimized
to optimize the overall solution is known as optimal substructure
substructure property. The main use of dynamic
programming is to solve optimization problems. Here, optimization problems mean that when we are
trying to find out the minimum or the maximum solution of a problem. The dynamic programming
guarantees to find the
he optimal solution of a problem if the solution exists.
The definition of dynamic programming says that it is a technique for solving a complex problem by first
breaking into a collection of simpler subproblems, solving each subproblem just once, and then storing
their solutions to avoid repetitive computations.
Let's understand this approach through an example.