[Archive!] Pure mathematics, physics, chemistry, etc.: brain-training problems not related to trade in any way - page 584

 
alsu:

In one pass:

Create an empty copy of an array of the same size, initialize it with twos.

Go from the beginning of the array. Encounter 1 - write to the copy from the beginning, encounter 3 - write to the copy from the end.


And initialising by twos (66% useless on average) is not a pass?
Plus you have to monitor three pointers at the same time!
 
GaryKa:

I'll give you a little task.

Of course, it's a hack, but at job interviews it works as an apotheosis of array sorting knowledge ))


So, the task for sorting

There is an array of N cells, in which the ones, twos and threes are placed in random order.

Build the best sorting algorithm.


Count on one pass, fill in the array on the second pass.
 
alsu:

In one pass:

Create an empty copy of an array of the same size, initialize it with twos.

Go from the beginning of the array. If 1 is encountered, write to the copy from the beginning; if 3 is encountered, write to the copy from the end.

Without copy (or you will say that filling the copy is a separate pass).

n=0, m=0

Start at the beginning of the array. If we meet 1 - change it with n-th element from the beginning, not equal to 1 (by construction it will always be 2), n++; if we meet 3 - change it with m-th element from the end, not equal to 3, m++, and if we meet 1, we follow steps of the first part.

 
alsu:

Without copy (or you will say that filling the copy is a separate pass)

n=0, m=0

We go from the beginning of the array. If we meet 1, we change it with n-th element from the beginning not equal to 1 (by construction it will always be 2), n++; if we meet 3, we change it with m-th element from the end not equal to 3, m++, and if we meet 1, we proceed as described in the first part.


an average of half a pass, plus the same amount of time for exchanges
 
alsu:

Without copy (or you will say that filling the copy is a separate pass)

n=0, m=0

We go from the beginning of the array. If we meet 1, we change it with n-th element from the beginning not equal to 1 (by construction it will always be 2), n++; if we meet 3, we change it with m-th element from the end not equal to 3, m++, and if we meet 1, we proceed as described in the first part.



That's a great way to go.
 
alsu:
Reducing the number of passes does not always give an increase in speed.
 
Exchanging two elements of an array is 3 operations. It is unlikely to be faster.
 
TheXpert:
Reducing the number of passes does not always give an increase in speed.
Well, the optimality criterion is not set...
 
Let me raise a question:
A+B=...
 
alsu:

Without copy (or you will say that filling the copy is a separate pass)

n=0, m=0

We go from the beginning of the array. If we meet 1, we change it with n-th element from the beginning not equal to 1 (by construction it will always be 2), n++; if we meet 3, we change it with m-th element from the end not equal to 3, m++, and if we meet 1, we proceed as described in the first part.



Good idea.
Reason: