Back in high school, we had to sort things sometimes. Everyone used Bubble sort, because we didn't know any other way to go about sorting. I remember thinking about sorting one day, and could not for the life of me come up with some other way to sort. And then came the day we talked about sorting in this class, and my mind just went "oh."
Selection sort is the closest thing to bubble sort, and kind of works on the same principle. In Bubble sort, you compare each pair of values next to each other, and swap if necessary. In Selection sort, you find the minimum (or maximum, or whatever else you're looking for) of the whole array and swap it with the first value, then repeat for the whole list starting from the next index. Had I ever thought of a different way to sort, it would have most likely been Selection sort, due to the iterative nature of the sort. I'm pretty sure that I didn't know recursion at that point, which is why I would never have come up with these next sorts:
Quick sort and Merge sort. These completely revolutionized how I thought about sorting. Not just because they're recursive, but because they're actually very simple concepts. Quick sort: separate the array according to each element being less than or greater than the first element, then sort each new array, and put all the pieces together at the end. Merge sort: cut the array in half, sort each half, then put the halves together by picking the smallest element between the two, then the next smallest (or largest, then the next largest, etc), and so on. These are perfect examples of how to properly use recursion: break a problem down into smaller copies of the same problem, until each portion can be easily managed.
So we have all these sorts, which do we pick? We probably want the fastest one. How do we know which is the fastest? We have to test them. However, we can't just do a single test, we need to know how the performance of each algorithm scales with the size of the input to properly determine their efficiencies. This is because the runtimes of different algorithms may not stay proportional to each other. After testing in class, and in one of the labs, we determined that Quick and Merge greatly outperform Selection, and Selection greatly outperforms Bubble, at large array sizes. However, at smaller sizes, there was no clear victor in terms of speed. As well, there may be situations where you need to sort an almost-sorted array. In that case, sorts like Selection and Bubble see a large increase in speed, where Quick and Merge don't.
The moral of the story: built-in sort outperforms all of these =\
Selection sort is the closest thing to bubble sort, and kind of works on the same principle. In Bubble sort, you compare each pair of values next to each other, and swap if necessary. In Selection sort, you find the minimum (or maximum, or whatever else you're looking for) of the whole array and swap it with the first value, then repeat for the whole list starting from the next index. Had I ever thought of a different way to sort, it would have most likely been Selection sort, due to the iterative nature of the sort. I'm pretty sure that I didn't know recursion at that point, which is why I would never have come up with these next sorts:
Quick sort and Merge sort. These completely revolutionized how I thought about sorting. Not just because they're recursive, but because they're actually very simple concepts. Quick sort: separate the array according to each element being less than or greater than the first element, then sort each new array, and put all the pieces together at the end. Merge sort: cut the array in half, sort each half, then put the halves together by picking the smallest element between the two, then the next smallest (or largest, then the next largest, etc), and so on. These are perfect examples of how to properly use recursion: break a problem down into smaller copies of the same problem, until each portion can be easily managed.
So we have all these sorts, which do we pick? We probably want the fastest one. How do we know which is the fastest? We have to test them. However, we can't just do a single test, we need to know how the performance of each algorithm scales with the size of the input to properly determine their efficiencies. This is because the runtimes of different algorithms may not stay proportional to each other. After testing in class, and in one of the labs, we determined that Quick and Merge greatly outperform Selection, and Selection greatly outperforms Bubble, at large array sizes. However, at smaller sizes, there was no clear victor in terms of speed. As well, there may be situations where you need to sort an almost-sorted array. In that case, sorts like Selection and Bubble see a large increase in speed, where Quick and Merge don't.
The moral of the story: built-in sort outperforms all of these =\