
Easy Bubble Sort Question
I am trying to bubble sort salespeople in descending order with the following coding, however it is sorting all but one value.<BR><BR>Thanks for any help you can provide in advance.<BR><BR>================================== =================<BR>'in this scenario, noe happens to be 10<BR><BR> for j = 1 to noe1<BR> for i = 1 to noe<BR> if sales(j) > sales(i) then<BR> temp = sales(i)<BR> sales(i) = sales(j)<BR> sales(j) = temp<BR> temp = name(i)<BR> name(i) = name(j)<BR> name(j) = temp<BR> end if<BR> next 'i<BR> next 'j

All wrong...
for j = noe1 To 1 Step 1<BR> for i = 1 to j<BR> if sales(i) < sales(i+1) then<BR> temp = sales(i) : sales(i)=sales(i+1) : sales(i+1)=temp<BR> temp = name(i) : name(i)=name(i+1) : name(i+1)=temp<BR> end if<BR> next 'i<BR>next 'j<BR><BR>

Still doing same thing
I changed my coding with yours and it still sorts all but one element.... say noe is 10 elements big...<BR><BR>1= 100,000<BR>2= 200,000<BR>3= 300,000<BR>4= 400,000<BR>5= 500,000<BR>6= 600,000<BR>7= 700,000<BR>8= 800,000<BR>9= 900,000<BR>10 = 1,000,000<BR><BR><BR>it is sorting them like this:<BR><BR>1 = 900,000<BR>2 = 800,000<BR>3 = 700,000<BR>4 = 600,000<BR>5 = 500,000<BR>4 = 400,000<BR>3 = 300,000<BR>2 = 1,000,000<BR>1 = 100,000<BR><BR><BR>These are the exact results I was getting with my previous formula... for some reason it seems to not sort the largest element in both scenarios...<BR><BR>

Sorry!
I followed your lead, and start with 1!<BR><BR>DOH! Arrays in VBS always start at *ZERO*!<BR><BR>DEMO:<BR><BR><HTML>< BODY><BR><BR><%<BR>sales = array( 1,2,3,4,5,6,7,8,9,10 )<BR><BR>noe = UBound(sales)<BR>for j = noe1 To 0 Step 1 <BR> for i = 0 to j <BR> if sales(i) < sales(i+1) then <BR> temp = sales(i) : sales(i)=sales(i+1) : sales(i+1)=temp <BR>' temp = name(i) : name(i)=name(i+1) : name(i+1)=temp <BR> end if <BR> next 'i <BR>next 'j <BR><BR>For i = 0 to 9 <BR> Response.Write sales(i) & "<BR/>"<BR>Next<BR>%><BR><BR></BODY></HTML><BR>

Interesting...
I looked at this again...<BR><BR>And I see why it does work!<BR><BR>It's not a "bubble sort". Dunno what name should be given to it.<BR><BR>So maybe the reason it doesn't work is because the elements of your "sales" array are *STRINGS*?<BR><BR>After all, the *STRING* "1,000,000" *IS* smaller than "200,000" (because even the first characters"1" vs. "2" are enough to distinguish them).<BR><BR>Maybe you should be doing<BR> If CDBL(sales(j)) > CDBL(sales(i)) Then<BR>???<BR><BR>

To make your code work...
You have to use<BR> for j = 0 to noe<BR> for i = 0 to noe<BR><BR>You can't go to just noe1 for the outer loop.<BR><BR>This means you will truly be doing an "Nsquared" number of comparisons.<BR><BR>With a true bubble sort, you do half that number. It's not a great improvement (compared to things like a Shell sort, say), but half is half.<BR><BR>

And to make it work faster...
Sigh. Shame on me. It's easy to modify your code to be the same speed as a traditional bubble sort.<BR><BR><HTML><BODY><BR>< BR><%<BR>' sales = array( 7,3,1,10,4,8,2,6,9,5 )<BR>sales = array( 1,2,3,4,5,6,7,8,9,10 )<BR><BR>noe = UBound(sales)<BR>for j = 0 to noe <BR> for i = [hl="yellow"]j[/hl] to noe <BR> if sales(i) > sales(j) then <BR> temp = sales(i) : sales(i) = sales(j) : sales(j) = temp <BR> end if <BR> next 'i <BR>next 'j <BR>For i = 0 to 9 <BR> Response.Write i & ": " & sales(i) & "<BR/>"<BR>Next<BR>%><BR><BR></BODY></HTML><BR><BR>Presto. Half the time of starting with zero in the inner loop.<BR><BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

