Monday, March 21, 2011

Sorting of Elements in an Array

'#####################################################
'Program to illustrate sorting of elements of an array
'This program takes the array boundary from the user
'Validate if it is a valid number <=10  
'Get the list of elements from the user  
' Validate if it is a number  
' Finally Sorts and displays the contents of the array
'#####################################################  

'Variable Declarations 
Dim nArray(),nArrayBound 
Dim nCounter, nCtr1,nCtr2 
Dim nTempVar,sTempStr 
Dim bFlag 

'Get the number of elements as input from the user nArrayBound=inputbox("Enter a number between 5 and 10" 

'Validate the input (if it is a valid number) 
bFlag=False  

Do While bFlag=False  

   If isNumeric(nArrayBound) Then 'Valid number   

      If nArrayBound<=10 Then 'Lesser than 10    

         If nArrayBound>=5 Then  'Greater than 5

            bFlag=True

         Else

            bFlag=False
            nArrayBound=inputbox("Enter a number greater than 5")

         End If

       Else

         bFlag=False
         nArrayBound=inputbox("Enter a number Less than or equal to 10")

       End If

   Else

       bFlag=False
       nArrayBound=inputbox("Enter a valid number")

   End If

Loop

'Redim the array decalred with the number of elements
ReDim nArray(nArrayBound-1) ' -1 because the array index starts with 0
nArrayBound=cint(ubound(nArray)) 'moving the upper boundary of the array to a variable

For nCounter=0 to nArrayBound

   'Get the elements of the array from the user
   nArray(nCounter)=inputbox("Enter the Array Element #"&nCounter)
   'Validate the input (if it is a valid number)
   bFlag=False

   Do While bFlag=False

      If isNumeric(nArray(nCounter)) Then ' Checking if the input is a number

         bFlag=True

      Else

         bFlag=False
         nArray(nCounter)=inputbox("Enter a valid number")

      End If

   Loop

Next

For nCtr1=0 to cint(ubound(nArray)) 'To track the current elemtent

   For nCtr2 = nCtr1+1 to cint(ubound(nArray))  ' The other elements in the Array

      If cint(nArray(nCtr2))< cint(nArray(nCtr1)) Then ' Check which one is smaller

            'Swap the elements
            nTempVar = nArray(nCtr1)
            nArray(nCtr1)=nArray(nCtr2)
            nArray(nCtr2)=nTempVar

      End If

   Next

Next

sTempStr="Sorted list : "

For nCounter=0 to ubound(nArray)

   'Putting the sorted elements in a string
   sTempStr=sTempStr&nArray(nCounter)&", "

Next

Msgbox(sTempStr)

'#######################
'#### Sample Result  #####
'#######################
' Sorted list: -8, -2,0,1,4,4,7,
'##### End of Script######


Program Explained:

Lines 01-10 : Comments
Lines 11-14 : Delcarations
Lines 16-52 : Get the array boundary and Validate the input from the user. Read the article "Validating an input from the user" for more information
Lines 54-80 : Get the list of array elements
Lines 82-106: Sort the elements
Line 108 : Display the sorted elements

For more VBScript programs click here.

2 comments:

  1. Could u please send this with the html tags and scripts please this is my mail id (mokkamohan13@gmail.com)

    ReplyDelete
  2. A better version of simple program to sort array

    Dim narray(5)
    narray(0) = 1
    narray(1) = 8
    narray(2) = 3
    narray(3) = 10
    narray(4) = 2

    Dim i

    For i = 0 to ubound(narray)
    For j = i + 1 to ubound(narray)
    If narray(i) > narray(j) Then
    temp = narray(j)
    narray(j) = narray(i)
    narray(i) = temp
    End If
    Next
    Next

    For i = lbound(narray) to ubound(narray)
    msg = msg & narray(i)
    Next

    Msgbox msg

    ReplyDelete

Please leave your comment here...