LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to sort an array in Even and odd parts


@T_reX wrote:

Now I've tried this logic. This logic is sorting the array but not creating two different arrays...


You need to remember (or know) that "replace array subset" does NOT change the size of the array, so if you replace an element that is outside the size of the array, nothing happens. The shift register needs to be initialized with an array of the correct size. Let's go back to the original problem where you have an array input (instead of generating random data inside the loop).

 

Since you don't know how many even or odd elements are in the final outputs, you need to prepare for the worst possible case. You would need to initialze both the odd and even array with the same size as the input array and trim at the end (This is still much more memory efficient than constantly resizing by appending elements inside the loop).

 

If the order of the elements in the output does not matter, here is a more memory efficient solution because you only need one array in the shift register and you know the needed size exactly, because it is the same size as the input. You can replace the even elements starting from the beginning and the odd elements starting from the end and once the loop has finished you can split it in the odd and even subarrays.

 

See if you understand it. (Since all elements get overwritten, you don't even need to initialize freshly, but you can initialize with the input array for simplicity)  😄

 

 

Download All
0 Kudos
Message 31 of 40
(1,349 Views)

Even more memory-efficient is to work on the array in-place without creating a copy, stepping from front and back simultaneously and swapping values that are in the "wrong" place.

 

SplitEvenOdd.png

 

Oops - just seen that altenbach suggested this very method halfway down this discussion - I'd skipped to the end and not noticed 🙂  Oh well, it's still a good way to do it...

Message 32 of 40
(1,335 Views)

@GregSands wrote:

Oops - just seen that altenbach suggested this very method halfway down this discussion - I'd skipped to the end and not noticed 🙂  Oh well, it's still a good way to do it...


Yup, that's what I basically had in mind. Thanks for implementing! 😉

 

I wonder if the two while loops could be modified to operate in parallel for a small performance gain. Would need to carefully check for buffer allocations though. You currently also have a small data dependency due to the limit check that would need to be worked around in some other way.

0 Kudos
Message 33 of 40
(1,325 Views)

hii

ı have problem can anyone help me ? ı want to create random array then ı want to divide this array in two part which are odd part and even part. i tried to examples at this forum but ı havent reached any solution. 

0 Kudos
Message 34 of 40
(1,178 Views)
Then attach what you have tried and we tell you what you are doing wrong.
0 Kudos
Message 35 of 40
(1,165 Views)

thank you so much finally i done it. but i have problem . when i run it for a first time it run properly. but when i run it for a second or third time it dont create new even and odd array. it add new value at my previous odd and even array. 

ı attached photos of it. additionaly ı cant find one tool in example.

Download All
0 Kudos
Message 36 of 40
(1,147 Views)
It's an array constant. Go to the array palette and then drag a numeric constant into it. Without it, the shift register is not initialized when you run the VI.

Have you taken any of the free tutorials?
0 Kudos
Message 37 of 40
(1,129 Views)

thank you no i havent taken any. i try to learn by means of one book which is proposed my sssistant professor.

0 Kudos
Message 38 of 40
(1,117 Views)
So, did you find the array constant container?
0 Kudos
Message 39 of 40
(1,111 Views)

yeah i found it thank you again for your help

0 Kudos
Message 40 of 40
(1,090 Views)