Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is imperative Quicksort in situ (in-place) or not?

Quicksort is often described as an in situ (in-place) algorithm, despite the fact that it requires O(log n) stack space. So does in situ mean "requires less than O(n) additional space", or does stack space generally not count as space complexity (but why would that be the case?), or is Quicksort actually not an in situ algorithm?

like image 422
fredoverflow Avatar asked Feb 01 '12 13:02

fredoverflow


People also ask

Is quick sort in situ?

Quicksort is an in-place, or in-situ sorting algorithm. It doesn't need other arrays or data structures, except a few local variables. However, quicksort does require space on the call stack for the frames for each call on it.

Why quick sort is not in-place?

If you define "in-place" as requiring a constant amount of memory, then quicksort is not "in-place" as it requires log(N) memory for the recursion. If you define "in-place" as more human-friendly "does not move the data outside the input structure", then quicksort is again not "in-place".

Is quicksort stable is quicksort an in-place algorithm?

QuickSort is an unstable algorithm because we do swapping of elements according to pivot's position (without considering their original positions).

How do you know if an algorithm is in-place?

In-place algorithmsAn in-place algorithm transforms the input without using any extra memory. As the algorithm executes, the input is usually overwritten by the output, and no additional space is needed for this operation. An in-place algorithm may require a small amount of extra memory for its operation.


2 Answers

is Quicksort actually not an in situ algorithm?

The standard implementation of it is not in situ. It's a horribly common misconception, but you as correctly note due to stack space consumption, that conception is wrong.

I say "standard implementation" of it because people have modified the algorithm to make it an O(1)-space algorithm. Here is one example: Quicksort without a stack.

Of course, consistent with the classic space-time tradeoff, such versions of quicksort are less performant than the standard implementation.

like image 93
jason Avatar answered Oct 29 '22 19:10

jason


Wikipedia offers the following definition:

In computer science, an in-place algorithm (or in Latin in situ) is an algorithm which transforms input using a data structure with a small, constant amount of extra storage space.

By this definition, the typical stack-based quicksort is clearly not an in situ algorithm.

In fact, the Wikipedia article explicitly discusses this:

An algorithm is sometimes informally called in-place as long as it overwrites its input with its output. In reality this is not sufficient (as the case of quicksort demonstrates) nor is it necessary; the output space may be constant, or may not even be counted, for example if the output is to a stream.

and

Quicksort is commonly described as an in-place algorithm, but is not in fact one. Most implementations require O(log n) space to support its divide and conquer recursion.

However, as pointed out by @Jason in his excellent answer, there appear to exist variants of quicksort that only require O(1) extra space. Any such alorithms would be considered in situ.

like image 27
NPE Avatar answered Oct 29 '22 20:10

NPE