Thread: problem in divide and conquer approach to find max-min

  1. #16
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    i must say, some posts laserlight is cryptic, others he is right to the point, speaking in 2nd grade phrases, and people still sit there and go...."huh????"

    lol

    right off the bat, i see one big problem, your array is set for 20, then you ask to inputhow many are going in the array, with no error checking to make sure the # of inputs dont excede the array size.

    another problem, is that you didnt post your complete code from beginning to end (yes your end is missing), your code is a mess, inconsistant spacing and tabs, and etc

    BUT here is your major problem, to find min and max in a full array, that does not sort them first, you CANNOT half it. You either have to sort the entire array into numerical order, then min max at the beginning array[0], and the end array[n]

    ===OR===

    you have to search one by one, through the entire array, array[0] to array[n]

    in plain english, your trying to cut the search time in half, by skipping half of what you need to do...it isnt your compilier, it is your programming logic.

    someone please correct me if i am wrong, or i missed something in the code snippits.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Crossfire, there IS actually a "fast select" algorithm to find what he wants. It has logic which seems vaguely similar to Quicksort, except it's faster, because it doesn't sort the array - it only finds the i'th value, within an array. It's complexity iirc is O(n), which is to say it's blinding fast.

    I haven't used it before, but happened to stumble across it when trying to create a new sorting algorithm. It uses a partitioning scheme to split the array.

    I'll get some more info on it, and post back.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    there IS actually a "fast select" algorithm to find what he wants. It has logic which seems vaguely similar to Quicksort, except it's faster, because it doesn't sort the array - it only finds the i'th value, within an array. It's complexity iirc is O(n), which is to say it's blinding fast.
    Yes, O(n) in the average case. However, that algorithm is more for the case where you trying to find some middle position element (hence it is the most likely candidate to implement the C++ standard library's std::nth_element generic algorithm). For the case of min and max, it would be overkill.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, this is the link to QuickSelect.

    Selection algorithm - Wikipedia, the free encyclopedia

    Which is substantially faster selecting than sorting the array with QuickSort.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    Which is substantially faster selecting than sorting the array with QuickSort.
    Yeah, but I expect it to be slower than just doing linear search for the max and min (either simultaneously, or with two linear searches), in the average case. Furthermore, linear search runs in linear time in the worst case, whereas the algorithm that you have in mind runs in quadratic time in the worst case. So, I can see no advantage in implementing it here, except maybe as an exercise since it is not so trivial to implement as linear search (and qualifies as divide and conquer).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    Quote Originally Posted by ashutosh124 View Post
    Adak, the two algos to find the max-min that you talked about are straightforward and I know this. What I want to do is to implement the "divide and conquer approach" to it. But your such a long post ends up prematurely without pointing the bug in it or suggesting a way out.

    Wow, ashutos. This was so unnecessary. Instead of just saying 'thanks' to stahta01, who actually pointed out the bug you could not find, you criticize Adak, who gave you other advise, but did not point out the bug.


    Your bug was like a big red flag for any experienced programmer with patience enough to read your whole program. But something makes it invisible: it is preceded by several larger red flags.


    In first place, the assignment sucks. I hope your teacher made clear that this is just a 'conceptual' exercise and that, in The Real World, it is a bad idea to apply divide-and-conquer to this particular problem. The naive solution takes a time proportional to the size of the array. No sequential algorithm can beat this, and your solution must be, at least, several times slower. I hope that your teacher gives you several examples of cases where divide-and-conquer is good.


    You didn't even describe the problem you were having when you started the thread! And this badly indented code... You used global variables...


    With your last comment you just showed that std10093 was right with his attitude of not wasting time in trying to help you.

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It was an honest answer, so I appreciate it. I thought he was looking for a min-max finding program - which he is not. He's looking for a recursive Quickselect-like program, ONLY. IF I understand him correctly, now.

    Unfortunately, I am not familiar with this algorithm, but it motivates me to study that algorithm pseudo code, over on the link I gave (Wikipedia). I know partitioning is very fast (I was using that in a new sorting algorithm extensively), but I'm not clear why it would be better to use than a plain vanilla linear search.

    Maybe it's not. I'm going to time them both, and see.

  8. #23
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    from what i read and understood, the way it devides the array into groups, (dont know if it sorts or not, but they do mention move alot) the groups being looked at are compaired, then compairs to another ect ect ect...i may be totally ass backwards and wrong. But it seems the grouping and excluding other groups (and maybe moving) cuts the time down compaired to just starting at 0 and going to xxx. But IF the search isnt perfect, it is like laserlight said, could take up to 4 times longer then 0-xxx.

    to be honest i have always done the sort then min/max, or just the 0-xxx. The other is grated more complex and in theory can make a quick search of huge arrays, but it is to me too complicated, and too much code to be practical for most c programs. Unless your array has like 100K + records!!!

  9. #24
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    Quote Originally Posted by Adak View Post
    It was an honest answer, so I appreciate it. I thought he was looking for a min-max finding program - which he is not. He's looking for a recursive Quickselect-like program,[..]
    I think he was asked to make a program to find min-max with a divide-and-conquer approach. The goal would be to learn how divide-and-conquer works, not to get a fast min-max finding algorithm.

    If the elements are in a normal array, not sorted, and there's no additional data... the only way to beat the min-max plain search is using some kind of parallelism.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    I thought he was looking for a min-max finding program - which he is not. He's looking for a recursive Quickselect-like program, ONLY. IF I understand him correctly, now.
    Dunno, a "divide and conquer approach to find max-min" does not really mean anything to me since I wouldn't use divide and conquer for this. A "recursive Quickselect-like program" would qualify, but what's the point? If a "recursive Quickselect-like program" is the answer, then the question is wrong: ashutosh124 should change the question. Otherwise, ashutosh124 should describe to us in words just what he/she has in mind by a "divide and conquer approach to find max-min".

    Quote Originally Posted by Adak
    I'm not clear why it would be better to use than a plain vanilla linear search
    It isn't for finding min, max or both, but it is better when you are trying to find say, the median element of an unsorted array. If you just use "plain vanilla linear search", then you need about n/2 linear searches before you find the median element. This means that you have an algorithm that runs in quadratic time: clearly inferior to this linear time algorithm in the average case. Or, you could do a partial sort of half the array, but then using comparison based sorting, you have an algorithm that runs in O(n log n), still inferior to "quick select".

    Quote Originally Posted by Crossfire
    from what i read and understood, the way it devides the array into groups, (dont know if it sorts or not, but they do mention move alot) the groups being looked at are compaired, then compairs to another ect ect ect..
    Not quite: if you understand quicksort style partitioning, then the basic idea is simple: pick a pivot, then partition the range according to the pivot. If the pivot is less than the element that you want to select, then clearly that element must lie in the partition of elements greater than the pivot, so you pick a pivot in that partition, etc. Otherwise, the element lies in the other partition, so you pick a pivot in that partition, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-25-2012, 09:50 PM
  2. Divide and Conquer Merge_sort
    By nimitzhunter in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 06:39 PM
  3. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 07:31 PM
  4. divide and conquer
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-13-2002, 09:52 AM