Re: random access iterator
The sorting algorithm isn't really relevant here. FYI, this is taught
in any decent university curriculum... E.g. bubble sort has two
flavors - n*(n-1)/2 passes and an optimization with a flag to
check if anything happened during the last pass. The code I passed
is actually worse since it doesn't take advantage of the fact one element
gets positioned on every pass. It was for illustration of the use of
a random access iterator - not meant to teach bubble sort to readers...
If you are interested in sorting algorithms, read Donald Knuth's
algorithm book "The Art of Computer Programming":
Volume 3: Sorting and Searching (2nd Edition), 1998.
Addison-Wesley Professional, ISBN 0-201-89685-0
(info courtesy of wikipedia.org)
Again, thuis is off topic for the VC group.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"George" <George@discussions.microsoft.com> wrote in message
news:C1C79E14-9DC8-4289-A536-0BAEE56A1B0A@microsoft.com...
Thanks Alexander,
Your algorithm is quite smart than traditional bubble sort implementation,
which is different form traditional implementation, like this,
http://java.poac.ac.cn/codeopen/jiaocheng/java2s/Code/Java/Collections-Data-Structure/Bubblesort.htm
Your algorithm has a status to mark whether in last round there is any
changes, if no change, just quit the algorithm. For the traditional
implementation, it also has advantage which we do not need to iterate from
beginning every time as you did in your solution.
So, my question is, compared with the traditional implementation, in what
situations your algorithm is better/worse? Why?
regards,
George
"Alexander Nickolov" wrote:
Well, it can be useful for example when writing bubble-sort
by hand (not that I'd actually advocate doing that...):
bool bSwap = false;
while (did_swap) {
bSwap = false;
for (range_type::iterator it = range.begin(); it != range.end() &&
it+1
!= range.end(); ++it) {
if (*it > it[1]) {
std::swap(*it, it[1]);
bSwap = true;
}
}
}
The important point is about accessing elements relative to the current
position of the iterator, not the bubble-sort algorithm of course... :)
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"Abhishek Padmanabh" <abhishek_padmanabh@hotmail.com> wrote in message
news:E1FD48A4-1547-43C4-8495-1EBADA6D3C79@microsoft.com...
"George" <George@discussions.microsoft.com> wrote in message
news:83684567-C1D3-4D9C-BB7F-2AB905DBC802@microsoft.com...
For random access iterator, operator[] is supported. Mentioned in
Bjarne's
book, Chapter 19 (Iterators and Allocators).
I have not used operator[] on random access iterator before and I have
not
found a good and simple sample either. :-)
That is because it is not very intuitive to use as compared to using
the
operator[] overloaded for the container itself. Also, as far as VC++
2005
is concerned, it causes two dereferences. You can use the iterator's
operator[] as below:
#include<vector>
using std::vector;
int main()
{
vector<int> vec(10);
vector<int>::iterator it = vec.begin();
int fifth_element = it[4]; //equivalent to advance it by 4 and
dererefence the resulting iterator
int fifth_element = vec[4]; //preferred and more intuitive
}