Quick sort

Discussion in 'Programmer's Corner' started by Dritech, Jan 11, 2015.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

    I am using the quick sort algorithm in this link: http://www.softwareandfinance.com/CSharp/QuickSort_Recursive.html

    Why is this sorting method working with strings but not with numbers?
    When sorting numbers using this method, I am getting the following error:

    "Process is terminated due to StackOverFlowException"

    Any help would be highly appreciated.
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    What language?

    Where is your code? Remember to use code tags.
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    There example is for numbers.

    In general, all you need to sort any list is a means of deciding whether item A is "greater" than item B.

    When you use their code as is and without any modification at all, what do you get?
     
    Last edited: Jan 11, 2015
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988

    OP said using "algorithm" . That suggests to me that OP is not using actual code but just the algorithm of the code. Could be any language and yes including C#.
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    Good point.

    All the more important to post the actual code -- well presented and commented -- so that we don't have to use our crystal balls to read his mind.
     
  6. amilton542

    Active Member

    Nov 13, 2010
    494
    64
    Because q-sort is primarily a string based algorithm.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Really? When in the past 55 years did they change it? Quick Sort can sort any kind of object. You just need to supply the proper comparison function.
     
    vpoko and djsfantasi like this.
  8. vpoko

    Member

    Jan 5, 2012
    258
    47
    Not true, it will sort anything where a comparison can be made between any 2 objects.
     
  9. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    Q-sort is essentially a generic algorithm. IIRC correctly (and it has literally been decades since I used it) the qsort function in the standard C library requires you to supply a pointer to a function that takes two arguments and returns an integer value that is <0, 0, or >0 depending on the relative order that the two items should be placed in. This is one of the reasons for the nature of the output of the strcmp() family of functions.
     
  10. amilton542

    Active Member

    Nov 13, 2010
    494
    64
    The reason why I said that (and how unqualified I am to be even contributing in this subset of a forum) is that I was writing code one day to sort numbers alone and I was trying to apply the same techniques but to strings and the guy just said that will never work you need to use the q-sort algorithm if that's what you're trying to achieve and the chap I am I just took his word for it.
     
  11. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,790
    827
    The error the TS is getting is "StackOverFlowException". He doesn't state which algorithm from that link he is using, but I note that one is a recursive algorithm. If that is what he is using, and depending on the actual data being sorted (which determines the number of recursions) and depending on what platform (memory restrictions) - recursion lends itself to stack overflows. Again, not enough information and the TS hasn't returned yet. As WBahn stated, I would like to see the code and his data in addition.
     
  12. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    A good way to learn is to engage in a discussion and put forth your best ideas and believes and have them be challenged and corrected.
     
Loading...