Bubble Sorting algorithm

Hi Hi Hi There !

After I have studied iterators long enough, I decided to try and see how a bubble sort would work if done completely with them, however my code is ignoring the first position of the vector I'm trying to sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>

using namespace std;

int main() {
	int arr[11] = {300, 4, 7, 90, 111, 13, 14, 60, 88, 54, 10};
	vector<int> ivec(arr, arr +(sizeof(arr)/sizeof(int)) );
	
	for(vector<int>::iterator i = ivec.begin(); i != ivec.end(); i++) {
		for(vector<int>::iterator j = ++ivec.begin(); j != ivec.end();    j++) {
			if(*i < *j) {
				int tmp = *i;
				*i = *j;
				*j = tmp;
			}
		}	
	}
	
	for(vector<int>::iterator i = ivec.begin(); i != ivec.end(); ++i) {
		cout << *i << "\t";
	}
	cout << endl;
}


As you'll probably notice by running this code, the 300 remains at its original position. Could somebody please tell me what I'm doing wrong ?

Best Regards,

Jose.
The solution is amazingly simple. In line 11 remove the the "++" in front of ivec.begin().
Hello Brown3141,

Thank you for your reply. It seems it was easy for you to see the problem in my code, however, it wasn't as evident to me, I placed that ++ purposefully because I remember that's how I learned to do the algorithm, and actually I don't understand how can both iterators point to the same first position during the initial iteration for for both loops. I believed that wouldn't work.
Can you provide some insights ?

Thank you,

Jose
Topic archived. No new replies allowed.