I can't seem to get this program to sort. Not sure why. Can anyone see a problem with my bubble sort? thanks.
Diapers are the best invention
Joined: Nov 04, 2001
hi this algo seems sorting in descending order. if u want to have ascending order sorting then change the comparision if() u 've like... if ( a[i].compareTo(a[i-1]) < 0 ).. it shd work... regards maulin
Jeremiah. First, due to the uninitialized stringArray, NullPointer exception was thrown when sort() method was called (check with MS-DOS for this exception). I put one line into init() to initialize stringArray... current inital value is "". for(int i=0;i<stringArray.length;i++) stringArray[i]=""; Second, as I dont really see why the sort() does not work as expected, I made some changes on the index inside the for loop of the sort() in a way I know how sorting is done,
then re-run the applet, the result is not perfect, but sorting is on the way. HTH [ April 24, 2003: Message edited by: chi Lin ]
not so smart guy still curious to learn new stuff every now and then
Joined: Mar 29, 2003
Chi, I tried it your way, but it seems like the sorting jumps about 8 index's down the array. not sure why your statement did get me somewhere. Thanks for that. [ April 24, 2003: Message edited by: Jeremiah Coleman ]
Joined: Aug 20, 2001
Just as an aside, since I'm not sure what your assignment entails, I would suggest using a List instead of an Array to hold your Strings and then sorting your list. Your sorting algorithm works fine, but as Balaji pointed out, the sortwing was failing when it encountered a null value in the Array. It woudl therefore do some sorting, but stop when it got the NullPointerException. If you filled the array with 10 variables it worked fine (albiet reversed; see Maulin's post). in general, you would still have to check for null, but in this instance you cnnot put null values into the list (just empty strings). And as an added benefit, you List is not limited to 10 entries. You can add as many as you like (and then see the performance difference between bubble-sorting 10 items and 300 items )
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Mar 29, 2003
Thanks you to everybody who replied. I appreciate your time. I tried the if (array[i]!=null) and it worked