Hi :) Can you recommend me a good site with C++ excercises? Exercises about data structures like stack and linked list, about inheritance and other useful and logical things :)
What do you mean by saying general algorithm? Something like this code?
And I don't know if you meant just to use iter_swap(Iter, Iter2);
Here is the code:
Oh no.. you can be sure it is my code. I have always done the bubble sort that way (usually for arrays) . I'm new to iterators and that's why I'm a bit confused. Do you think that I will ask for help and then just copy the code from somewhere? :D
But it is not the Bubble sort algorithm Can you give a reference where this algorithm is described? As I said it looks more as some modification of the selection sort algorithm.
I read it in one book but there are many sites that show it this way. For example mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
I usually do it that way like in the first code that I gave you.. not with the swap function. If it is not pure bubble sort that means that I haven't understand it well. Van you give me example or source to read how it should look like? :)
The bubble sort is described in many books on data structures and algorithms. I advice to read "Data Structures & Algorithms in Java" by Robert Lafore. Though sometimes his code examples contain bugs nevertheless the text is written with simple clear language.
The algorithm you showed in fact is some modification of the selection sort. You search the minimum element and substitute the first element of a subsequence for this minimumj element. There is no problem to write such an algorithm with forward iterator. It is more interestiong to realize the bubble sort with using forward iterator.
This time it's not my code (http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Bubble_sort#C.2B.2B) :D. I tested it with list and vector containers and it's working. In fact it was not so hard. Just a normal template(but as I said, I'm new to iterators and I tought that it will be much harder).
You was asking about that right? And is this the right Bubble Sort?
Again I very doubt that this algorithm is the bubble sort algorithm. It more resembles the insertion sort algorithm. I think you should not trust such internet references as you pointed out.
Let consider the algorithm.
It splits the sequence into two parts. The left part is partially sorted and the right part is unsorted. Then the algorithm takes the first element of the unsorted part and looks where it can be inserted in the partially sorted part. If it founds the position where the new element can be inserted it in fact shifts right all other elements after the position by means of using the swap operation.
So during the discussion of the exercise of writing the bubble sort algorithm you showed two other algorithms: 1) selection sort, 2) insertion sort.:)
Till now we did not see the bubble sort algorithm.:)
But in any case your examples of algorithms are interesting.