

{"id":93540,"date":"2021-05-07T09:00:24","date_gmt":"2021-05-07T03:30:24","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=93540"},"modified":"2021-06-26T19:22:56","modified_gmt":"2021-06-26T13:52:56","slug":"divide-and-conquer-algorithm","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/","title":{"rendered":"Divide and Conquer Algorithm"},"content":{"rendered":"<p>Have you ever been a part of the annual fest organizing committee at your college or your institution? If not, allow me to tell you how to organize a large-scale annual fest efficiently at any institution.<\/p>\n<p>There are mainly two tasks while organizing a fest. Decorating the campus and organizing events. Now the event organizer cannot do these huge tasks alone, so the organizer assigns these two tasks to two different people.<\/p>\n<p>Event coordinator-1 volunteers for the task of decorating the whole campus and another person, event coordinator-2 gets the task of organizing all the events. Now further these two persons cannot complete the task. So they divide their task into sub-members of the committee.<\/p>\n<p>A group of people gets to make banners for decoration and another group gets to put all the lights all over the campus. The third group gets to organize the gaming events and the last group gets the task to organize the talk shows events.<\/p>\n<p>Now they achieve their task separately, combine them and at the end, we have a very well organized annual fest which you are going to remember for years to come. This is a very good example of divide and conquer.<\/p>\n<p>When a single task is very huge to execute, the problem is divided into multiple subproblems. Each problem is then solved individually and at the end combined to get the solution to the whole problem.<\/p>\n<p>In this article, we will study the divide and conquer algorithm, its fundamentals, and its pros and cons. We shall also see some very good real-life applications of the divide and conquer algorithm approach.<\/p>\n<h3>Divide and Conquer Algorithm<\/h3>\n<p><span style=\"font-weight: 400;\">In the divide and conquer algorithm, the problem is divided into multiple subproblems. Each subproblem is then solved, and then combine all the solutions to these subproblems to get the solution of the whole problem.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This algorithm has three steps:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. <strong>Divide<\/strong>: divide the larger problem into multiple subproblems.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. <strong>Conquer<\/strong>: recursively solve every subproblem individually.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. <strong>Combine<\/strong>: combine all the solutions of all the subproblems to get the solution to the whole problem.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-93546\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01.jpg\" alt=\"Divide and conquer algorithm steps\" width=\"900\" height=\"650\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01.jpg 900w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01-300x217.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01-150x108.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01-768x555.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01-720x520.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01-520x376.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images01-320x231.jpg 320w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><\/p>\n<h3>Fundamentals of divide and conquer algorithm<\/h3>\n<p><span style=\"font-weight: 400;\">The divide and conquer strategy of designing algorithms has two fundamentals :<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Relational formula<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Stopping condition\u00a0<\/span><\/li>\n<\/ul>\n<p><b>1. Relational formula<\/b><\/p>\n<p><span style=\"font-weight: 400;\">It is the formula generated from a given technique. After generating the formula, we apply the divide and conquer strategy to break the problem into many subproblems and solve them individually.<\/span><\/p>\n<p><b>2. Stopping condition<\/b><\/p>\n<p><span style=\"font-weight: 400;\">When you divide the problem by divide and conquer strategy, you need to understand for how long you have to keep dividing. You have to put a stopping condition to stop your recursion steps.<\/span><\/p>\n<h3>Divide and conquer recurrence relations<\/h3>\n<p><span style=\"font-weight: 400;\">Recurrence relation for divide and conquer is given by:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T(n) = aT(n\/b) + f(n), where a &gt; 1 and b &gt; 1<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We solve the above recurrence relation using the Mas<\/span><span style=\"font-weight: 400;\">ter Method. <\/span><span style=\"font-weight: 400;\">It is a direct way of getting a solution to recurrence relations for divide and conquer algorithms. There are three cases:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If f(n) = \u0398(n<\/span><sup><span style=\"font-weight: 400;\">c<\/span><\/sup><span style=\"font-weight: 400;\">) where c &lt; log<\/span><sub><span style=\"font-weight: 400;\">b<\/span><\/sub><span style=\"font-weight: 400;\">a then T(n) = \u0398(n<\/span><sup><span style=\"font-weight: 400;\">logba<\/span><\/sup><span style=\"font-weight: 400;\">)<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If f(n) = \u0398(n<\/span><sup><span style=\"font-weight: 400;\">c<\/span><\/sup><span style=\"font-weight: 400;\">) where c = log<\/span><sub><span style=\"font-weight: 400;\">b<\/span><\/sub><span style=\"font-weight: 400;\">a then T(n) = \u0398(n<\/span><sup><span style=\"font-weight: 400;\">c<\/span><\/sup><span style=\"font-weight: 400;\">log n)<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If f(n) = \u0398(n<\/span><sup><span style=\"font-weight: 400;\">c<\/span><\/sup><span style=\"font-weight: 400;\">) where c &gt; log<\/span><sub><span style=\"font-weight: 400;\">b<\/span><\/sub><span style=\"font-weight: 400;\">a then T(n) = \u0398(f(n))<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Let a recurrence relation be\u00a0 :\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T(n) = 2T(n\/2) + cn. Here a=2,b=2,k=1.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Log<\/span><sub><span style=\"font-weight: 400;\">b<\/span><\/sub><span style=\"font-weight: 400;\">a = log<\/span><sub><span style=\"font-weight: 400;\">2<\/span><\/sub><span style=\"font-weight: 400;\">2 =1 = k.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Complexity :\u00a0 \u0398(nlog2(n))<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example:<\/span><\/p>\n<p><b>For merge sort:<\/b><span style=\"font-weight: 400;\"> recurrence relation : T(n) = 2T(n\/2) + \u0398(n). Here, c=1 and log<\/span><sub><span style=\"font-weight: 400;\">b<\/span><\/sub><span style=\"font-weight: 400;\">a=1.\u00a0 The complexity of merge sort come out to be \u0398(n log n).<\/span><\/p>\n<h3>Advantages of Divide and Conquer Algorithm<\/h3>\n<p><span style=\"font-weight: 400;\">1. Divide and conquer successfully solved one of the biggest problems of the mathematical puzzle world,\u00a0 the tower of Hanoi. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">You might have a very basic idea of how the problem is going to be solved but dividing the problem makes it easy since the problem and resources are divided.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Is very much faster than other algorithms.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. The divide and conquer algorithm works on parallelism. Parallel processing in operating systems handles them very efficiently.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. The divide and conquer strategy used cache memory without occupying much main memory. Executing problem in cache memory which is faster than main memory.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">5. Brute force technique and divide and conquer techniques are similar but divide and conquer is more proficient<\/span><\/p>\n<h3>Disadvantages of Divide and Conquer Algorithm<\/h3>\n<p><span style=\"font-weight: 400;\">1. Most of the divide and conquer design uses the concept of recursion therefore it requires high memory management.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Memory overuse is possible by an explicit stack.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. It may crash the system if recursion is not performed properly.<\/span><\/p>\n<h3>Divide and conquer algorithm vs dynamic programming<\/h3>\n<p>Both divide and conquer and dynamic programming divides the given problem into subproblems and solves those problems. Dynamic programming is used where the same problem is required to be solved again and again, whereas divide and conquer is used where the same problem is not required to be calculated again.<\/p>\n<p>For example, in the merge sort algorithm, we don&#8217;t need to calculate the same subproblem again, but while calculating the Fibonacci series we need to calculate the sum of the past two numbers again and again to get the next number. So we use dynamic programming in this case.<\/p>\n<p>Let us see an example for better understanding.<\/p>\n<p>Arrange the letter of the word \u201cDATAFLAIR\u201d alphabetically using the merge sort technique.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-93547\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02.jpg\" alt=\"Merge Sort Example\" width=\"1000\" height=\"800\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02.jpg 1000w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02-300x240.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02-150x120.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02-768x614.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02-720x576.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02-520x416.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-images02-320x256.jpg 320w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><\/p>\n<p><strong>Writing a function in Dynamic programming to generate Fibonacci series:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">member=[ ]\r\nfibonacci(n)\r\n           If  n in member: return member[n]\r\n          else,     \r\n                  If n &lt; 2, f = 1\r\n                   else , f = f(n - 1) + f(n -2)\r\n                    mem[n] = f\r\n                    return f\r\n\r\n<\/pre>\n<p><strong>Writing a function using divide and conquer approach to generate Fibonacci series:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fibonacci(n)\r\n    If n &lt; 2, return 1\r\n    Else , return f(n - 1) + f(n -2)\r\n\r\n<\/pre>\n<h3>Max-Min problem Analysis<\/h3>\n<p>Traditional algorithm for finding max, min element in an array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">findmaxmin (a [ ])\r\nMax = a [0]\r\nMin =  a [0]\r\nFor i= 1 to n-1 do\r\n    If a[i] &gt; max then\r\n          max = a[i]\r\n    if a[i] &lt; min then\r\n          Min = a[i]\r\nreturn (max, min)\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">In the above method, the number of comparisons between elements is 2n-2.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In a divide and conquer approach for finding maximum and minimum elements, we divide the problem into subproblems and find max \/ minor for each subproblem. And in the next step, the outputs of individual subproblems are compared again to get results.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let n = size of the array<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let T (n) = time required to execute an algorithm on an array of size n. Divide terms as T(n\/2)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u20182\u2019 here tends to the comparison of the maximum with maximum and minimum with minimum as in the above example.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T(n) = T(n\/2 ) + T(n\/2) + 2\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span> <span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8230;(1)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T (2) = 1, time taken to compare two elements.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T(n\/2) = 2T(n\/2<\/span><sup><span style=\"font-weight: 400;\">2<\/span><\/sup><span style=\"font-weight: 400;\">) + 2\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">\u2026(2)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Put (2) in (1):<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T(n) = 2 [ 2T (n\/2<\/span><sup><span style=\"font-weight: 400;\">2<\/span><\/sup><span style=\"font-weight: 400;\">) + 2 ] + 2<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 2<\/span><sup><span style=\"font-weight: 400;\">2<\/span><\/sup><span style=\"font-weight: 400;\"> T( n\/2<\/span><sup><span style=\"font-weight: 400;\">2<\/span><\/sup><span style=\"font-weight: 400;\">) +2<\/span><sup><span style=\"font-weight: 400;\">2<\/span><\/sup><span style=\"font-weight: 400;\"> + 2<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Applying\u00a0 the same procedure recursively on each subproblem we get:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T(n) = 2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> T( n\/2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> ) + 2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> + 2<\/span><sup><span style=\"font-weight: 400;\">i-1<\/span><\/sup><span style=\"font-weight: 400;\"> + \u2026\u2026\u2026\u2026\u2026. + 2\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span> <span style=\"font-weight: 400;\">\u2026(3)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Terminate at:\u00a0 ( n\/2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> ) = 2 <\/span><span style=\"font-weight: 400;\">n = 2<\/span><sup><span style=\"font-weight: 400;\">i+1<\/span><\/sup> <span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span> <span style=\"font-weight: 400;\">&#8230; (4)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Put (4) into (3).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T(n) = 2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\">T(2) + 2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> + 2<\/span><sup><span style=\"font-weight: 400;\">i-1<\/span><\/sup><span style=\"font-weight: 400;\"> + \u2026\u2026\u2026\u2026\u2026. + 2<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\">.1 + 2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> + 2<\/span><sup><span style=\"font-weight: 400;\">i-1<\/span><\/sup><span style=\"font-weight: 400;\"> + \u2026\u2026\u2026\u2026\u2026. + 2<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> + (2(2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> &#8211; 1)) \/ (2-1)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 2<\/span><sup><span style=\"font-weight: 400;\">i+1<\/span><\/sup><span style=\"font-weight: 400;\"> +2<\/span><sup><span style=\"font-weight: 400;\">i<\/span><\/sup><span style=\"font-weight: 400;\"> &#8211; 2<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= n + n\/2 &#8211; 2<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 3n\/2 &#8211; 2<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The number of comparisons required after the divide and conquering approach on n elements\/items = ( 3n\/2 &#8211; 2 ). This is less than the no. of comparisons in a traditional approach.<\/span><\/p>\n<p><b>For example, <\/b><span style=\"font-weight: 400;\">array size=11<\/span><\/p>\n<p><b>Traditional approach:<\/b><span style=\"font-weight: 400;\"> (2n-2) = 20 comparisons<\/span><\/p>\n<p><b>Divide and conquer approach:<\/b><span style=\"font-weight: 400;\"> ( 3n\/2 &#8211; 2 )= 14.5 \u2248 15 comparisons.<\/span><\/p>\n<h3>Applications of Divide and Conquer<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-93988\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01.jpg\" alt=\"Applications of Divide and conquer\" width=\"1200\" height=\"580\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01-300x145.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01-1024x495.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01-150x73.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01-768x371.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01-720x348.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01-520x251.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-normal-image-01-320x155.jpg 320w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p><b>1. Binary search:<\/b><span style=\"font-weight: 400;\"> Binary search is the fastest search algorithm requiring the only condition that the elements in the array are already arranged either in ascending or descending order. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Also known as half interval search, it compares the search element to the middle element. If the search element is less than the middle element, it selects the first half otherwise the second half. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">The same process is recursively applied on the selected half of the array until the search element is found.<\/span><\/p>\n<p><b>2. Bubble sort: <\/b><span style=\"font-weight: 400;\">Compare each pair of elements, and if they are not in order, then swap them. This sorting technique has a worst-case time complexity of O(n<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">) and therefore it is not suitable for large data sets. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">While sorting, the last element is the first one to settle at its final position, and hence the modified algorithm does not include the last iteration in the loop after each iteration.<\/span><\/p>\n<p><b>3. Quicksort:<\/b><span style=\"font-weight: 400;\"> This most efficient sorting algorithm selects a pivot value from an array and divides the rest of the array elements into two subarrays. It compares the pivot element to each element and sorts the array recursively.<\/span><\/p>\n<p><b>4. Merge sort:<\/b><span style=\"font-weight: 400;\"> This algorithm divides the array into sub-arrays and then recursively sorts them. After sorting it merges them back.<\/span><\/p>\n<p><b>5. Strassen\u2019s algorithm:<\/b><span style=\"font-weight: 400;\"> Named after Volker Strassen, this is an algorithm for matrix multiplication that is much faster than a traditional algorithm used for large matrices.\u00a0 <\/span><\/p>\n<p><span style=\"font-weight: 400;\">This method implies 7 recursive calls. It divides the matrices of order N into sub-matrices of order N\/2 and calculates the sub-matrices of resultant matrices according to the following equations:\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">p1 = a(f-h) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 p2 = (a + b)h\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">p3 = (c + d)e \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 p4 = d(g- e)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">p5 = (a + d)(e+ h)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 p6 = (b-d)(g+h)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">p7 = (a-c)(e+f)<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-93614\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11.png\" alt=\"Divide and Conquer Algorithm\" width=\"1999\" height=\"182\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11.png 1999w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11-300x27.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11-1024x93.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11-150x14.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11-768x70.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11-1536x140.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11-720x66.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11-520x47.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/image4-11-320x29.png 320w\" sizes=\"auto, (max-width: 1999px) 100vw, 1999px\" \/><\/a><\/p>\n<p><b>6. Cooley Tukey Fast Fourier Transform:<\/b><span style=\"font-weight: 400;\"> Named after J.W. Cooley and John Turkey. It is the most common FFT algorithm based on the divide and conquer approach. It works in O(n log n)\u00a0 time complexity.<\/span><\/p>\n<p><b>7. Closest pair of points:<\/b><span style=\"font-weight: 400;\"> This is a problem of computational geometry focusing on finding out the closest pair of points in metric space, given n point, such that the distance between the pair of points is minimum.<\/span><\/p>\n<p><b>8. Karatsuba algorithm for fast multiplication:<\/b><span style=\"font-weight: 400;\"> Invented by <\/span><span style=\"font-weight: 400;\">Anatoly Karatsuba<\/span><span style=\"font-weight: 400;\"> in late 1960, this is one of the fastest multiplication algorithms of the traditional time. It performs multiplication of two n digit numbers in at most a single-digit multiplication in general.\u00a0 <\/span><\/p>\n<p><span style=\"font-weight: 400;\">We divide the numbers into two halves. Let numbers be X and Y:<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">\u00a0 \u00a0X =\u00a0 Xl*2n\/2 + Xr\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">Y =\u00a0 Yl*2n\/2 + Yr<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0Xl, Yl: contains leftmost N\/2 bits of numbers X and Y respectively.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Xr, Yr : contains rightmost\u00a0 N\/2 bits of numbers X and Y respectively<\/span><\/p>\n<p><span style=\"font-weight: 400;\">XY = 2n XlYl + 2n\/2 * [(Xl + Xr)(Yl + Yr) &#8211; XlYl &#8211; XrYr] + XrYr<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The complexity of the above equation is O(n^<\/span><span style=\"font-weight: 400;\">1.59<\/span><span style=\"font-weight: 400;\">) which is better than the time complexity of the traditional approach (O(n^<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)).<\/span><\/p>\n<h3>Conclusion<\/h3>\n<p>In this article, we observed some real-life applications of the divide and conquer approach for designing algorithms. We also solved an example for merge sort. After this article, we are now aware of the differences between the divide and conquer approach and the dynamic programming approach.<\/p>\n<p>Solving recurrence relations to calculate the time complexity is also covered here. We also learned about the types, and advantages, and disadvantages of divide and conquer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever been a part of the annual fest organizing committee at your college or your institution? If not, allow me to tell you how to organize a large-scale annual fest efficiently at&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":93987,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24020],"tags":[24199,24200],"class_list":["post-93540","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structure-tutorials","tag-divide-and-conquer-algorithm","tag-max-min-problem-analysis"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Divide and Conquer Algorithm - DataFlair<\/title>\n<meta name=\"description\" content=\"In divide and conquer approach, the problem, is divided into smaller sub-problems &amp; then each problem is solved independently. Learn about it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Divide and Conquer Algorithm - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In divide and conquer approach, the problem, is divided into smaller sub-problems &amp; then each problem is solved independently. Learn about it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-07T03:30:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-26T13:52:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Divide and Conquer Algorithm - DataFlair","description":"In divide and conquer approach, the problem, is divided into smaller sub-problems & then each problem is solved independently. Learn about it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/","og_locale":"en_US","og_type":"article","og_title":"Divide and Conquer Algorithm - DataFlair","og_description":"In divide and conquer approach, the problem, is divided into smaller sub-problems & then each problem is solved independently. Learn about it.","og_url":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-05-07T03:30:24+00:00","article_modified_time":"2021-06-26T13:52:56+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Divide and Conquer Algorithm","datePublished":"2021-05-07T03:30:24+00:00","dateModified":"2021-06-26T13:52:56+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/"},"wordCount":1700,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-1.jpg","keywords":["Divide and Conquer Algorithm","Max Min Problem Analysis"],"articleSection":["Data Structure Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/","url":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/","name":"Divide and Conquer Algorithm - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-1.jpg","datePublished":"2021-05-07T03:30:24+00:00","dateModified":"2021-06-26T13:52:56+00:00","description":"In divide and conquer approach, the problem, is divided into smaller sub-problems & then each problem is solved independently. Learn about it.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/05\/Divide-and-conquer-1.jpg","width":1200,"height":628,"caption":"Divide and conquer algorithm"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/divide-and-conquer-algorithm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Data Structure Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/data-structure-tutorials\/"},{"@type":"ListItem","position":3,"name":"Divide and Conquer Algorithm"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/93540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=93540"}],"version-history":[{"count":8,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/93540\/revisions"}],"predecessor-version":[{"id":97805,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/93540\/revisions\/97805"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/93987"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=93540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=93540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=93540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}