GeeksforGeeks » Interview Questions

Microsoft Interview Question about Algorithms, Arrays

(5 posts)
  1. hina
    guest
    Posted 1 year ago #

    How QuickSort is good in Virtual Memory Enviroment ?

  2. Dev
    guest
    Posted 1 year ago #

    quicksort is better than heapsort in VM(avg. case)
    Directly equating space requirement with performance in a virtual memory environment is a fallacy...

    if you look at the algorithm for quicksort, it operates on subarrays (i.e contiguous elements) and compares them in a linear manner (i.e a[j] with pivot, a[j+1] with pivot etc.)... the way the computer works is that it first looks up the cache when trying to access a[j]... the first time a[0] is accessed, there is a cache miss and a[0] has to be fetched from the VM hierarchy... but caches are built to take advantage of spatial locality of reference, which means that when a[0] is fetched a[1], a[2], a[3], a[4] etc.. are fetched as well... quicksort (in the next iteration of the loop) will access a[1] which will be in the cache! Therefore there is a large reduction in number of cache misses... in this respect it is better than Heapsort in which, if you look at the algorithm in CLRS, comparisons are made between a[left(j)], a[right(j)] and a[j] all over the algorithm... now a[left(j)] is a[2*j], so actually you are comparing elements which are 'j' apart... needless to say a[2*j] will not always be fetched when a cache miss occurs for a[j], so you end up having two cache misses per comparison...this is magnified because a cache miss occurs in quicksort only once every 4 or more elements....

    the second point is that the quicksort algorithm itself can be tuned quite nicely... it's difficult to get this point across to ppl who have not implemented it in assembly language (i did this in my btech)... all variables and the pivot can be placed inside registers, and you can rewrite the basic quicksort algorithm to have one recursive call (see exercises in CLRS)...

    Courtesy to Satish (http://www.orkut.co.in/Main#CommMsgs?cmm=22317&tid=2547900341623167061&kw=Virtual+Memory+Enviroment&na=3&nst=11&nid=22317-2547900341623167061-2548070680005420721)

  3. kster
    Member
    Posted 1 year ago #

    Dev, it is a great post :-)

  4. MOZHI
    guest
    Posted 7 months ago #

    nice post

  5. tanmaydude
    Member
    Posted 7 months ago #

    Dev nice post bro!!!
    i want to know what is the chance that worst case occurs in quicksort?


Reply

You must log in to post.

RSS feed for this topic