Sorting Numbers

How to Organise Numbers.
Numbers, we all know but how to organise... It's quite Simple.
Very easy sample of how to sort numbers (of an array).

This part sort from Small to Big

// First you need a simple Form and then place a button
// Declaration in the Form Class
Dim A(20) As Integer
Dim num, i, j, k, arr, temp As Integer
// And this is what will start all
Private Sub Command1_Click()
arr = 0
msgbox "Your array contains: "
For k = 0 To num - 1
msgbox "A" & "(" & arr & "):" & A(k)
arr = arr + 1
Next k
End Sub
// To make some starting put this in the FormLoad
Private Sub Form_Load()
num = InputBox("Intialize the size of your array:")
For i = 0 To num - 1
A(i) = InputBox("Enter your array: ")
Next i

'this will sort your array in ascending order
For i = 0 To num - 1
For j = i + 1 To num - 1
If A(i) > A(j) Then
temp = A(i)
A(i) = A(j)
A(j) = temp
End If
Next j
Next i

End Sub

// In the Reverse Sense.
// Won't explain now I think you know the basics
Dim A(20) As Integer
Dim num, i, j, k, arr, temp As Integer
Private Sub Command1_Click()
arr = 0
msgbox "Your array contains: "
For k = 0 To num - 1
msgbox "A" & "(" & arr & "):" & A(k)
arr = arr + 1
Next k
End Sub
Private Sub Form_Load()
num = InputBox("Intialize the size of your array:")
For i = 0 To num - 1
A(i) = InputBox("Enter your array: ")
Next i

'this will sort your array in descending order
For i = 0 To num - 1
For j = i + 1 To num - 1
If A(i) < A(j) Then
temp = A(i)
A(i) = A(j)
A(j) = temp
End If
Next j
Next i

End Sub

// when compilation..
An input box will ask for the array length.
Then you'll be asked for each numbers
Sorted and displayed Msgbox by Msgbox

0 comments:

Post a Comment