Easy Bubble Sort Question

# Thread: Easy Bubble Sort Question

1. Member
Join Date
Dec 1969
Posts
46

## 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>&#039;in this scenario, noe happens to be 10<BR><BR> for j = 1 to noe-1<BR> for i = 1 to noe<BR> if sales(j) &#062; 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 &#039;i<BR> next &#039;j

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## All wrong...

for j = noe-1 To 1 Step -1<BR> for i = 1 to j<BR> if sales(i) &#060; 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 &#039;i<BR>next &#039;j<BR><BR>

3. Member
Join Date
Dec 1969
Posts
46

## 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>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Sorry!

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

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Interesting...

I looked at this again...<BR><BR>And I see why it does work!<BR><BR>It&#039;s not a "bubble sort". Dunno what name should be given to it.<BR><BR>So maybe the reason it doesn&#039;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)) &#062; CDBL(sales(i)) Then<BR>???<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## 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&#039;t go to just noe-1 for the outer loop.<BR><BR>This means you will truly be doing an "N-squared" number of comparisons.<BR><BR>With a true bubble sort, you do half that number. It&#039;s not a great improvement (compared to things like a Shell sort, say), but half is half.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## And to make it work faster...

Sigh. Shame on me. It&#039;s easy to modify your code to be the same speed as a traditional bubble sort.<BR><BR>&#060;HTML&#062;&#060;BODY&#062;<BR>< BR>&#060;%<BR>&#039; 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) &#062; sales(j) then <BR> temp = sales(i) : sales(i) = sales(j) : sales(j) = temp <BR> end if <BR> next &#039;i <BR>next &#039;j <BR>For i = 0 to 9 <BR> Response.Write i & ": " & sales(i) & "&#060;BR/&#062;"<BR>Next<BR>%&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<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
•