I know there are plenty of threads on the topic, but I wished to start a thread exploring the logic of sorting. Below is a simple sorting algorithm that goes through an array in passes, moving larger values to the right, and smaller values to the left. The primary question I have is how can I increase the efficiency of my sorting without introducing more complexity. Currently, the array will be sorted in anywhere from 3-8 passes.
The one optimization I know about is already in the program provided by Stewbond. Look at the for-loop in the bubblesort method. Notice that at each iteration, the number of elements to check decreases by one? This is because the next largest/smallest element (depending on the order you want to sort the array), "bubbles" to the back of the array after each iteration of the for-loop. So there is no need to check that last element after each iteration.
This reduction in complexity is almost non significant with this optimization, but it is nonetheless slightly faster than any bubblesort program that does not make use of this
To test this, use the following numbers and run one loop of bubblesort on it then look at the last value:
Some sorting algorithms are more efficient than others when randomly sorted, inverted, nearly sorted, or with only a few values. Check this site out. It explains things pretty well: http://www.sorting-algorithms.com/