By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,986 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,986 IT Pros & Developers. It's quick & easy.

Sorting

P: 5
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
Share this Question
Share on Google+
3 Replies


100+
P: 1,646
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

P: 5
Thanks a bunch! :DDD
Nov 14 '06 #3

100+
P: 267
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.