Array, ReDim, Sorting array values


Array

An Array variable to hold multiple values, 

Instead of storing multiple values in multiple variables which are stored in different locations in memory,

The values can be stored in the array, which are stored contigiously one after the other and effectively use the memory

Declare Array
Dim x(3)
       x is array to store 4 values,,, as array index starts from 0 so 0 -3 total 4 values can be stored
       The values in array are refered based on index
        Like  x(0)     x(1)         x(2)………..etc

Declare dyanamic size of array use ReDim

When redefine the size of array, the existing values in array are deleted so use “preserve” when redefine

Like ReDim preserve x(5)
Below are the programs in VBScript to sort values in array

Dim a,flag   '''''''''''flag is used to check the status, if interchaged then update flag=true to that it start again from beginning

a=Array(2,3,1,5,8)
Do
   flag=false
   For i=0 to ubound(a)-1
     If a(i)>a(i+1) Then
         c=a(i)
         a(i)=a(i+1)
         a(i+1)=c
         flag=true
    End If
Next
loop while flag
For j=0 to ubound(a)
    print a(j)
Next

--------------------------------------------------------------------------------------------------------------
'''''''''' selection
 sort'''''''''''''  This is more performance than bubble soft

dim a
a=
Array(4,2,6,8,1)
For i=0 to ubound(a)-1
 min=i
 For j=i+1 to ubound(a)
       If a(j)            min=j
       End If
 Next
If i<>min Then
      s=a(i)
      a(i)=a(min)
      a(min)=s
End If
Next
For each i in a
     print i
Next

Comments