-2

Given a array of elements like [a1,a2,...an, b1,b2,...bn, c1,c2,...cn] how to merge like [a1, b1, c1, a2, b2, c2, ... an, bn, cn] without using extra memory.

Adi agarwal
  • 188
  • 1
  • 1
  • 9
  • I'm new to stack overflow, but two negative votes on my question?? What was that for?? – Adi agarwal Feb 07 '15 at 17:25
  • Without using extra memory, I don't think it can be done in general. If you have one extra temporary location, it is doable. Or, you might be able to find a way to do it if we knew the data was a numeric type. – Degustaf Feb 07 '15 at 17:36
  • It is hard, but not impossible (google: "in-place mergsort"). But yes: you need *at least* one memory location just for swapping two others. – wildplasser Feb 07 '15 at 17:37
  • SO is about specific programming issues. – alk Feb 07 '15 at 17:38
  • Wellcome to SO. What did you try? What difficulties did you encounter? What is your question? – Jens Gustedt Feb 07 '15 at 17:40
  • @JensGustedt read the description for the question. I have tried to this using extra memory, but nothing is coming in my mind doing it inorder – Adi agarwal Feb 07 '15 at 17:43
  • I have found the solution here but unable to understand the solution- https://effprog.wordpress.com/2010/08/02/in-a-given-array-of-elements-like-a1-a2-a3-a4-an-b1-b2-b3-b4-bn-c1-c2-c3-c4-cn-without-taking-a-extra-memory-how-to-merge-like-a1-b1-c1-a2-b2-c2-a3-b3-c/ – Adi agarwal Feb 07 '15 at 17:46
  • @AdityaGoel The algorithm you linked too is awful in terms of extra memory usage. it stores 3 integers for each function call, and its recursive so you will have 3 times the size of your array on the stack at the deepest recursion. – Degustaf Feb 07 '15 at 17:53
  • I have edited the title to constant space to avoid confusion – Adi agarwal Feb 07 '15 at 17:53
  • There are more duplicate questions such as [this](http://stackoverflow.com/questions/18043778/array-movement-from-a1-an-b1-bn-to-a1-b1-an-bn), [this](http://stackoverflow.com/questions/9227747/in-place-transposition-of-a-matrix), and [this](http://stackoverflow.com/questions/23527241/interleave-three-equally-sized-partitions-in-an-array-inplace-in-on-time). – Blastfurnace Feb 07 '15 at 17:59
  • The title of this thread should be changed, since it's not a merge sort, but as mentioned in the links, swapping array elements from column order to row order, or better yet, it should be called an "in place matrix transpose". – rcgldr Feb 07 '15 at 21:18

1 Answers1

0

Assuming "element" is a signed integer less than half MAXINT, the following lets you swap two integers x and y without using an additional memory cell (source: Prof. Dekker, UvA):

x= x + y;    // x is now (x+y)
y= x - y;    // y is now (x+y) - y = old x
x= x - y;    // x is now (x+y) - ((x+y)-y) =old y
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    It is not just about one swap. – Adi agarwal Feb 07 '15 at 17:51
  • @AdityaGoel it could be applied to all pairs that needs swapping (subject to stated limitation). – Weather Vane Feb 07 '15 at 17:52
  • Can you please elaborate your solution how swapping would be of help here to convert [a1,a2,...an, b1,b2,...bn, c1,c2,...cn] to [a1, b1, c1, a2, b2, c2, ... an, bn, cn] – Adi agarwal Feb 07 '15 at 17:58
  • or assume elements are two's complement unsigned integer and it always works. So does | x = x ^ y; | | y = x ^ y ; | | x = x ^ y; | . – rcgldr Feb 07 '15 at 21:17
  • 1
    @WeatherVane It's actually very complicated to do it in-place, using just swaps: http://en.wikipedia.org/wiki/In-place_matrix_transposition – Niklas B. Feb 08 '15 at 14:56