-3

I tried search here and there to find what exactly in-place merge sort and where I need to use it ? But not found a straight answer. Please help me by answering below.

1) When and where in-place merge is required ? Practical usage of in-place merge.

2) What happens if the input arrays to the in-place merge is not sorted ?

3) Which eats more memory to sort among Merge sort, in-place merge sort and quick sort ?

Note : I am asking regarding "std::inplace_merge" which is an stl algorithm.

  • I will be so glad if you add a comment here when you down vote my question pals. At least you should give a chance to know what is wrong with my question right. And if possible, please share the material if it is a basic question along with down voting the question. All I want is concept. Thanks. – Bharadwaj Gali Jun 07 '16 at 01:16

1 Answers1

2

1) In-place merge sort is used when you want to sort a list in O(nlogn) time while using less space than standard mergesort.

2) The whole purpose of sorting is the make the input arrays sorted, so not sorted input arrays will be sorted by the in-place mergesort.

3) Mergesort uses more memory because it creates two new arrays of half size for the two recursive calls. In-place merge sort and quick sort should take around the same space, because they are both in-place. For mergesort, in-place means O(log n) extra space from keeping the relevant indices of the array of length n, not the strictest O(1) meaning of in-place. Quicksort takes O(nlogn) extra space in the worst case because there can be O(n) recursive calls, each of which has pointers which take space O(logn).

Hope this helps.

ghui
  • 156
  • 3
  • 12
  • Careful with the wording, "in place" has two definitions. "In its strictest form, the algorithm can only have a constant amount of extra space, counting everything including function calls and pointers... More broadly, in-place means that the algorithm does not use extra space for manipulating the input but may require a small though nonconstant extra space for its operation. Usually, this space is O(log n), though sometimes anything in o(n) is allowed. " https://en.wikipedia.org/wiki/In-place_algorithm – Mooing Duck Jun 09 '16 at 23:04
  • @MooingDuck Thanks, I will edit my answer to reflect this. – ghui Jun 09 '16 at 23:13
  • Quicksort takes Log(n) amortized extra memory, O(n) worst case extra memory. I'm not really aware of anyone using "external" memory for algorithms. – Mooing Duck Jun 09 '16 at 23:16
  • By "external" I just meant extra memory. – ghui Jun 09 '16 at 23:19
  • How does Quicksort take O(n) worst case extra memory? We just keep a constant number of indicies of length n, each of which takes O(log n) space, right? – ghui Jun 09 '16 at 23:20
  • 1
    Each "pass" of the quicksort takes (at least) a start pointer, a mid pointer, and an end pointer on the stack, and in the worst case, quick sort has a recursive depth of n. Which means there's at _least_ 3*n pointers on the stack, which takes memory. – Mooing Duck Jun 09 '16 at 23:25
  • Hi @ghui. About the second point, I am asking about "std::inplace_merge". Which takes two sorted containers (one container but two sorted parts) as input and gives us a properly merged array. The question is what happens if we gave unsorted arrays as input ?. – Bharadwaj Gali Jun 10 '16 at 09:28
  • Hi @BharadwajGali. If gave two unsorted arrays as input, then the resulting merged array would also be unsorted, because the merging algorithm that runs in O(n) assumes that the two input arrays are sorted. – ghui Jun 10 '16 at 16:12