This conception I get from a post on CodeProject by Heriberto Lugo. But that was for only 10 numbers. Then I had an idea to use it in vb. There are many shorting process like Bubble Short, Linear Short etc. But exceptionally it does not use loops as others do. It does it by recursion.
If we take an array of 10 elements and store a value from 10 to 1 in each Array Element. It should be like
Let us try to go through the rotation.
Consider for the first element, the stored value is 10. It runs 9 comparison processes. Likely, for the value 9 it runs 8 and so on and for the value 2 it runs only 1 process. Every time the process starts from the array element 0.
Finally we get
How it works.
I create a Sub Procedure “TryToShort”, which is a recursive sub.
The comparison process is as usual. Check the condition if the value of an element is greater than the next element value then swap the values between them after that do same for next.
But when it goes to the end stop and go to the next rotation from the first element of the array. Every time it starts from first element of the array. But in the middle of the process, if it looks that the next element value is less or equal to its value then it stops and the process starts for the next element value. And finally it produces to us a shorted array.
But the question arises here how many times the process would be and also the rotation.
The number of process depends on the number of rotation. Not every rotation has performed equal numbers of processes.
Rotation no is one short from the number of array elements.
Suppose, R is the number of rotation then
R = NumArray.GetUpperBound(0) - 1
We get the rotation number but what is the number of process for every rotation. It differs on each rotation by 1 from the previous.
Number of process is the number of array element minus rotation number, if you take the numbers serially in descending order. Like from 10 to 1 (in example).
Suppose, P is the number of process then
P = NumArray.GetUpperBound(0) – Rotation number
But, if you take the numbers randomly, the calculation is the same. Because when it looks that the next element value is less or equal to it’s the process stops and goes to the next element to perform the process for the next element. After last process in last rotation the sub stops working and produces a shorted array.
But how does the sub stop and how does it assume that it is the last process of last rotation.
I did it by a small control flow into the sub.
Here element number is in consideration, from where we could calculate the rotation number. And after last rotation we could stop the sub.
Here I take a Static variable to store the rotation number.
The codes are
Static round As Integer = 0 'A static variable to hold the number of 'rotation from lowwer bound of array to upperbound of array If i >= num.GetUpperBound(0) Then i = 0 round += 1 'If rotation reaches to the upper bound of Array then exit from sub If round >= num.GetUpperBound(0) Then Exit Sub End If End If
When “i”, the element number reaches to the last element, reset the value of ‘i” to 0 to start the process from the first..
and increment 1 to the rotation number.
Here “round” is the variable for counting the rotation.
round += 1
When “round” reaches to the upper bound of the array then exit from sub.
If round >= num.GetUpperBound(0) Then Exit Sub End If
Now, when do we call the sub recursively?
In the comparison process after each comparison and swapping the values between them we call the sub recursively to perform the next comparison.
The comparison process is
If num(i) > num(i + 1) Then temp = num(i) num(i) = num(i + 1) num(i + 1) = temp 'After every swaping call the procedure Me.TryToShort(num, i + 1) Else 'If the value of an array element is 'less or equel to the next array element 'call the procedure Me.TryToShort(num, i + 1) End If
Here, temp is a variable to store the element value temporarily.
Project design :
Here I take a form with two listboxes, one textbox and four buttons. The design is the following.
NumTetxBox – to write the numbers
AddToListButton – to add the numbers to the listbox as like the order in the array.
NumListBox – to show the numbers, which entered first time.
ShortListBox – to show the shorted array elements.
ShortShowButton – to call the shorting sub..
ClearAllButton – to clear all values
CloseButton – to close the form.
The procedures and events.
Declaring Variables :
Dim numArray(0) As Integer 'Declaring an array to hold the numbers and short them in request Dim numinc As Integer = 0 'To trac the increment of array element
Initializing Controls, Variables :
Private Sub TryToInitialize() 'Try to clear all data from all controls and 'Redimentioning the Array NumTextBox.Text = "" NumListBox.Items.Clear() ShortListBox.Items.Clear() numinc = 0 ReDim numArray(numinc) End Sub
Form_Load event : to initialize first time
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.TryToInitialize() End Sub
NumTextBox_KeyPress event : (This event is to restrict the input except digits, backspace and enter keys.)
Private Sub NumTextBox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles NumTextBox.KeyPress 'Creating here a restriction for input 'into the TextBox except Digits, backspace and enter/return keys Select Case e.KeyChar Case "0" To "9" 'write digits 0-9 Case Chr(8) 'remove digits by pressing backspace key Case Chr(13) : AddToListButton.PerformClick() 'Press Enter key to Perform the Button1 Click Event Case Else : e.Handled = True 'Restrict others key input End Select End Sub
AddToListButton_Click event : to add numbers to the listbox and array
Private Sub AddToListButton_Click(sender As System.Object, e As System.EventArgs) Handles AddToListButton.Click If Not String.IsNullOrWhiteSpace(NumTextBox.Text) Then 'Adding numerical value to the ListBox NumListBox.Items.Add(NumTextBox.Text) 'redimentioning the array by incrementing an element 'and also try to preserve the previous stored values ReDim Preserve numArray(numinc) 'Try to store value into the newly created Array element numArray(numinc) = CInt(Val(NumTextBox.Text)) numinc += 1 End If 'After adding to list clear the text 'and set focus to the textbox NumTextBox.Text = "" NumTextBox.Focus() End Sub
ShortShowButton_Click event : Short and show shorted element values
Private Sub ShortShowButton_Click(sender As System.Object, e As System.EventArgs) Handles ShortShowButton.Click 'Call the sub procedure to short the Array Me.TryToShort(numArray, 0) 'TryToShort.TryToShort(numArray, 0) 'Add and show the shorted numbers For n As Integer = 0 To numArray.GetUpperBound(0) ShortListBox.Items.Add(numArray(n)) Next End Sub
ClearAllButton_Click event :
Private Sub ClearAllButton_Click(sender As System.Object, e As System.EventArgs) Handles ClearAllButton.Click 'Clear all previous data Me.TryToInitialize() End Sub
CloseButton_Click event :
Private Sub CloseButton_Click(sender As System.Object, e As System.EventArgs) Handles CloseButton.Click 'Close the form Me.Close() Me.Dispose() End Sub
TryToShort sub procedure : to short the array elements
Private Sub TryToShort(ByVal num() As Integer, ByVal i As Integer) 'Declare a temporary integer variable Dim temp As Integer Static round As Integer = 0 'A static variable to hold the number of 'rotation from lowwer bound of array to upperbound of array If i >= num.GetUpperBound(0) Then i = 0 round += 1 'If rotation reaches to the upper bound of Array then exit from sub If round >= num.GetUpperBound(0) Then Exit Sub End If End If 'Method to swaping the values from one 'array element to next If num(i) > num(i + 1) Then temp = num(i) num(i) = num(i + 1) num(i + 1) = temp 'After every swaping call the procedure Me.TryToShort(num, i + 1) Else 'If the value of an array element is 'less or equel to the next array element 'call the procedure Me.TryToShort(num, i + 1) End If End Sub
In this Sub Procedure we take two parameters one for the array and other for the element number. And when we call the sub to short the array we pass the Main Array , where we stored the values, entirely as argument value.
After completion of shorting process we get the result as follows.
No Do Loop or For Loop is in use in the shorting process.
The entire project codification is as follows.
Edited by Shark_1