Koen, look at your code. You have an outer loop on I and an inner loop on J, both of which run from 1 to the size of your input. Then you have a compare and 3 assignments. These don't matter, but the two nested loops give your code a time complexity of O(n^2), which means that for any input, your algorithm will take the quadratic of the input to complete. This is very bad, save for the smallest of inputs.
You might want to consider implementing Quicksort, which has an average case of O(n log(n)), and a worst case of O(n^2), but this can be made exponentially unlikely with a well chosen pivoting strategy.
You should consult a good book on algorithms and data structures. If your inputs will never be too big, you might also consider Shellsort or Mergesort.
I hope this helps.
EDIT: Recursive algorithms are not very efficient for small datasets due to the cost of recursion, so you might want to switch to a nonrecursive strategy to sort the last 20 - 30 elements in your set.
|