+ Reply to Thread
Results 1 to 8 of 8

Thread: GRE Practice Test Q 52 - Sort Run Time wrt Input Ordering

  1. #1
    TestMagic Guru-in-Training nonevent99 has disabled reputation
    Join Date
    Oct 2003
    Location
    USA
    Posts
    508
    Of the following sorting algorithms, which has a running time that is LEAST dependent on the initial ordering of the input?

    (a) Insertion Sort
    (b) Quick Sort
    (c) Merge Sort
    (d) Selection Sort
    (e) Shell Sort



    Insertion sort is O(n) best case, O(n^2) worst case.
    Quick sort is O(n lg n) best case, O(n^2) worst case.
    Merge sort is O(n lg n) best case, O(n lg n) worst case.
    Selection sort is O(n^2) best case, O(n^2) worst case.
    Shell sort is at least as good as O(n^1.25) best case, O(n^1.5) worst case.

    So best[merge] = worst[merge] and best[selection] = worst[selection]. Why aren't they equally good answers?



  2. #2
    Eager! Laks has disabled reputation
    Join Date
    Oct 2003
    Location
    India
    Posts
    35
    Hey there... selection does depend on the ordering of the input does it not???

    Cheers'
    Laks

    Nothing endures but change

  3. #3
    Here I am !! The_Wonderful_Vipul has no rep yet. The_Wonderful_Vipul's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    165
    Hey ,

    the thing is that take 2 cases : 1, where the input is sorted and the other where it is the worst case .

    Which of the algos' running time varies the least between the two cases i.e. doesn't depend if the input is sorted in any way or not./

    Vipul/

  4. #4
    Trying to make mom and pop proud jobel has disabled reputation
    Join Date
    Oct 2003
    Posts
    1
    The answer is selection sort because it ALWAYS runs the same, regardless of input. Look at the code, for sorting integers:

    void
    selection( int *items, int length )
    {
    for ( int I = 0; I < length - 1; ++i )
    {

    int low = i;
    for ( int j = i+1; j < length; ++j )
    {
    if ( items[ j ] < items[ low ] ) low = j;
    }

    int tmp = items[ low ];
    items[ low ] = items[ I ];
    items[ I ] = tmp;
    }
    }

    You can see the number of operations is independent of the order of the initial ordering. It first searches items 1..n for the smallest, puts it in slot 0, then searches 2..n, puts it in slot 1, etc.

  5. #5
    TestMagic Guru-in-Training nonevent99 has disabled reputation
    Join Date
    Oct 2003
    Location
    USA
    Posts
    508
    That's very interesting, because ETS's answer key claims that the answer is merge sort (c)

    How can this be?

  6. #6
    vik
    vik is offline
    Trying to make mom and pop proud vik has disabled reputation
    Join Date
    Sep 2003
    Location
    USA
    Posts
    20
    you guys are right. I have an idea but it's kinda extreme:
    assume an input where all values are identical. Obviously, it's already sorted.
    The selection sort with a tiny improvement can run O(n) on this input, while merge sort still runs its O(n log n).

    The improvement - remember the last element of the sorted part of the input. Inside the selection run, stop if you find an equal element.

    What do you think?

  7. #7
    Here I am !! The_Wonderful_Vipul has no rep yet. The_Wonderful_Vipul's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    165
    now look guyz,

    the answer merge sort is indeed correct.

    suppose you use insertion sort. if the array is already sorted, you dont have to shift any elements and it runs in O(n) time. however, if the array is in descending order, you have to shift for each element and the complexity is O(n^2).

    consider merge sort. it is a divide n conquer technique and it keeps on dividing the input into smaller sets of elements until only one element in each set remains. it doesn't matter if the input was sorted o not because the number of operations performed would be the same. Divide into n sets and merge them.

    pls assk if you hv any doubts.
    hth,
    Vipul.

  8. #8
    TestMagic Guru-in-Training nonevent99 has disabled reputation
    Join Date
    Oct 2003
    Location
    USA
    Posts
    508
    Originally posted by vik

    you guys are right. I have an idea but it's kinda extreme:
    assume an input where all values are identical. Obviously, it's already sorted.
    The selection sort with a tiny improvement can run O(n) on this input, while merge sort still runs its O(n log n).

    The improvement - remember the last element of the sorted part of the input. Inside the selection run, stop if you find an equal element.

    What do you think?
    That seems like a reasonable improvement, though I don't remember ever seeing it in a textbook. (I have seen a roughly equivalent enhancement to bubble.)

+ Reply to Thread

Similar Threads

  1. I am really bad at this sort of question, pls help
    By praguish in forum GMAT Critical Reasoning
    Replies: 11
    Last Post: 12-05-2007, 03:37 AM
  2. Please help me sort out my life (Thank you)
    By John E Coissac in forum PhD in Economics
    Replies: 10
    Last Post: 10-30-2006, 06:32 PM
  3. Right time to take the FPGEE practice test
    By rphexams in forum FPGEE
    Replies: 0
    Last Post: 06-17-2006, 12:55 AM
  4. Replies: 1
    Last Post: 04-07-2006, 03:58 PM
  5. what sort is this?
    By nonevent99 in forum GRE Computer Science
    Replies: 2
    Last Post: 11-02-2003, 10:12 PM

Bookmarks

What you can do

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO 3.5.0 RC2