Classic array overshoot problem. Look at your sort code inner for loop.
for(i=0; i<n; i++) if (values[i] > values[i+1])...
The values array has n elements, numbered from 0 to n-1. Your if statement tries to test values[i+1], but i+1 = n. You're trying to test values[n], which is beyond the end of the array. Instant error. I bet you're getting a seg fault.
Adjust your limits and it should fix this problem. No guarantees that there aren't any other issues though. ;-)
If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)