943,654 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 6228
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 26th, 2008
0

Number Sorting

Expand Post »
Hey,
Im having a problem with sorting numbers. I want to sort 4 numbers in orders of size i.e. if the order is 5,8,9,2 I would like it to distinguish the highest number (which in this case would be 9) and lowest (2). Could someone suggest how i would go about doing this. Ive declared each number as num1, num2, num3, num4 and declared both the highestNum and lowestNum. If someone could show any way of doing thsi be it an if statement or array i'd be extremely greatfull.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
-Flipz- is offline Offline
2 posts
since Nov 2008
Nov 26th, 2008
0

Re: Number Sorting

Instead of declaring each number in a separate variable, you should use an array to store the numbers. Once you have them in an array, you then need to decide on a sorting method. There are many sorting algorithms: http://en.wikipedia.org/wiki/Sorting_algorithm

I would recommend Bubble Sort if you are just learning about sorting and sorting algorithms. I would recommend Quick Sort if you need a more efficient general purpose sort; however, this algorithm is slightly more difficult to program.
Reputation Points: 27
Solved Threads: 29
Posting Whiz
timothybard is offline Offline
317 posts
since Mar 2007
Nov 26th, 2008
0

Re: Number Sorting

Yeah simple bubble sort

Define an array [] of integers
Define a counter , lets say its called 'i'

Use a for loop with another loop nested in it

e.g For (i = 0; i<Size of array; i++)

what this means is start the counter at zero. Add 1 to it each time we go round the loop until it = the size of the array (meaning we have read every element in it)

Within that check to see if the first number which is array[i] is > or < than the one after it which would logically be array[i+1]. If so, swap them so they are in order (use a temporary variable)

Keep doing this.
This is where the second loop comes in. We want to keep repeating this whole process for the whole dataset until we go through it and there is not a single swap (indicating its sorted)

Use a boolean variable for that. e.g nest the for loop in a do while loop which runs while !=sorted.

Dont know how to code this in vb, I have Java code if that helps?
Last edited by jbennet; Nov 26th, 2008 at 9:55 am.
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,485 posts
since Apr 2005
Nov 26th, 2008
0

Re: Number Sorting

Even better solution is to use Array.Sort in the .NET.
Declare an integer array, put your numbers there call sort method
VB.NET Syntax (Toggle Plain Text)
  1. Dim IntArr(3) As Integer
  2. IntArr(0) = 5
  3. IntArr(1) = 8
  4. IntArr(2) = 9
  5. IntArr(3) = 2
  6. Array.Sort(IntArr)
Sort method sorts in the ascending order. To get smallest and largest numbers:
VB.NET Syntax (Toggle Plain Text)
  1. Dim highestNum As Integer
  2. Dim lowestNum As Integer
  3. lowestNum = IntArr(0)
  4. highestNum = IntArr(3) ' Or IntArr(IntArr.GetUpperBound(0)) if you make a more general approach
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Nov 26th, 2008
0

Re: Number Sorting

Cheers lads. Actually funny enough i came up with an easier way of doing it. This is what i did.


'Declaring variables
Dim num_1 As integer = 5
Dim num_2 As integer = 8
Dim num_3 As integer = 9
Dim num_4 As integer = 2
Dim highestnum As integer = 0


'Finding the highest production

If num_1 > highestnum Then
highestnum = num_1
End If

If num_2 > highestnum Then
highestnum = num_2
End If

If num_3 > highestnum Then
highestnum = num_3
End If

If num_4 > highestnum Then
highestnum = num_4
End If

Console.Writeline( Highest Number: & highestnum)

For the lowest number you do the same but you declare lowest number as a number that is superior to all numbers. It can be 100000000000000000, as long as its larger.

'Declaring variables
Dim num_1 As integer = 5
Dim num_2 As integer = 8
Dim num_3 As integer = 9
Dim num_4 As integer = 2
Dim lowestnum As integer = 100000000000000000

If num_1 < lowestnum Then
lowestnum = num_1
End If

If num_2 < lowestnum Then
lowestnum = num_2
End If

If num_3 < lowestnum Then
lowestnum = num_3
End If

If num_4 < lowestnum Then
lowestnum = num_4
End If

Console.Writeline( Lowest Number: & lowestnum)

Worked for me. Hope it helps.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
-Flipz- is offline Offline
2 posts
since Nov 2008
Nov 26th, 2008
0

Re: Number Sorting

OMG why so much of work
VB.NET Syntax (Toggle Plain Text)
  1. int arr[5]
  2. int hig
  3. int low
  4. high = arr[0]
  5. for (i=1;i<5;i++)
  6. {
  7. if ( arr[i] > high)
  8. high = arr[i]
  9. }
  10. console.writeline(high);

Thats it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isaackhazi is offline Offline
22 posts
since Sep 2008
Nov 27th, 2008
0

Re: Number Sorting

that one done get the lowest
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,485 posts
since Apr 2005
Nov 27th, 2008
0

Re: Number Sorting

Simple solution and if it's enough for you, that's great. No more C# code in VB.NET forum

I still like to add one point. VB.NET has built-in min/max values:
VB.NET Syntax (Toggle Plain Text)
  1. highestNum = Integer.MinValue
  2. lowestNum = Integer.MaxValue
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Nov 27th, 2008
0

Re: Number Sorting

Quote ...
No more C# code in VB.NET forum
woops i think thats my fault i seem to have confused everyone by using a java-like for loop - it wasnt really meant to be any language in particular, just pseudocode.
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,485 posts
since Apr 2005
Nov 27th, 2008
0

Re: Number Sorting

Im just talking about the logic irrespective of language.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isaackhazi is offline Offline
22 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: I need Urgent help/guidance.
Next Thread in VB.NET Forum Timeline: vb.net crystal report





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC