Divided Conquest Conceptual Understanding Question.

Asked 2 years ago, Updated 2 years ago, 109 views

In the recursive function problem, many people use an algorithm called segmentation conquest.

Stop and sum up by continuing to divide a big problem and get a full answer to a small one

I think it's called divisional conquest.

But I don't understand even if I look at the example.

I would appreciate it if you could give me a simple example and understand the concept.

c recursive

2022-09-20 21:38

3 Answers

The following is an example of an array [3, 7, 6, 1, 9, 3, 2] with seven numbers in ascending order.

The problem, which used to be seven number alignment, has been simplified to be two number alignment (A) and four number alignment (B) problems. For arrays of A, you can also compare them immediately to determine the order because there are two numbers to sort.

At the end of Phase 2, when tied together, it looks like this.

(1, [2]), 3, ([6, 3], 7, [9])

Repeat the same operation until the generated array length becomes one.

1, (2), 3, ([3], 6), 7, 9

Ascending sort completed.

With this divisional conquest, you can break up big problems (difficult problems) into small problems (easy problems) and combine them to solve difficult problems only with easy problems.

Thank you.


2022-09-20 21:38

Not all recursive functions are used to solve segmental conquests, and segmental conquests can only be implemented as recursive functions. Recursive functions are simply tools.

For example, if you're going to use a recursive function to create a program that inversely outputs an odd number of n or less, you can make it like this:

#include <stdio.h>
void oddPrint(int i);
int main(){
    oddPrint(8);
    return 0;
}
void oddPrint(int i){
    if (i<0)
        return;
    if (i%2 != 0){
        printf("%d\n",i);
    }
    i--;
    oddPrint(i);
}

The segmentation and conquest problem can also be created in stages, such as Phase 1 and Phase 2, through a loop.

A recursive function is a function that simply calls itself.

The reason why we write segmentation conquest as a recursive function is that it's easy to teach about the usefulness of the recursive function and it's convenient to write segmentation conquest in a way that's easy for people.

In fact, I understand that recursion functions are not recommended because your computer is wasting a lot of resources.

Thank you.


2022-09-20 21:38

Let me give you a simple example of everyday life.

I'm going to decide the first place among 32 teams.

Let's make two groups of 16 teams. The person who wins the first place in each group will be the winner.

Now, in each group,

I'm going to decide the winner out of 16 teams.

Let's make two groups of 8 teams. The person who wins the first place in each group will be the winner.

Now, in each group,

I'm going to decide the first place among the eight teams.

Let's make two groups of four. The person who wins the first place in each group will be the winner.

...


2022-09-20 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.