473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
3 1193
willakawill
1,646 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
albertw
267 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

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

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.