-
My Bubble Sort, help
This sort seems to work the first time it is run, but if i run it a second time, it sorts the array in the opposite direction, and so on. Any ideas?
for (j = 0; j < i - 1; j++)
for (x = i - 1; x > j; --x)
if (VideoData[x-1].videocat < VideoData[x].videocat)
{
tmp = VideoData[x-1];
VideoData[x-1] = VideoData[x];
VideoData[x] = tmp;
}
// then print statements
-
Here's some code I wrote in java a while ago, the idea is the same for C and C++, except that i'm using vectors here.
Code:
public int bubbleSort() {
String temp;
for(int i=0; i < current_size; i++) {
for(int j=i+1; j < current_size; j++) {
if(lessThan((String)data.elementAt(j),
(String)data.elementAt(i))) {
temp = (String)data.elementAt(j);
data.setElementAt(data.elementAt(i), j);
data.setElementAt(temp, i);
}//end if
}//end for
}//end for
return current_size;
}//end bubbleSort