If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Swap algorithm for arrays O(N)

Hello everybody!

I would like to ask some help regarding one swap algorithm I am doing. The algorithm need to swap all elements of the array with every possible combination of the elements of it. I am stuck trying to get the best possible time complexity for this algorithm, but I cannot do better than O(N raised to 2). I am running out of ideas. The algorithm I have is the following:

Re: Swap algorithm for arrays O(N)

I will clarify more what I am asking. What I ment about "swap all elements of the array with every possible combination of the elements of it" is, for example, having array [3,-6,2,1]

I want to swap every element with in every possible way, that means:

Swap element in the position 0 with element in the position 1 (3,-6)
Swap element in the position 0 with element in the position 2 (3, 2)
Swap element in the position 0 with element in the position 3 (3, 1)
Swap element in the position 1 with element in the position 2 (-6, 2)
Swap element in the position 1 with element in the position 3 (-6, 1)
And the last swap element in the position 2 with element in the position 3 (2, 1)

For that I need to do two loops in order to check the array (like I am doing in the program). What Im asking if there is any more optimal way in order to achieve a O(N) time complexity (mine is O(N2) because the two loops for.

Re: Swap algorithm for arrays O(N)

You are doing a swap to the right of the active array element. My understanding of the "swap all elements of the array with every possible combination of the elements of it" should give the result (given input array 012):
012
021
102
120
201
210

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.