10-20-2013 01:51 PM - edited 10-20-2013 05:42 PM
@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) 😄
10-20-2013 04:07 PM - edited 10-20-2013 04:10 PM
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.
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...
10-20-2013 05:41 PM - edited 10-20-2013 05:43 PM
@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.
04-18-2014 11:16 AM
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.
04-18-2014 11:44 AM
04-19-2014 04:34 AM
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.
04-19-2014 08:24 AM
04-19-2014 09:08 AM
thank you no i havent taken any. i try to learn by means of one book which is proposed my sssistant professor.
04-19-2014 09:14 AM
04-20-2014 02:49 AM
yeah i found it thank you again for your help