DEV Community

Jack Cole
Jack Cole

Posted on • Edited on

2 1

Learning How to Divide and Conqueror

Last week I discussed the concept of memoization. This week I wanted to continue discussing concepts needed for dynamic programming and focus on using the method of divide and conqueror.

What Is Divide and Conqueror

Divide and conqueror is an algorithm paradigm based on multi branch recursion. More specifically you take the initial problem and breaking it down into sub-problems until the sub-problems are simple enough to effectively solve. The solutions of the sub-problems are then combined to form the solution of the original problem.

Divide and conqueror is often used to solve problems such as sorting and multiplying large numbers. To show an example I'll show the implementation of merge sort and quick sort. Since I've previously covered these in a post on sorting algorithms this time I've coded them in Ruby!

Remember that these methods are going to be following the following three steps.

  • Divide the problem into sub-problems
  • Conqueror the sub-problems
  • Merge the results of the sub-problems into the final solution

In my implementation I use two functions to clearly illustrate the process of dividing and conqueror. The first method divides the input into smaller arrays that can easily be sorted. The next methods then sorts the smaller arrays and then combines the sorted numbers to return the solution.

Merge Sort Divide

Merge Sort Sort

In the implementation of quick sort these concepts are also applied in a single method.

Quick Sort

Thanks for reading! The code for this lesson can be found here.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay