U
U
uvelichitel2015-02-06 19:52:07
Programming
uvelichitel, 2015-02-06 19:52:07

Is there an algorithm for list reorganization that is optimal in terms of the number of permutations?

Is: an array or a list in general a collection - a, b, c, d, e. You need to get the given b, d, a, e, c. There is no buffer, you need to do inplace. New(), Set(i) and Get(i) are not, and they are not needed because there is no buffer anyway. There is only Swap(i, j). There is a stack to account for permutations. That is, they do not have to be applied immediately, but it is permissible to accumulate and reduce (if possible) before use.
That is, it is necessary: ​​to reorganize the array into a given possibly minimal (well, or at least countable) number of pairwise permutations.
For example from "a, b, c" to "b, c, a" this is "1-2" we get "b, a, c" then "2-3" we get "b, c, a" Required permutations Swap (1, 2), Swap(2, 3).
The array is named a, b, c, d to simplify the problem statement, let 1, 2, 3, 4, 5 in 2, 3,
Does such an algorithm exist that is better than exhaustive search with proven correctness?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mrrl, 2015-02-06
@uvelichitel

If there is an IndexOf operation for finding an element, then the optimal number of permutations is achieved as follows (turning L1 into L2):

for(int i=0;i < L1.Count;i++){
  while(L1[i]!=L2[i]){
     L1.swap(i,L2.IndexOf(L1[i]));
  }
}

During each exchange, the current element L1[i] is placed in the place where it would have been in the list L2, and does not leave this place anymore.

A
Armenian Radio, 2015-02-06
@gbg

And what did the usual qsort not please?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question