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.

8 Years
Discussion Span
Last Post by isaackhazi

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.


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 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?


Even better solution is to use Array.Sort in the .NET.
Declare an integer array, put your numbers there call sort method

Dim IntArr(3) As Integer
IntArr(0) = 5
IntArr(1) = 8
IntArr(2) = 9
IntArr(3) = 2

Sort method sorts in the ascending order. To get smallest and largest numbers:

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

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.


OMG why so much of work

int arr[5]
int hig
int low
high = arr[0]
for (i=1;i<5;i++)
if ( arr[i] > high)
   high = arr[i]

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:

highestNum = Integer.MinValue
lowestNum = Integer.MaxValue

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.