Number Sorting

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 2
Reputation: -Flipz- is an unknown quantity at this point 
Solved Threads: 0
-Flipz- -Flipz- is offline Offline
Newbie Poster

Number Sorting

 
0
  #1
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 306
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 26
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: Number Sorting

 
0
  #2
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,204
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 538
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Number Sorting

 
0
  #3
Nov 26th, 2008
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.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Number Sorting

 
0
  #4
Nov 26th, 2008
Even better solution is to use Array.Sort in the .NET.
Declare an integer array, put your numbers there call sort method
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: -Flipz- is an unknown quantity at this point 
Solved Threads: 0
-Flipz- -Flipz- is offline Offline
Newbie Poster

Re: Number Sorting

 
0
  #5
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: isaackhazi is an unknown quantity at this point 
Solved Threads: 0
isaackhazi isaackhazi is offline Offline
Newbie Poster

Re: Number Sorting

 
0
  #6
Nov 26th, 2008
OMG why so much of work
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,204
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 538
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Number Sorting

 
0
  #7
Nov 27th, 2008
that one done get the lowest
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Number Sorting

 
0
  #8
Nov 27th, 2008
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:
  1. highestNum = Integer.MinValue
  2. lowestNum = Integer.MaxValue
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,204
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 538
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Number Sorting

 
0
  #9
Nov 27th, 2008
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.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: isaackhazi is an unknown quantity at this point 
Solved Threads: 0
isaackhazi isaackhazi is offline Offline
Newbie Poster

Re: Number Sorting

 
0
  #10
Nov 27th, 2008
Im just talking about the logic irrespective of language.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC