At first I thought that was just part of your implementation and that it would be taken care of by the size parameter. You always pass the same array to your recursive calls for lsum and rsum. The response by also gives a good explanation of where my code was incurring errors.Īs haris has said, in your code you add the same number to both the right sum and the left sum however, there is a much greater problem with your code. For a function that correctly solves the problem, see M's answer. I will leave my original code here in case others are struggling with divide and conquer and might be making similar mistakes. UPDATE: Thanks to all the helpful responses, I have fixed my code and so that it compiles and runs nicely. I feel silly asking, as I know the answer is staring me right in the face, but how do I repair my function so that it returns an accurate result? For some reason, however, I cannot seem to identify a fix. I know the issue lies within my recursive call to sumArray using rsize ( a variable that is equal to the size of the original array, minus the midpoint). I have identified the problem as being that the function includes the value of lsum in its calculation of rsum. Int rsum = anArray + sumArray(anArray + mid, -rsize) Int lsum = anArray + sumArray(anArray, -mid) I have tried to use similar concepts to those I employed when writing recursive sum algorithms and a divide and conquer algorithm for identifying the maximum element in an array, but I am struggling to combine the two ideas.īelow is the code I have written for sumArray, which compiles, but does not return the correct result. This function must be done by dividing the array in half and performing recursive calls on each half. I am attempting to write a function called sumArray that computes the sum of an array of integers. We’ll explore many ways of using divide and conquer algorithms in this course, especially when we learn to sort and search through lists of values.I'm having a bit of trouble with divide and conquer algorithms and was looking for some help. In this case, it is! So, we can return that our original list did indeed include the number $19$. Here, we simply need to determine if the single item in the list is the number we are looking for. Once again, it is not, but we know that $19$ is greater than $12$, so we’ll need to look in the second half of the list.įinally, we have reduced our problem to the simplest, or base case of the problem. Once again, we ask ourselves if $12$, the centermost number in the list, is the one we are looking for. Once we’ve figured out how to divide our data, we can usually follow the same steps again to solve the smaller problems as well. This is the powerful feature of a divide and conquer algorithm. Now we can just repeat that process, this time using only the first half of the original list. In this case, since $19$ is less than $23$, we must only look at the first half of the list. Likewise, if it is greater than the middle number, it must be in the second half. If our desired number is less than the middle number, we know that it must exist in the first half of the list. Thankfully, we know the list is sorted, so we can use that to our advantage. So, we need to figure out how we can use it to divide our input into a smaller problem. Is it our desired number? Unfortunately, it is not. First, we can look at the item in the middle of the list, which is $23$. If we have a list of data that has already been sorted, as seen in the figure above, we can easily find any item in the list using a divide and conquer process.įor example, let’s say we want to find the value $19$ in that list. One great example of a divide and conquer algorithm is the binary search algorithm. By reducing the problem’s size and complexity, it becomes easier to search through each individual piece of furniture in the house, either finding our lost object or eliminating that area as the likely location it will be found. Then, within each room, we can even further subdivide the problem by looking at each piece of furniture individually. Instead of trying to search the entire house, we can subdivide the problem into smaller parts by looking in each room separately. It might even try to subdivide those smaller problems again and again to finally get to a small enough problem that it is easy to solve.Ī great real-world example of using a divide and conquer approach to solving a problem is when we need to look for something that we’ve lost around the house. First, it will divide the problem into at least two or more smaller problems, and then it will try to solve each of those problems individually. A divide and conquer algorithm works just like it sounds. The next most common algorithmic technique is divide and conquer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |