GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
177 views

asked in Algorithms by Boss (6.5k points)   | 177 views

1 Answer

+6 votes
Best answer
As far as space complexity is concerned , first we consider those algo as having less space requirement which are inplace and hence no auxilliary space is required.

So bubble sort and selection both are simple are simple iterative sorting algos , so they will consume space mainly for the input array only.

So  1  = 2

Now coming to quicksort and mergesort ,

space required for input array = O(n)

for function call stack = O(logn)  [using tail recursion optmisation we can reduce the space complexity of function call for quicksort to O(logn) and for merge sort it is always O(logn) ]

The difference hence comes in the fact that mergesort requires auxilliary array for merge operation whereas quicksort does not require any auxilliary array as it is an inplace algorithm.

Hence space complexity of quicksort < mergesort

Therefore the correct order is : 1 = 2 < 4 < 3

Hence A) should be the correct option.

For reference on tail recursion optimisation , plz refer :

http://www.geeksforgeeks.org/quicksort-tail-call-optimization-reducing-worst-case-space-log-n/
answered by Veteran (65.9k points)  
selected by
[using tail recursion optmisation we can reduce the space complexity of function call for quicksort to O(logn) and for merge sort it is always O(logn) ]

What does it mean...after optimisation what is the space complexity?

For mergesort the recurrence is always :

T(n) = 2T(n/2) + O(n)

So the height of the recursion tree which decides the number of function instances in function call instances will remain O(logn) only.

And for tail recursion optimisation method for quicksort , plz refer :

http://www.geeksforgeeks.org/quicksort-tail-call-optimization-reducing-worst-case-space-log-n/

okk..thank you


Top Users Apr 2017
  1. akash.dinkar12

    3514 Points

  2. Divya Bharti

    2546 Points

  3. Deepthi_ts

    2040 Points

  4. rude

    1966 Points

  5. Tesla!

    1768 Points

  6. Shubham Sharma 2

    1610 Points

  7. Debashish Deka

    1588 Points

  8. Arunav Khare

    1454 Points

  9. Kapil

    1424 Points

  10. Arjun

    1420 Points

Monthly Topper: Rs. 500 gift card

22,076 questions
28,042 answers
63,234 comments
24,135 users