newbie question

Justin Shaw wyojustin at hotmail.com
Sat Dec 14 13:12:45 EST 2002


Compare your code to this java code I found.  Looks like you should only
swap i, and i+1.

public void sort()
{
for ( int pass=1; pass < a.length; pass++ ) { // passes
swapMade = false; // default to no swap made
for ( int i = 0; i < a.length - pass; i++ ) // one pass integer 1 changed to
pass

if ( a[ i ] > a[ i + 1 ] ) { // one comparison
hold = a[i]; // one swap
a[ i ] = a[ i + 1 ];
a[ i + 1 ] = hold;
swapMade = true; // there was a swap made
}
if ( swapMade = false ) // no changes last pass so we are done
break;
}
}

Justin

"Classified" <stephenxbchen at hotmail.com> wrote in message
news:pan.2002.12.14.15.56.07.271399 at hotmail.com...
> Hi, I'm new to python and I encounter a problem...
> I'm frustrated because the following code won't work...
> a=[('a',1),('c',5),('k',5),('o',3), ('i',3)]
> for i in range(0, len(a)-1):
> for j in range(1, len(a)):
> if a[i][1]>a[j][1]:
> a[i],a[j]=a[j],a[i]
> print a
> it appears that the bubble sort will not sort a...
> I guess I must have made a stupid mistake somewhere...
> I'm a newbie, I don't know how to use the debugger...
> thanks to whoever that can help me!





More information about the Python-list mailing list