Questions tagged [in-place]

When something happens with only a constant amount of extra space, it is said to happen in-place.

16 questions
66
votes
3 answers

In-place algorithm for interleaving an array

You are given an array of $2n$ elements $$a_1, a_2, \dots, a_n, b_1, b_2, \dots b_n$$ The task is to interleave the array, using an in-place algorithm such that the resulting array looks like $$b_1, a_1, b_2, a_2, \dots , b_n, a_n$$ If the in-place…
Aryabhata
  • 6,291
  • 2
  • 36
  • 47
43
votes
4 answers

Worst case $O(n \ln n)$ in place stable sort?

I am having trouble finding good resources that give a worst case $O(n \ln n)$ in place stable sorting algorithm. Does anyone know of any good resources? Just a reminder, in place means it uses the array passed in and the sorting algorithm is only…
user834
  • 849
  • 1
  • 8
  • 11
23
votes
3 answers

What is the most efficient constant-space sorting algorithm?

I'm looking for a sorting algorithm for int arrays that doesn't allocate any byte other than the size of the array, and is limited to two instructions: SWAP: swap the next index with the current one; MOVE: moves the cursor to +1 or -1 index; That…
MaiaVictor
  • 4,199
  • 2
  • 18
  • 34
5
votes
0 answers

Sorting in place & stable in linear time

Given an array with only 0 & 1. Can we have an algorithm which has all the following desirable characteristics- The algorithm runs in $O(n)$ time. The algorithm is stable. The algorithm sorts in place. I know the algorithms if only 2…
Mr. Sigma.
  • 1,301
  • 1
  • 16
  • 38
5
votes
1 answer

Merge two sorted arrays without using additional memory

We have two sorted arrays of integers. Without using additional memory we need to merge these two arrays such that the smallest numbers are in the 1st array and the remaining numbers are in the second array. For instance, if your two arrays are…
Guy Kahlon
  • 163
  • 1
  • 5
4
votes
1 answer

Choosing an element from a set satisfying a predicate uniformly at random in $O(1)$ space

We are given a set of objects, say integers, $S$. In addition, we are given a predicate $P$, for example $P(i): \Leftrightarrow i \geq 0$. We don't know in advance how many elements of $S$ satisfy the predicate $P$, but we would like to sample or…
Juho
  • 22,905
  • 7
  • 63
  • 117
3
votes
2 answers

Shuffling a file on disk using $O(\log n)$ memory

How do you shuffle the bytes in a file (bytes for simplicity) on disk with a small, $O(\log n)$, amount of memory and preferably in-place? If the file had size $2^m$, then we can first split the file into 2-byte chunks, shuffle them individually,…
2
votes
0 answers

In-place patching of diff between two sets?

For two ordered sets of keys Paul Heckel's diffing algorithm generates the necessary actions to transform, or patch, one set to the other. I am asking how you'd do the patching without keeping a copy of the "old set" around, nor incurring a large…
Axel F
  • 21
  • 2
2
votes
1 answer

Index variable takes up log(n) space?

In the definition of in-place algorithm, from Wiki, " However, this form is very limited as simply having an index to a length n array requires O(log n) bits." How does an array index requires log n bits? For example, A[0], 0 here just take O(1)…
wangge
  • 123
  • 3
1
vote
2 answers

Is there a lower bound on space complexity of non-in-place sorting?

There is a well known result stating that any comparison-based sorting algorithm has a time complexity of at least O(n log n). Selection sort and merge sort are two common examples, with time complexity O(n^2) and O(n log n) respectively. Merge sort…
A. Darwin
  • 123
  • 3
1
vote
1 answer

In-place matrix transposition using rotations

Some time ago I invented an algorithm for in-place matrix transposition using rotations. A matrix of size N×M is represented as a one-dimensional array of size N*M, which is also the future storage for the corresponding transposed M×N matrix. The…
user107819
0
votes
0 answers

Interleaving first and second half of an array of even length in place

If A is an array with the following elements: $$ a_1,a_2,...,a_n,b_1,b_2,...,b_n $$ How to shuffle A to form: $$ a_1,b_1,a_2,b_2,...,a_n,b_n $$ with minimal swaps and using no additional space? I read this answer and wrote the following code: def…
Erric
  • 143
  • 6
0
votes
1 answer

Inplace sorting of variable length records

Is there an in-place algorithm for sorting variable length records? For example a vector of arbitrary length strings. Most provided in-place algorithms assume that you can switch two elements without affecting the rest of the structure. However…
Taemyr
  • 605
  • 3
  • 9
0
votes
0 answers

In-place linear sort of integers, again

I am amazed by the many discussion regarding the existence of any linear and in-place sorting algorithm, and variants, see…
0
votes
1 answer

LinkedList: Remove the kth node form the end

Question: Write a function that takes in the head of a Singly Linked List and an integer k and removes the kth node from the end of the list. The removal should be done in place, meaning that the original data structure should be mutated (no new…
1
2