I have figured out a problem that I have. I've been developing software for number of years but most of the software I did was just Input/output. No algorithms are involved.

I know for example how binarysearch works, like it divides the array in two, then search left or right and that optimizes the searching.

When I try to write the code off my head, I can't. Even for insertion sorts, things like a[j+1] = a[j], in general playing with arrays or these things, I can't visualize or write them off my head. I don't know why, I didn't CS, but I'm EE :/.

How can I be better in writing algorithms :/ or at least understand the code in my head and visualize it

I tried to figure out the meaning of that function, I couldn't, even tried on paper, but didn't get it. Would someone explain that to me ? If that's possible with Ascii text or some images would be so much appreciated.