Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Calling Bubble Sort Function

I have an 1D array that I have filled within a For Next Loop. There
are roughly 60,000 values within the array. Upon filling the array, I
want to sort the data within the array. The array is called
CalculationArray. When I call the bubble sort function, it fails
within the function and gives a type mismatch error. (It fails on "For
i = 1 To List - 1").

Why is this so?


'Call Sort Function and Sort Bidding Array

BubbleSort CalculationArray

Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Long, Last As Long
Dim i As Long, j As Long
Dim Temp As Long

First = LBound(List)
Last = UBound(List)
For i = 1 To List - 1 'THIS IS WHERE IT FAILS
For j = i + 1 To List
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i

End Function

Thanks

TS
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Calling Bubble Sort Function

Bubble sort is too slow. This was written to sort an Excel spreadsheet
but you can definitely adapt it to work with your array. Basically it
searches the un-sorted items to find the lowest value and places that
at the top.

Sub SortByCount(ctrStart As Long, ctrEnd As Long)

Dim foo As Integer
Dim bar As Integer
Dim fizz As Integer
Dim nums As Worksheet

Set nums = Worksheets("FinalNumbers")
bar = ctrStart

Do
foo = 0
fizz = -100
For x = bar To ctrEnd
If nums.Cells(x, 7).Value = fizz Then
foo = x
fizz = nums.Cells(x, 7).Value
End If
Next x

If foo < bar And foo < 0 Then
nums.Rows(foo).Cut
nums.Rows(bar).Insert
End If

bar = bar + 1
Loop Until bar = ctrEnd

End Sub


---
Message posted from http://www.ExcelForum.com/

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Calling Bubble Sort Function

Hello Terry,

I believe the error comes from using the wrong limits on the loop.
Replace "For i = 1 to List -1" with...
"For i = First to Last"

Regards,
Jim Cone
San Francisco, CA
wrote in message
om...
I have an 1D array that I have filled within a For Next Loop. There
are roughly 60,000 values within the array. Upon filling the array, I
want to sort the data within the array. The array is called
CalculationArray. When I call the bubble sort function, it fails
within the function and gives a type mismatch error. (It fails on "For
i = 1 To List - 1").
Why is this so?
'Call Sort Function and Sort Bidding Array
BubbleSort CalculationArray
Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Long, Last As Long
Dim i As Long, j As Long
Dim Temp As Long


First = LBound(List)
Last = UBound(List)
For i = 1 To List - 1 'THIS IS WHERE IT FAILS
For j = i + 1 To List
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i
End Function
Thanks
TS



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Calling Bubble Sort Function

Your For...Next Loop does not recognize "List" as a number. It is an
Array right? What you want to use in it's stead is the number of items
in the array. - Pikus


---
Message posted from http://www.ExcelForum.com/

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Calling Bubble Sort Function

Also, unless you specifically changed this, the first item in the array
is numbered 0 (zero). So you want to do it like:

For x = 0 To listCount - 1

- Pikus


---
Message posted from http://www.ExcelForum.com/



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Calling Bubble Sort Function


Some comments on the advice so far:
a bubble sort is one of the slowest sort algorithms
for the "i" loop you want to go to last - 1
You don't want to use the number of items in the array - you want to look at
the lower and upper bounds.
you can't assume list has a lower bound of 0


Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Long, Last As Long
Dim i As Long, j As Long
Dim Temp As Long

First = LBound(List)
Last = UBound(List)
For i = First To Last - 1
For j = i + 1 To Last
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i

End Function

--
Regards,
Tom Ogilvy


wrote in message
om...
I have an 1D array that I have filled within a For Next Loop. There
are roughly 60,000 values within the array. Upon filling the array, I
want to sort the data within the array. The array is called
CalculationArray. When I call the bubble sort function, it fails
within the function and gives a type mismatch error. (It fails on "For
i = 1 To List - 1").

Why is this so?


'Call Sort Function and Sort Bidding Array

BubbleSort CalculationArray

Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Long, Last As Long
Dim i As Long, j As Long
Dim Temp As Long

First = LBound(List)
Last = UBound(List)
For i = 1 To List - 1 'THIS IS WHERE IT FAILS
For j = i + 1 To List
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i

End Function

Thanks

TS



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Calling Bubble Sort Function

This is something I wrote to sort rows of information in a spreadsheet.
If you adapt it to what you're doing it should be faster. Let me know
what you think. - Pikus

Sub RankByCount(ctrStart As Long, ctrEnd As Long)

Dim foo As Integer
Dim bar As Integer
Dim fizz As Integer
Dim nums As Worksheet

Set nums = Worksheets("FinalNumbers")
bar = ctrStart

Do
foo = 0
fizz = -100
For x = bar To ctrEnd
If nums.Cells(x, 7).Value = fizz Then
foo = x
fizz = nums.Cells(x, 7).Value
End If
Next x

If foo < bar And foo < 0 Then
nums.Rows(foo).Cut
nums.Rows(bar).Insert
End If

bar = bar + 1
Loop Until bar = ctrEnd

End Sub


---
Message posted from http://www.ExcelForum.com/

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 'bubble' chart where colors are used instead of bubble size paulmichael Charts and Charting in Excel 3 December 5th 07 03:33 PM
How can I change the color of negative bubble in bubble chart Jon Peltier Charts and Charting in Excel 0 July 4th 07 03:29 PM
How can I change the color of negative bubble in bubble chart Daniel Charts and Charting in Excel 1 July 4th 07 03:25 PM
ho to change in the bubble chart the bubble position and size laszlo Charts and Charting in Excel 0 March 25th 05 04:45 PM
How do I move a hidden bubble to the front in a bubble chart in E. Scott Excel Discussion (Misc queries) 0 February 20th 05 07:55 PM


All times are GMT +1. The time now is 03:30 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"