467,161 Members | 884 Online

Sorting

 Hola... In a console program, I need to prompt the user for one digit numbers, and then sort them from least to greatest or vice versa. We were given something along the lines of: If(mylist(0) < mylist(1)) then Temp = mylist(0) mylist(0) = mylist(1) mylist(1) = Temp Just wondering if there is a better way to go about this? Nov 14 '06 #1
• viewed: 1052
Share:
3 Replies
 1GB Hola... In a console program, I need to prompt the user for one digit numbers, and then sort them from least to greatest or vice versa. We were given something along the lines of: If(mylist(0) < mylist(1)) then Temp = mylist(0) mylist(0) = mylist(1) mylist(1) = Temp Just wondering if there is a better way to go about this? Hi. What you have shown is the swap part of a sort. This is part of any kind of sort (and there are many). One of the easiest to learn is called a bubble sort where the largest values 'bubble' to the top. Expand|Select|Wrap|Line Numbers Private Sub SortArray(ByRef ar() As Long)     Dim temp As Long     Dim lngOuterLoop As Long     Dim lngInnerLoop As Long       For lngOuterLoop = 0 To UBound(ar)         For lngInnerLoop = 0 To UBound(ar) - 1             If ar(lngInnerLoop) > ar(lngInnerLoop + 1) Then                 temp = ar(lngInnerLoop)                 ar(lngInnerLoop) = ar(lngInnerLoop + 1)                 ar(lngInnerLoop + 1) = temp             End If         Next lngInnerLoop     Next lngOuterLoop End Sub   Nov 14 '06 #2
 Thanks a bunch! :DDD Nov 14 '06 #3
 100+ Hi. What you have shown is the swap part of a sort. This is part of any kind of sort (and there are many). One of the easiest to learn is called a bubble sort where the largest values 'bubble' to the top. Expand|Select|Wrap|Line Numbers Private Sub SortArray(ByRef ar() As Long)     Dim temp As Long     Dim lngOuterLoop As Long     Dim lngInnerLoop As Long       For lngOuterLoop = 0 To UBound(ar)         For lngInnerLoop = 0 To UBound(ar) - 1             If ar(lngInnerLoop) > ar(lngInnerLoop + 1) Then                 temp = ar(lngInnerLoop)                 ar(lngInnerLoop) = ar(lngInnerLoop + 1)                 ar(lngInnerLoop + 1) = temp             End If         Next lngInnerLoop     Next lngOuterLoop End Sub   hi shouldn't it be ...? Expand|Select|Wrap|Line Numbers Private Sub SortArray(ByRef ar() As Long)     Dim temp As Long     Dim lngOuterLoop As Long     Dim lngInnerLoop As Long       For lngOuterLoop = 0 To UBound(ar)-1         For lngInnerLoop = lngOuterLoop+1 To UBound(ar)             If ar(lngOuterLoop) > ar(lngInnerLoop) Then                 temp = ar(lngInnerLoop)                 ar(lngInnerLoop) = ar(lngOuterLoop)                 ar(lngOuterLoop) = temp             End If         Next lngInnerLoop     Next lngOuterLoop End Sub   Nov 14 '06 #4