There are many reasons why an array might need to be sorted, and also many different sizes and types of arrays. The algorithms devised to sort them, generally each work best under different circumstances. For example, when just a few numbers in a narrow range need to be sorted, there is no need for a complex algorithm (such as Quick or Merge) as the difference between one and two milliseconds is not going to make a difference to the user. If thousands of such arrays, or very large arrays need to be sorted, then a complex algorithm will save a significant amout of time. Below are explanations of how the different algorithms work, as well as results from speed tests carried out with them. The algorithms I have chosen to test are ShellSort, BubbleSort, InsertionSort, SelectionSort, and QuickSort.
The first four sorting algorithms are of the order n2 which means that the time taken to sort arrays increases exponentially as the arrays increase in size. They are the simpler type of sorting algorithm and are best used on the smaller arrays. Very large arrays, even using the best of the O(n2) algorithms (ShellSort), will take a very long time. I will procede to explain how each of these work.
BubbleSort is the easiest but most inefficient sort. Items are continually compared with the items next to them and swapped if the smaller item is the latter. This repeats untill no such swaps need to be carried out (the array will be in order). This sort is generally inefficient.
SelectionSort checks through the array over and over and each time finds the smallest number, and swaps it with the next array position to be filled.
InsertionSort searches through the array and copies the lowest value into the next position in a different array.
ShellSort is the best of the O(n2) algorithms. It runs through the array sorting small parts of it with Insertion Sort, then progressively bigger parts and finally the whole array, by which point it is so nearly all sorted that it goes through Insertion Sort very fast. Basically bigger and bigger pieces are sorted but at higher and higher speeds which all averages out to be generally faster than the other O(n2) algorithms.
I devised a test which runs these algorithms using different sets of data. I put through arrays of various lengths containing random integers between 0 and 1000 and then between -10m and 10m. I put through arrays that were already sorted, and arrays that were backwards, as well as arrays that increased and then decreased. The test ran each of the sorting algorithms a number of times for each array (more for the smaller arrays, and quicker sorters) to find the average time it took each algorithm to sort each array. Below are the results for the four O(n2) algorithms .
As is evident from the graphs, the different sorts are better at different things. For the arrays filled with random values (graphs 1 and 2). The efficiencies of the different sorts are very clear, Shell Sort totally dominating. Insertion and selection sorts are pretty much equally easy to implement but insertion is faster, which would make using selection silly in most circumstances.
The already ordered arrays, as expected, were "sorted" fastest by Shell and Insertion (Insertion is used in Shell Sort). Bubble and selection took dismally long to realise, the job had been done.
Selection Sort took quite a lead over Insertion when sorting a reverse-ordered list, but in the "Up then Down" arrays, things were back to normal. The ordered and reverse-ordered arrays were faster to sort than the random ones.
1. 
2. 
3. 
4. 
5. 
An algorithm of a different order (O(n log n)) was also implemented and tested. While this algorithm is far harder to understand and to implement, the speed advantages are phenomenal. Quick Sort is extemely complex but the just of it is that it picks a value, makes two puts all the values higher than the chosen value on one side, and the rest on the other, then repeats for each of the new segments. When any segment is of length 1, it is left unchanged for the remainder of the sorting.
The graphs show what an algorithm this complex can do as far as speed goes. It is on average 100 times faster than Bubble sort, and as array sizes increase so does its relative advantage.
The results are all what one would expect except perhaps the sorted (forwards and backwards) arrays, where shell sort seems to have the advantage over quick. Interestingly enough, quicksort, seems to not do too well with these sort of arrays. This proves my point earlier on that each of the sorts have their uses - there is no one sort that "has it all".
6. 
7. 
8. 
9. 
10. 
So if you need an array sorted and need to use a sorting algorithm, ask first, "What size is the array?" If its small, go for one of the O(n2) sorts; Shell sort if you can, Bubble sort if you need quick implementation and time is no issue. If it is large use Quick Sort (Merge Sort and Heap Sort are two other O(n log n) sorting algorithms). "What is the nature of the array?" If its already mostly sorted, avoid quicksort, use Shell sort instead. The speed of these algorithms are all shown above, as is the proof of thier optimal usage environments. In big projects, the selection of an algorithm can be crucial to success, which is why tests like these will continue to be executed.