You're putting the elements into the exact same spot as they were in the original array. What you should be doing is using a separate counter to determine where to put the elements. This won't change...