>However, for small data sets it doesnt matter much what sorting algorithm u use.
That's true, so let's look at it from a clarity standpoint. But first, two reasonable implementations:
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
#define length(x) (sizeof x / sizeof *x)
void bubble_sort(int array[], int size);
void insert_sort(int array[], int size);
int main()
{
int a[] = {5,8,3,8,1,9,0,2,4};
int b[] = {5,8,3,8,1,9,0,2,4};
copy(a, a + length(a), ostream_iterator<int>(cout, " "));
cout<<endl;
copy(b, b + length(b), ostream_iterator<int>(cout, " "));
cout<<endl;
bubble_sort(a, length(a));
insert_sort(b, length(b));
copy(a, a + length(a), ostream_iterator<int>(cout, " "));
cout<<endl;
copy(b, b + length(b), ostream_iterator<int>(cout, " "));
cout<<endl;
}
void bubble_sort(int array[], int size)
{
for (int i = 0; i < size; i++) {
bool swapped = false;
for (int j = size - 1; j > i; j--) {
if (array[j - 1] > array[j]) {
swap(array[j - 1], array[j]);
swapped = true;
}
}
if (!swapped)
break;
}
}
void insert_sort(int array[], int size)
{
int j, save;
for (int i = 1; i < size; i++) {
save = array[i];
for (j = i; j >= 1 && save < array[j - 1]; j--)
array[j] = array[j - 1];
array[j] = save;
}
}
Now we can look at your arguments from an objective viewpoint (yes, I can do that):
for small data sets it doesnt matter much what sorting algorithm u use
Small data sets have a tendency toward getting much bigger. This is a naive excuse, and I won't spend time on it because it doesn't deserve time.
Apart from being the simplest and the easiest of the sorting algorithms
I fail to see how the algorithm for bubble sort is simpler or easier than insertion sort. They're both simple, they're both easy. However, you'll find that the concept behind insertion sort is easier to grasp than that of bubble sort (from my experience as a teacher). The implementation is also easier to get right (I've written them and helped others write them enough to be able to say this with confidence).
it knows when the data set is already sorted
Resulting in a linear pass over the data set, yes. However both implementations I gave do that. This isn't an argument in favor of bubble sort, it's an argument that manages to keep bubble sort in the game. Of course, insertion sort handles this feature automagically while bubble sort requires a modification (the addition of a "when to stop" flag) to the basic algorithm.
making it ideal for sorted or nearly sorted data items
But no better than insertion sort, and it takes more work to get that "ideal" algorithm.
>From galmca's code it is quite apparent that he is trying to learn bubble sort
galmca has a lot to learn before trying any sorting algorithms. But why start learning with the worst possible thing? Why not learn the right way, then if you're interested in poor algorithms as a curiosity, take a look?
>and u replied "don't use bubble sort, ever"!!!!!!!!
No, I replied: "don't use bubble sort, ever." I have a strong distaste of using multiple punctuation characters without good cause. And you still haven't given a good reason for using bubble sort. There's no existing situation (outside of theoretical computer science) where bubble sort is better than any other sorting algorithm.
Bubble sort should have died long ago, but people like you keep saying "Aww, it's not really that bad if the data set is miniscule and won't ever grow and the items are tiny and you don't compare it with any other sorting algorithm or take your head out of the sand and look around". But of course, you're not going to listen to me because hey, what do I know?