Thread: Need help to check sort

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    Need help to check sort

    Hello everyone.

    So through my journey of all these different algorithms of sorting, I came across quick sort. Which is pretty tight, does everything, very nice and everything, but I realized... quick sort's weakness... its weakness is a sorted list :P

    I got my code written and everything on getting a unsorted data sorted, but how would I check to see if the data is already sorted, and I don't wanna print it out to check..

    What if there is like 1million inputs of data... don't wanna read 1 million things :P.

    Thanks ya'll

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Double check the sorting part of your code? You have to nail that down either way, it's your only option though if you don't want to inspect some small portions of the list. Write a program to take some random samples and compare them to see if they're sorted locally...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Depending on how much time you want to spend, you can just check the first so many. Something like
    Code:
    already_sorted = true
    if (first > second)
        already_sorted = false
    else
        move first, second up one spot
        repeat
    EDIT: Although doing something like picking the pivot in a better way will really eliminate the need for this sort of thing.

  4. #4
    Registered User vassago's Avatar
    Join Date
    Apr 2010
    Posts
    6
    Depending on your data, you could try counting sort or radix. The worst you'll waste is linear time.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's your job as a programmer, to know what kind of data you'll be sorting. If the data has any reasonable chance of being in already sorted order, then use Introsort. It uses Quicksort, but also checks to see if lilttle progress is being made during the sort. When it detects that is happening, it switches over to heap sort, and very little time is wasted.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dlwlsdn
    So through my journey of all these different algorithms of sorting, I came across quick sort. Which is pretty tight, does everything, very nice and everything, but I realized... quick sort's weakness... its weakness is a sorted list :P
    This would be the case for a "canonical" version of quick sort that picks the first element of each partition as the pivot. As tabstop noted, there are other pivot selection strategies available, e.g., median of three and random. Introsort is an example of another take on the situation: when the recursion depth exceeds some limit (indicating that pathological worst case might be present), the algorithm switches to heap sort, which does not suffer from quadratic time worst case.

    Quote Originally Posted by dlwlsdn
    What if there is like 1million inputs of data... don't wanna read 1 million things
    Depending on what are the "things", 1 million might still be okay. But if it is not, you might not even want to use quick sort in that case, e.g., you might want to use a version of merge sort applied to files, and only switch to quick sort for sufficiently small partitions.
    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. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Sort two vectors in the same order
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2008, 05:49 PM
  3. choosing right sort algorithm
    By Micko in forum C++ Programming
    Replies: 15
    Last Post: 05-29-2006, 10:38 AM
  4. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  5. vector: sort multiple fields
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2005, 11:00 AM