Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2007, 6:25 AM   #1
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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)
  1. If asc = True Then
  2. Select Case col
  3. Case 1
  4. For y As Integer = 0 To wkstation.Length - 3
  5. For x As Integer = y To wkstation.Length - 2
  6. If String.Compare(wkstation(x).showsmhp, wkstation(x + 1).showsmhp) > 0 Then
  7. temp = wkstation(x)
  8. wkstation(x) = wkstation(x + 1)
  9. wkstation(x + 1) = temp
  10. End If
  11. Next
  12. Next
  13. Case 2
  14. For y As Integer = 0 To wkstation.Length - 3
  15. For x As Integer = y To wkstation.Length - 2
  16. If String.Compare(wkstation(x).shownovell, wkstation(x + 1).shownovell) > 0 Then
  17. temp = wkstation(x)
  18. wkstation(x) = wkstation(x + 1)
  19. wkstation(x + 1) = temp
  20. End If
  21. Next
  22. Next
  23. Case 3
  24. For y As Integer = 0 To wkstation.Length - 3
  25. For x As Integer = y To wkstation.Length - 2
  26. If String.Compare(wkstation(x).showcmid, wkstation(x + 1).showcmid) > 0 Then
  27. temp = wkstation(x)
  28. wkstation(x) = wkstation(x + 1)
  29. wkstation(x + 1) = temp
  30. End If
  31. Next
  32. Next
  33. Case 4
  34. For y As Integer = 0 To wkstation.Length - 3
  35. For x As Integer = y To wkstation.Length - 2
  36. If String.Compare(wkstation(x).showloc, wkstation(x + 1).showloc) > 0 Then
  37. temp = wkstation(x)
  38. wkstation(x) = wkstation(x + 1)
  39. wkstation(x + 1) = temp
  40. End If
  41. Next
  42. Next
  43. Case 5
  44. For y As Integer = 0 To wkstation.Length - 3
  45. For x As Integer = y To wkstation.Length - 2
  46. If String.Compare(wkstation(x).showuser, wkstation(x + 1).showuser) > 0 Then
  47. temp = wkstation(x)
  48. wkstation(x) = wkstation(x + 1)
  49. wkstation(x + 1) = temp
  50. End If
  51. Next
  52. Next
  53. Case 6
  54. For y As Integer = 0 To wkstation.Length - 3
  55. For x As Integer = y To wkstation.Length - 2
  56. If String.Compare(wkstation(x).showport, wkstation(x + 1).showport) > 0 Then
  57. temp = wkstation(x)
  58. wkstation(x) = wkstation(x + 1)
  59. wkstation(x + 1) = temp
  60. End If
  61. Next
  62. Next
  63. Case 7
  64. For y As Integer = 0 To wkstation.Length - 3
  65. For x As Integer = y To wkstation.Length - 2
  66. If String.Compare(wkstation(x).showip, wkstation(x + 1).showip) > 0 Then
  67. temp = wkstation(x)
  68. wkstation(x) = wkstation(x + 1)
  69. wkstation(x + 1) = temp
  70. End If
  71. Next
  72. Next
  73. Case 8
  74. For y As Integer = 0 To wkstation.Length - 3
  75. For x As Integer = y To wkstation.Length - 2
  76. If String.Compare(wkstation(x).showcrt, wkstation(x + 1).showcrt) > 0 Then
  77. temp = wkstation(x)
  78. wkstation(x) = wkstation(x + 1)
  79. wkstation(x + 1) = temp
  80. End If
  81. Next
  82. Next
  83. End Select
  84. Else
  85. Select Case col
  86. Case 1
  87. For y As Integer = 0 To wkstation.Length - 3
  88. For x As Integer = y To wkstation.Length - 2
  89. If String.Compare(wkstation(x).showsmhp, wkstation(x + 1).showsmhp) < 0 Then
  90. temp = wkstation(x)
  91. wkstation(x) = wkstation(x + 1)
  92. wkstation(x + 1) = temp
  93. End If
  94. Next
  95. Next
  96. Case 2
  97. For y As Integer = 0 To wkstation.Length - 3
  98. For x As Integer = y To wkstation.Length - 2
  99. If String.Compare(wkstation(x).shownovell, wkstation(x + 1).shownovell) < 0 Then
  100. temp = wkstation(x)
  101. wkstation(x) = wkstation(x + 1)
  102. wkstation(x + 1) = temp
  103. End If
  104. Next
  105. Next
  106. Case 3
  107. For y As Integer = 0 To wkstation.Length - 3
  108. For x As Integer = y To wkstation.Length - 2
  109. If String.Compare(wkstation(x).showcmid, wkstation(x + 1).showcmid) < 0 Then
  110. temp = wkstation(x)
  111. wkstation(x) = wkstation(x + 1)
  112. wkstation(x + 1) = temp
  113. End If
  114. Next
  115. Next
  116. Case 4
  117. For y As Integer = 0 To wkstation.Length - 3
  118. For x As Integer = y To wkstation.Length - 2
  119. If String.Compare(wkstation(x).showloc, wkstation(x + 1).showloc) < 0 Then
  120. temp = wkstation(x)
  121. wkstation(x) = wkstation(x + 1)
  122. wkstation(x + 1) = temp
  123. End If
  124. Next
  125. Next
  126. Case 5
  127. For y As Integer = 0 To wkstation.Length - 3
  128. For x As Integer = y To wkstation.Length - 2
  129. If String.Compare(wkstation(x).showuser, wkstation(x + 1).showuser) < 0 Then
  130. temp = wkstation(x)
  131. wkstation(x) = wkstation(x + 1)
  132. wkstation(x + 1) = temp
  133. End If
  134. Next
  135. Next
  136. Case 6
  137. For y As Integer = 0 To wkstation.Length - 3
  138. For x As Integer = y To wkstation.Length - 2
  139. If String.Compare(wkstation(x).showport, wkstation(x + 1).showport) < 0 Then
  140. temp = wkstation(x)
  141. wkstation(x) = wkstation(x + 1)
  142. wkstation(x + 1) = temp
  143. End If
  144. Next
  145. Next
  146. Case 7
  147. For y As Integer = 0 To wkstation.Length - 3
  148. For x As Integer = y To wkstation.Length - 2
  149. If String.Compare(wkstation(x).showip, wkstation(x + 1).showip) < 0 Then
  150. temp = wkstation(x)
  151. wkstation(x) = wkstation(x + 1)
  152. wkstation(x + 1) = temp
  153. End If
  154. Next
  155. Next
  156. Case 8
  157. For y As Integer = 0 To wkstation.Length - 3
  158. For x As Integer = y To wkstation.Length - 2
  159. If String.Compare(wkstation(x).showcrt, wkstation(x + 1).showcrt) < 0 Then
  160. temp = wkstation(x)
  161. wkstation(x) = wkstation(x + 1)
  162. wkstation(x + 1) = temp
  163. End If
  164. Next
  165. Next
  166. End Select
  167. End If
Jabo is offline   Reply With Quote
Old Nov 25th, 2007, 7:48 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 25th, 2007, 6:29 PM   #3
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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.
Jabo is offline   Reply With Quote
Old Nov 25th, 2007, 7:01 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 25th, 2007, 9:30 PM   #5
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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.
Jabo is offline   Reply With Quote
Old Nov 25th, 2007, 9:45 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 25th, 2007, 9:57 PM   #7
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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.
Jabo is offline   Reply With Quote
Old Nov 25th, 2007, 11:23 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
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
I think that your y loop should be going to wkstation.Length - 2. Picture that case where you only have two items, the original code would not enter the loop at all.
The Dark is online now   Reply With Quote
Old Nov 25th, 2007, 11:38 PM   #9
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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.
Jabo is offline   Reply With Quote
Old Dec 22nd, 2007, 12:11 PM   #10
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 58
Rep Power: 0 Adak is an unknown quantity at this point
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 J

So 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.
Adak is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC