More important than being able to pick between various sorting routines, is learning *WHY* the various routines work, and the principle behind them.
Bubble sorts are often the easiest to understand, and are the sort that most programmers quickly figure out for themselves. The thinking process behind it is really very simple -- run a loop with the array from the starting element to the finish element and check for the lowest possible value in that array. Once found, make certain to move it to the start of the array, and then continue on until you run out of elements.
1, 3, 5, 2, 4 <-- let's say this is the array. To bubble sort, we'd start at the 1 and read each element until the end, and we'd find the lowest value. (1). We'd then move it to the start of the array.
(1), 3, 5, 2, 4 <-- then we repeat this process. Start at the 2nd element (3) and go all the way to the end of the array and look for the lowest value. (2). Move that to the 2nd element position.
(1, 2) , 5, 3, 4 <-- repeat the process as above until we're finished.
(1, 2, 3), 5, 4
(1, 2, 3, 4), 5
(1, 2, 3, 4, 5)
And that's basically a bubble sort in action. As you can see (or visualize perhaps), it's not very efficient. Each time you get the lowest value, you have to search the whole dataset from start to finish, resulting in a ton of comparisons and swaps going on with your data. It works, but it doesn't work very efficiently.
**********
The next type of sort which most people will learn to implement is a QuickSort routine. They tend to jump to it because it's just sooo much faster than a bubble sort, and once they realize a bubble sort has very limited uses, they want to jump to something quick and efficient which they can use anytime they want.
Let's take the same dataset of (1, 3, 5, 2, 4) and run it through a quicksort.
First, find a *pivot point* -- that's going to be the dividing point where you split the sort. 1 is the lowest value. 5 is the highest. 1 + 5 = 6... Half of 6 is 3, so we'll make 3 the initial pivot point.
Anything 3 or lower gets put into one array. Anything higher than 3 goes into a different array.
(1, 3, 2) --- (5, 4)
Now, repeat that process with each of those half arrays. ! + 3 = 4... 2 is the pivot point on the left, 5 + 4 = 9.... 4.5 is the pivot point on the right.
(1, 2) --- (3) ---- (4) --- (5)
Repeat that process with any array which still has over 2 elements in it.. 1 + 2 = 3... 1.5 is the pivot point.
(1) ---- (2) --- (3) ---- (4) ---- (5)
Now just assemble those pieces back to make the final sorted array.
(1, 2, 3, 4, 5)
That's the process behind a Quicksort, in a nutshell.
********************************************
If anyone has any questions about any of the other sorting methods, feel free to ask them and I'll answer if I know them. There's a zillion sorting methods out there, and I've learned maybe a dozen or so of the most commonly used ones. For me, I think the importance should be more on understanding the CONCEPT behind the routines, rather than to just know the routine themselves. Anyone can copy/paste code into a program. It's only when we understand WHAT we've copy/pasted that we can actually say we've learnt and grown as programmers.
Bubble sorts are often the easiest to understand, and are the sort that most programmers quickly figure out for themselves. The thinking process behind it is really very simple -- run a loop with the array from the starting element to the finish element and check for the lowest possible value in that array. Once found, make certain to move it to the start of the array, and then continue on until you run out of elements.
1, 3, 5, 2, 4 <-- let's say this is the array. To bubble sort, we'd start at the 1 and read each element until the end, and we'd find the lowest value. (1). We'd then move it to the start of the array.
(1), 3, 5, 2, 4 <-- then we repeat this process. Start at the 2nd element (3) and go all the way to the end of the array and look for the lowest value. (2). Move that to the 2nd element position.
(1, 2) , 5, 3, 4 <-- repeat the process as above until we're finished.
(1, 2, 3), 5, 4
(1, 2, 3, 4), 5
(1, 2, 3, 4, 5)
And that's basically a bubble sort in action. As you can see (or visualize perhaps), it's not very efficient. Each time you get the lowest value, you have to search the whole dataset from start to finish, resulting in a ton of comparisons and swaps going on with your data. It works, but it doesn't work very efficiently.
**********
The next type of sort which most people will learn to implement is a QuickSort routine. They tend to jump to it because it's just sooo much faster than a bubble sort, and once they realize a bubble sort has very limited uses, they want to jump to something quick and efficient which they can use anytime they want.
Let's take the same dataset of (1, 3, 5, 2, 4) and run it through a quicksort.
First, find a *pivot point* -- that's going to be the dividing point where you split the sort. 1 is the lowest value. 5 is the highest. 1 + 5 = 6... Half of 6 is 3, so we'll make 3 the initial pivot point.
Anything 3 or lower gets put into one array. Anything higher than 3 goes into a different array.
(1, 3, 2) --- (5, 4)
Now, repeat that process with each of those half arrays. ! + 3 = 4... 2 is the pivot point on the left, 5 + 4 = 9.... 4.5 is the pivot point on the right.
(1, 2) --- (3) ---- (4) --- (5)
Repeat that process with any array which still has over 2 elements in it.. 1 + 2 = 3... 1.5 is the pivot point.
(1) ---- (2) --- (3) ---- (4) ---- (5)
Now just assemble those pieces back to make the final sorted array.
(1, 2, 3, 4, 5)
That's the process behind a Quicksort, in a nutshell.
********************************************
If anyone has any questions about any of the other sorting methods, feel free to ask them and I'll answer if I know them. There's a zillion sorting methods out there, and I've learned maybe a dozen or so of the most commonly used ones. For me, I think the importance should be more on understanding the CONCEPT behind the routines, rather than to just know the routine themselves. Anyone can copy/paste code into a program. It's only when we understand WHAT we've copy/pasted that we can actually say we've learnt and grown as programmers.