467,161 Members | 884 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

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
  1. Private Sub SortArray(ByRef ar() As Long)
  2.     Dim temp As Long
  3.     Dim lngOuterLoop As Long
  4.     Dim lngInnerLoop As Long
  5.  
  6.     For lngOuterLoop = 0 To UBound(ar)
  7.         For lngInnerLoop = 0 To UBound(ar) - 1
  8.             If ar(lngInnerLoop) > ar(lngInnerLoop + 1) Then
  9.                 temp = ar(lngInnerLoop)
  10.                 ar(lngInnerLoop) = ar(lngInnerLoop + 1)
  11.                 ar(lngInnerLoop + 1) = temp
  12.             End If
  13.         Next lngInnerLoop
  14.     Next lngOuterLoop
  15. End Sub
  16.  
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
  1. Private Sub SortArray(ByRef ar() As Long)
  2.     Dim temp As Long
  3.     Dim lngOuterLoop As Long
  4.     Dim lngInnerLoop As Long
  5.  
  6.     For lngOuterLoop = 0 To UBound(ar)
  7.         For lngInnerLoop = 0 To UBound(ar) - 1
  8.             If ar(lngInnerLoop) > ar(lngInnerLoop + 1) Then
  9.                 temp = ar(lngInnerLoop)
  10.                 ar(lngInnerLoop) = ar(lngInnerLoop + 1)
  11.                 ar(lngInnerLoop + 1) = temp
  12.             End If
  13.         Next lngInnerLoop
  14.     Next lngOuterLoop
  15. End Sub
  16.  
hi

shouldn't it be ...?

Expand|Select|Wrap|Line Numbers
  1. Private Sub SortArray(ByRef ar() As Long)
  2.     Dim temp As Long
  3.     Dim lngOuterLoop As Long
  4.     Dim lngInnerLoop As Long
  5.  
  6.     For lngOuterLoop = 0 To UBound(ar)-1
  7.         For lngInnerLoop = lngOuterLoop+1 To UBound(ar)
  8.             If ar(lngOuterLoop) > ar(lngInnerLoop) Then
  9.                 temp = ar(lngInnerLoop)
  10.                 ar(lngInnerLoop) = ar(lngOuterLoop)
  11.                 ar(lngOuterLoop) = temp
  12.             End If
  13.         Next lngInnerLoop
  14.     Next lngOuterLoop
  15. End Sub
  16.  
Nov 14 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by dont bother | last post: by
7 posts views Thread by Federico G. Babelis | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
4 posts views Thread by Ambica Jain | last post: by
5 posts views Thread by lemlimlee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.