![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Not a user?
Join Date: Sep 2007
Posts: 308
Rep Power: 2
![]() |
sort problem
I'm using a bubble sort to sort items in an array of classes. When I try to sort, the behaviour is seemingly totally random and I get things all over the place. Can somebody point out what I'm doing wrong here?
vb Syntax (Toggle Plain Text)
|
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: sort problem
That's a lot of code. I'd suggest writing a single sort function to which you can pass a reference to the item to be sorted, possibly the number of items, and a specification of whether the sort is ascending or descending. Get that to work. Then use it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Not a user?
Join Date: Sep 2007
Posts: 308
Rep Power: 2
![]() |
Re: sort problem
That's what I was trying to do, but I can't figure out how to make my classes public domain. As it stands now, I'm creating the classes as needed. I know that's not smart, but hey, it's a work in progress. I tried using a variable to indicate the property to be sorted, but the compiler wants an absolute reference.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: sort problem
A novice programmer is screwed by the language. A slightly advanced programmer is confused by the language. An advanced programmer has to adapt to the stupidities of the langaage. A truly advanced programmer says,"All that is silly, and it sucks."
Which are you, and how should we treat this post?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Not a user?
Join Date: Sep 2007
Posts: 308
Rep Power: 2
![]() |
Re: sort problem
Well, since I just graduated, I'd have to say I'm somewhere between a novice and "slightly advanced" programmer. I'm not really confused by the language, but I sure don't know enough to be efficient. I guess you can call me a bungler at this point; I'll eventually bungle my way to a solution.
As far as how to treat this post, I think you've already treated it. I'll try to use your advice when I get a chance to work on it some more. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: sort problem
Are you suggesting that, in the absence of personal priority, wrong is as good as right? Just curious, since I'm not on the same wavelength as the "new generation."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Not a user?
Join Date: Sep 2007
Posts: 308
Rep Power: 2
![]() |
Re: sort problem
I don't know where you came up with that. I'm not proposing that anything I'm doing is right, I'm trying to learn. As far as a new generation, I'm hardly that. I'm almost 40.
|
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Re: sort problem
I think you are stopping your loops a bit early.
For y As Integer = 0 To wkstation.Length - 3 For x As Integer = y To wkstation.Length - 2 |
|
|
|
|
|
#9 |
|
Not a user?
Join Date: Sep 2007
Posts: 308
Rep Power: 2
![]() |
Re: sort problem
wkstation is an array, length is the number of items in that array. the array is zero-based. I'm comparing the "current" index with the next index. wkstation.length would be 2 on a rank 2 array, but the index would be 0 and 1. so length-1 would be the index of the last item in the array. I want to get the "current" 1 away from the last so that I can compare it to the last one, which is why I have it going to length-2.
I may be wrong on this, but it made sense when I coded it. Now that I think about it, though, I think you could be right, length-3 on a 2 item array would put it at -1. I'll have to consider what I need to do with that. I hope it's as simple as just changing it to length-2. Thanks Dark. Last edited by Jabo; Nov 25th, 2007 at 11:54 PM. |
|
|
|
|
|
#10 |
|
Hobby Coder
Join Date: May 2006
Posts: 58
Rep Power: 0
![]() |
Re: sort problem
This is one right design for a bubblesort, of an array, in C:
(from "C by Dissection" Kelley and Pohl) void swap[int *, int *];
void bubblesort[int a[], int n] /* n is the size of a[] */
{
int i, j;
for( i = 0; i < n-1; ++i)
for( j = n - 1 ; i < j; --j)
if( a[j - 1] > a[j])
swap( &a[j - 1], &a[j]);The comparisons are made from opposite ends of the array, which allow the sorted numbers (usually the lower one's), to "bubble up" to the lower numbered elements of the array. You're clearly not doing this. What you seem to want to use is what I know as a "modified substitution sort", which in my non-professional opinion, is just about the same as a bubble sort, except the array elements being compared are done so in a different way: (QuickBasic) FOR J = 0 TO row - 1 'row is the last data row, not the end of the array
FOR K = J + 1 TO row
IF FoldNames$(J) > FoldNames$(K) THEN
SWAP FoldNames$(J), FoldNames$(K)
END IF
NEXT K
NEXT JSo the first K is set to J + 1 (you don't do that). and J continues up to row - 1, which in this case, is the next to last array element with data. K then goes one element more, to the very last element with data, which completes one "run through" of the array. (that is, J would normally stop at N-2, and K would stop at N-1) So yep, you're stopping short on each sorting run through the array. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Last item in bubble sort goes missing after leaving function | ssrun | C++ | 10 | Sep 3rd, 2006 7:44 PM |
| Sorting algorithms problem....... | MicDareall | Python | 11 | Apr 19th, 2006 3:48 PM |
| Problem Associated with Vector Source code | buggytoast | Java | 3 | Apr 2nd, 2006 6:41 AM |
| sort and swap | brad sue | C | 1 | Mar 25th, 2006 7:33 AM |
| threaded merge sort help | AusTex | C | 1 | May 1st, 2005 5:58 PM |