Solved

VBA bubble sort

Posted on 2013-11-25
1
340 Views
Last Modified: 2013-11-25
Could anyone refer me to a bubble sort that works?

I have the following code below, but it fails.

I receive the following error...

Compile error:
Expected: )

The statement below turns red...
If (ToSort(BubbleSort) ToSort(BubbleSort + 1) And SortAscending) _
          Or (ToSort(BubbleSort) < ToSort(BubbleSort + 1) And Not SortAscending) Then

I am just not seeing what the compiler is referring to. Could someone please help.

The Code...
Sub BubbleSort(ToSort As Variant, Optional SortAscending As Boolean = True)

Dim AnyChanges As Boolean
Dim BubbleSort As Long
Dim SwapFH As Variant

Do
  AnyChanges = False
    For BubbleSort = LBound(ToSort) To UBound(ToSort) - 1
      If (ToSort(BubbleSort) ToSort(BubbleSort + 1) And SortAscending) _
          Or (ToSort(BubbleSort) < ToSort(BubbleSort + 1) And Not SortAscending) Then
          ' These two need to be swapped
          SwapFH = ToSort(BubbleSort)
          ToSort(BubbleSort) = ToSort(BubbleSort + 1)
          ToSort(BubbleSort + 1) = SwapFH
          AnyChanges = True
      End If
    Next BubbleSort
Loop Until Not AnyChanges

End Sub

Thanks in advance.
0
Comment
Question by:tesla764
1 Comment
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
ID: 39675408
You're missing the comparison operator in the first part of your comparison: You want

 If (ToSort(BubbleSort) > ToSort(BubbleSort + 1) And SortAscending) ...

Note the ">" which is currently missing.

But don't get me started on the appalling inefficiency of a bubble sort...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now