Solved

VBA bubble sort

Posted on 2013-11-25
1
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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