Hey guys okay so im trying to do a Bubble Sort dealing with an array of data that is being loaded at form load from a text file But right now i keep getting an error everytime it is being executed. I keep getting this error: Index was outside the bounds of the array! But i cant see where I am gng wrong? This is what i am trying to do: my array contains values like this: "C2 H2 SA D4 D2 DA SK ...
PAguilar09 wrote: If ShuffleCards.cards2(i) > ShuffleCards.cards2(i + 1) Then If i counts up to the biggest possible value, when it gets there, is i+1 still inside the array, or not ?
well i set breakpoints and saw that as the loop runs my arraylength is 51 since the index value = 51 because there are 52 values total During the loop i also saw that the values are not being sorted in ascending order it takes the first value in teh array and tests to see if its in order if not then it should swap. I am so lost at why this sort wont work??????
I'm not sure how I can make it clearer. Look at the error message again, then consider if you have 52 items, numbered 0 to 51, and your loop value is 51, and you look for the index at i+1, what will happen.
if your going to use a do and a for - then you need to make sure your not exceeding the size of the array. You can't compare if i+1 is greater then getupperbound(0). so you need to test for that. if you use a for and a for then it will do it for you. Dim sa() As String = { " a" , " z" , " e" , " b" , " j" , " k" , " o" , " c" , " d" , " r" , " n" , " i" , " ...
So if I am looking at item 51... (because it's a 0 based array, therefore 52 items are indexed 0 to 51), you are saying that if I attempt to look at item 51 + 1 = 52, this might be out of bounds on an array of items indexed 0 to 51?
The Man from U.N.C.L.E. replied 1 month, 2 weeks ago
I remember developing a wonderful bit of code for a bubblesort algorithm. Imagine how gutted I was when I discovered that by writing a simple IComparer implementation I could take advantage of the much better bubblesort algorithim built in to .Net. Just like Microsoft to develop the same code already. Serves me right for not checking the documentation first.
well i set breakpoints and saw that as the loop runs my arraylength is 51 since the index value = 51 because there are 52 values total During the loop i also saw that the values are not being sorted in ascending order it takes the first value in teh array and tests to see if its in order if not then it should swap. I am so lost at why this sort wont work??????
if your going to use a do and a for - then you need to make sure your not exceeding the size of the array. You can't compare if i+1 is greater then getupperbound(0). so you need to test for that. if you use a for and a for then it will do it for you. Dim sa() As String = { " a" , " z" , " e" , " b" , " j" , " k" , "...
I remember developing a wonderful bit of code for a bubblesort algorithm. Imagine how gutted I was when I discovered that by writing a simple IComparer implementation I could take advantage of the much better bubblesort algorithim built in to .Net. Just like Microsoft to develop the same code already. Serves me right for not checking the documentation first.
Related threads on "CodeProject: Discussion Boards. Free source code and programming help":
OK Excel, what's the deal with this fucked-up sort... OK Excel, what's the deal with this fucked-up sort routine? Don't make me write a bubble-sort in VBA or I swear I'll bulk-erase my hard disk
12:29 PM July 14, 2008
from web
Thread profile page for "BUBBLE SORT" on http://www.codeproject.com.
This report page is a snippet summary view from a single thread "BUBBLE SORT", located on the Message Board at http://www.codeproject.com.
This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity