| | |
Number Sorting
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
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.
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.
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?
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.
Even better solution is to use Array.Sort in the .NET.
Declare an integer array, put your numbers there call sort method
Sort method sorts in the ascending order. To get smallest and largest numbers:
Declare an integer array, put your numbers there call sort method
VB.NET Syntax (Toggle Plain Text)
Dim IntArr(3) As Integer IntArr(0) = 5 IntArr(1) = 8 IntArr(2) = 9 IntArr(3) = 2 Array.Sort(IntArr)
VB.NET Syntax (Toggle Plain Text)
Dim highestNum As Integer Dim lowestNum As Integer lowestNum = IntArr(0) highestNum = IntArr(3) ' Or IntArr(IntArr.GetUpperBound(0)) if you make a more general approach
Teme64 @ Windows Developer Blog
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
'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.
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 0
OMG why so much of work
Thats it.
VB.NET Syntax (Toggle Plain Text)
int arr[5] int hig int low high = arr[0] for (i=1;i<5;i++) { if ( arr[i] > high) high = arr[i] } console.writeline(high);
Thats it.
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:

I still like to add one point. VB.NET has built-in min/max values:
VB.NET Syntax (Toggle Plain Text)
highestNum = Integer.MinValue lowestNum = Integer.MaxValue
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- hi, need help with sorting (Python)
- YaBasic: The beginners choice (Computer Science)
- Need Help with ArrayList sorting (Java)
- Sorting Video titles using a ID numbers (Java)
- What is the error in sorting names and tolower function? (C++)
- sorting an array of string (C)
- help by sorting a simply linked list (C)
- sorting 2d arrays (C)
- re: Sorting Algorithms (C++)
Other Threads in the VB.NET Forum
- Previous Thread: I need Urgent help/guidance.
- Next Thread: vb.net crystal report
| Thread Tools | Search this Thread |
.net 30minutes 2005 2008 access account arithmetic array basic binary bing button buttons center check code combobox component connectionstring crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dissertationthesis dropdownlist excel fade file-dialog folder ftp generatetags google gridview hardcopy image images input insert intel internet listview mobile monitor ms net networking objects output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print problem problemwithinstallation project remove reports" save searchbox searchvb.net select serial shutdown soap survey table tcp temperature text textbox timer toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf






