divide-and-conquer

finding nth root of a number by using divide and conquer method

你离开我真会死。 提交于 2019-12-01 12:10:33
I need help on how to get nth root of some number. User enters number n and number he wants root of. I need to solve this without cmath lib and with divide and conquer method. Here's my code that doesn't work yet: #include<iostream> using namespace std; float pow(float a,float c){ if (a == 0) return 0; else if(a == 1) return 1; else{ float p = pow(a,(c/2)); if(c%2) return p*p*a; else return p*p; } } int main(){ float a,b; float c; cout << "Enter positive number:(base)" << endl; do{ cin >> a; }while (a < 0); cout << "Enter number: (root)" << endl; cin >> b; c = 1/b; cout << "Result:"<<pow(a,c)

finding nth root of a number by using divide and conquer method

空扰寡人 提交于 2019-12-01 11:50:59
问题 I need help on how to get nth root of some number. User enters number n and number he wants root of. I need to solve this without cmath lib and with divide and conquer method. Here's my code that doesn't work yet: #include<iostream> using namespace std; float pow(float a,float c){ if (a == 0) return 0; else if(a == 1) return 1; else{ float p = pow(a,(c/2)); if(c%2) return p*p*a; else return p*p; } } int main(){ float a,b; float c; cout << "Enter positive number:(base)" << endl; do{ cin >> a;

Understanding double recursion

你离开我真会死。 提交于 2019-11-30 20:13:52
I'm able to comprehend recursion easily if there is just one recursive call within a function. However, I am really getting confused when I see two or more recursive calls within the same function. Example: int MaximumElement(int array[], int index, int n) { int maxval1, maxval2; if ( n==1 ) return array[index]; maxval1 = MaximumElement(array, index, n/2); maxval2 = MaximumElement(array, index+(n/2), n-(n/2)); if (maxval1 > maxval2) return maxval1; else return maxval2; } I understand one thing that n gets decremented by half during each recursive call. I just don't understand how the next

Why should Insertion Sort be used after threshold crossover in Merge Sort

旧街凉风 提交于 2019-11-30 04:58:08
问题 I have read everywhere that for divide and conquer sorting algorithms like Merge-Sort and Quicksort , instead of recursing until only a single element is left, it is better to shift to Insertion-Sort when a certain threshold, say 30 elements, is reached. That is fine, but why only Insertion-Sort ? Why not Bubble-Sort or Selection-Sort , both of which has similar O(N^2) performance? Insertion-Sort should come handy only when many elements are pre-sorted (although that advantage should also

Understanding double recursion

旧巷老猫 提交于 2019-11-30 04:48:21
问题 I'm able to comprehend recursion easily if there is just one recursive call within a function. However, I am really getting confused when I see two or more recursive calls within the same function. Example: int MaximumElement(int array[], int index, int n) { int maxval1, maxval2; if ( n==1 ) return array[index]; maxval1 = MaximumElement(array, index, n/2); maxval2 = MaximumElement(array, index+(n/2), n-(n/2)); if (maxval1 > maxval2) return maxval1; else return maxval2; } I understand one

nth smallest number among two databases of size n each using divide and conquer [closed]

百般思念 提交于 2019-11-29 04:19:57
We have two databases of size n containing numbers without repeats. So, in total we have 2n elements. They can be accessed through a query to one database at a time. The query is such that you give it a k and it returns kth smallest entry in that database. We need to find nth smallest entry among all the 2n elements in O(log n) queries. The idea is to use divide and conquer but I need some help thinking through this. Thanks! Here's how I thought this through. Since it's an educational problem, I suggest you stop reading if any of the points makes you think, "aha, I hadn't thought of that, I

Why is Binary Search a divide and conquer algorithm?

五迷三道 提交于 2019-11-28 20:38:18
I was asked if a Binary Search is a divide and conquer algorithm at an exam. My answer was yes, because you divided the problem into smaller subproblems, until you reached your result. But the examinators asked where the conquer part in it was, which I was unable to answer. They also disapproved that it actually was a divide and conquer algorithm. But everywhere I go on the web, it says that it is, so I would like to know why, and where the conquer part of it is? Kenci The book Data Structures and Algorithm Analysis in Java, 2nd edtition, Mark Allen Weiss Says that a D&C algorithm should have

Divide and conquer algorithm applied in finding a peak in an array.

自古美人都是妖i 提交于 2019-11-28 13:45:27
For an array a: a 1 , a 2 , … a k , … a n , a k is a peak if and only if a k-1 ≤ a k ≥ a k+1 when 1 < k and k < n. a 1 is a peak if a 1 ≥ a 2 , and a n is a peak if a n-1 ≤ a n . The goal is to find one peak from the array. A divide and conquer algorithm is given as follows: find_peak(a,low,high): mid = (low+high)/2 if a[mid-1] <= a[mid] >= a[mid+1] return mid // this is a peak; if a[mid] < a[mid-1] return find_peak(a,low,mid-1) // a peak must exist in A[low..mid-1] if a[mid] < a[mid+1] return find_peak(a,mid+1,high) // a peak must exist in A[mid+1..high] Why this algorithm is correct? I think

How to find multiplicative partitions of any integer?

北城以北 提交于 2019-11-28 08:27:35
I'm looking for an efficient algorithm for computing the multiplicative partitions for any given integer. For example, the number of such partitions for 12 is 4, which are 12 = 12 x 1 = 4 x 3 = 2 x 2 x 3 = 2 x 6 I've read the wikipedia article for this, but that doesn't really give me an algorithm for generating the partitions (it only talks about the number of such partitions, and to be honest, even that is not very clear to me!). The problem I'm looking at requires me to compute multiplicative partitions for very large numbers (> 1 billion), so I was trying to come up with a dynamic

Difference between Divide and Conquer Algo and Dynamic Programming

╄→гoц情女王★ 提交于 2019-11-28 02:38:54
What is the difference between Divide and Conquer Algorithms and Dynamic Programming Algorithms? How are the two terms different? I do not understand the difference between them. Please take a simple example to explain any difference between the two and on what ground they seem to be similar. OneMoreError Divide and Conquer Divide and Conquer works by dividing the problem into sub-problems, conquer each sub-problem recursively and combine these solutions. Dynamic Programming Dynamic Programming is a technique for solving problems with overlapping subproblems. Each sub-problem is solved only