Your algorithm does not look like a viable array sorting algorithm. Perhaps there are some data sets that it would be able to yield a correct result on, but I do not think it would yield correct results for arbitrary data sets. Maybe it's alright ... without working through it all I can say is that it's an unusual solution.
Following is the pseudo-code for a conceptually very simple sorting algorithm called the "Delayed Replacement Sort" (or, perhaps more commonly, the "Selection Sort"):
Begin DELAYEDSORT
For ITEM=1 to maximum number of items in list-1
LOWEST=ITEM
For N=ITEM+1 to maximum number of items in list
Is entry at position N lower than entry at position LOWEST?
If so, LOWEST=N
Next N
Is ITEM different from LOWEST
If so, swap entry at LOWEST with entry in ITEM
Next ITEM
End DELAYEDSORT
You should be able to easily adapt this pseudo-code into a Java method. Note that this algorithm has two loops:
- the outer loop is finished when the whole array has been sorted
- the inner loop finds the "lowest" element in the portion of the array that is still unsorted.
After the inner loop, a check is made to see if a swap needs to be made. It is this step that gives the algorithm its name; swapping is only done if it needs to be done to order the element indicated by the index of the outer loop.
Good luck.
i-=1
would not work. – BevynQ Oct 17 '13 at 23:35