Your problem is with the following line: values = result; You can not assign every value from one array into another with a simple one-line assignment like this. In order to change the array 'haystack', the sorted values from your `result` array need to be put back into the `values` array. You would probably need to use another `for` loop. As an aside, you could also simplify this code, removing the cumulative counting, getting rid of the `result` array altogether and just assigning the sorted values straight back into `values`.