Go Back   TestMagic Forums > Test preparation > GRE Subject Tests > GRE Computer Science
Register Forum Rules FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 2003 October 12th, 06:06 PM   #1 (permalink)
TestMagic Guru-in-Training
 
Join Date: Oct 2003
Location: USA
Posts: 508
nonevent99 has disabled reputation
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?


nonevent99 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2003 October 13th, 09:59 AM   #2 (permalink)
Eager!
 
Join Date: Oct 2003
Location: India
Posts: 35
Laks has disabled reputation
Hey there... selection does depend on the ordering of the input does it not???

Cheers'
Laks

Nothing endures but change
Laks is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2003 October 15th, 05:27 AM   #3 (permalink)
Here I am !!
 
The_Wonderful_Vipul's Avatar
 
Join Date: Feb 2003
Location: New York
Posts: 165
The_Wonderful_Vipul has no rep yet.
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/
The_Wonderful_Vipul is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2003 October 20th, 05:03 PM   #4 (permalink)
I JUST got here.
 
Join Date: Oct 2003
Posts: 1
jobel has disabled reputation
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.
jobel is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2003 October 21st, 11:31 AM   #5 (permalink)
TestMagic Guru-in-Training
 
Join Date: Oct 2003
Location: USA
Posts: 508
nonevent99 has disabled reputation
That's very interesting, because ETS's answer key claims that the answer is merge sort (c)

How can this be?
nonevent99 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2003 October 23rd, 11:01 AM   #6 (permalink)
vik
I JUST got here.
 
Join Date: Sep 2003
Location: USA
Posts: 20
vik has disabled reputation
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?
vik is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2003 October 23rd, 12:32 PM   #7 (permalink)
Here I am !!
 
The_Wonderful_Vipul's Avatar
 
Join Date: Feb 2003
Location: New York
Posts: 165
The_Wonderful_Vipul has no rep yet.
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.
The_Wonderful_Vipul is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2003 October 23rd, 03:53 PM   #8 (permalink)
TestMagic Guru-in-Training
 
Join Date: Oct 2003
Location: USA
Posts: 508
nonevent99 has disabled reputation
Quote:
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.)
nonevent99 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2009 October 17th, 10:54 PM   #9 (permalink)
I JUST got here.
 
Join Date: Oct 2009
Posts: 2
dwarf just joined TestMagic.
Referring to the question 52 GR 9629 (As given in the first post of this thread),

The answer given in the test book (merge sort) to this question directly contradicts wikipedia's articles on selection sort and merge sort.

Check the Selection Sort article on Wikipedia,
["It can be seen as an advantage for some real-time applications that selection sort will perform identically regardless of the order of the array,..."]

Even if we analyse the algorithms, observe that algorithm MERGE used in merge sort that sorts two sorted lists makes between 'n' and '2n - 1' comparisons for merging two sorted lists of size 'n' each.

Try merging 1 2 3 4 and 5 6 7 8.
Now try merging 1 3 5 7 and 2 4 6 8.

This results in a best case recurrence
T(n) = 2T(n/2) + n/2

and a worst case recurrence
T(n) = 2T(n/2) + n - 1

Although the solutions to the recurrences will result in the same complexity class, they will differ by a constant factor if solved exactly by iteration.

As for selection sort, it makes the exactly the same number of comparisons no matter whatever the input (Reason: two for loops).

If the running time of an algorithm is dominated by the number of comparisons it makes, then the answer to this question is clearly SELECTION SORT. (This is how we decide the big-Oh complexity of almost all sorting algorithms, isn't it?)

ETS's answer is MERGE SORT.

Any comments? <No variants of these algorithms please>
dwarf is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2009 October 18th, 07:31 PM   #10 (permalink)
Eager!
 
Join Date: May 2008
Posts: 39
Enigma211 just joined TestMagic.
I think ETS's answer is based one the fact that best, avg and worst case scenarios on multiple input for a merge sort is O(nlogn).

I agree that strictly based on comparisons, selection sort does n^2 comparisons regardless of the input. But the hidden constant cost required to swap the numbers could make the difference in the best and worst case scenario. The cost difference is n^2 vs n^2+n . The additional cost n for n swaps.

Thoughts ?
Enigma211 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

What you can do
You cannot post new threads
You cannot post replies
You cannot post attachments
You cannot edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:52 AM.

Contact TestMagic   TestMagic Forums      Archive   Privacy Statement

TestMagic Locations   Legal   Privacy


SEO by vBSEO 3.2.0
Copyright © 2009 TestMagic
Ad Management by RedTyger

Scroll Up