Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Sort And Binary Search in array using “ARRAY CLASS” in VB and C# (.net 2.0).

Author: Zunnair
Download Source Code : 853_array.zip

: In this simple article you will learn how to sort an array and Binary Search in an array using “ARRAY CLASS” in VB and C# .net 2.0.

 

Binary Search:
Binary search is fast algorithm to search in an array. Array class provides a method named Binary Search () to search in array.
This function has eight overloaded methods.
In first method Binary Search () function takes array class object and value, which user want to search, as a parameter. It returns index value.
To demonstrate make a window application. Drag one text box and one button on the form. Enter value in the text box and press button.
Now write the following code on button click event:
 C#
private void btn_binary_Click(object sender, EventArgs e)
        {
            arr.SetValue(1, 0);
            arr.SetValue(2, 1);
            arr.SetValue(3, 2);
            arr.SetValue(4, 3);
            arr.SetValue(5, 4);
            string str = txt_binary.Text;
            int a = 0;
            a = Convert.ToInt16(str);
            int b = Array.BinarySearch(arr, a);
            if (b == -1)
            {
                MessageBox.Show("value not exist in array");
            }
            else
            {
                MessageBox.Show(b.ToString());
            }
        }
VB                          
Private Sub btn_binary_Click(ByVal sender As Object, ByVal e As EventArgs)
        arr.SetValue(1, 0)
        arr.SetValue(2, 1)
        arr.SetValue(3, 2)
        arr.SetValue(4, 3)
        arr.SetValue(5, 4)
        Dim str As String = txt_binary.Text
        Dim a As Integer = 0
        a = Convert.ToInt16(str)
        Dim b As Integer = Array.BinarySearch(arr, a)
        If b = -1 Then
            MessageBox.Show("value not exist in array")
        Else
            MessageBox.Show(b.ToString())
        End If
    End Sub
Other overloaded method of Binary Search () function takes object of array, an index, length of array and value which user want to search. This function searches from specific index to the end.
To demonstrate make a window application. Drag three text boxes and one button on the form. Enter value, index and length in the text boxes and press button.
Now write the following code on button click event:
 C#
private void btn_bin_Click(object sender, EventArgs e)
        {
            arr.SetValue(1, 0);
            arr.SetValue(2, 1);
            arr.SetValue(3, 2);
            arr.SetValue(4, 3);
            arr.SetValue(5, 4);
            string str = txt_bvalue.Text;
            string str1 = txt_bindex.Text;
            string str2 = txt_blength.Text;
            int a = 0,b=0,c=0,ans=0;
            a = Convert.ToInt16(str);
            b = Convert.ToInt16(str1);
            c = Convert.ToInt16(str2);
 
            ans = Array.BinarySearch(arr,b,c,a);
            if (ans == -1)
            {
                MessageBox.Show("value not exist in array");
            }
            else
            {
                MessageBox.Show(ans.ToString());
            }
        }
VB
Private Sub btn_bin_Click(ByVal sender As Object, ByVal e As EventArgs)
        arr.SetValue(1, 0)
        arr.SetValue(2, 1)
        arr.SetValue(3, 2)
        arr.SetValue(4, 3)
        arr.SetValue(5, 4)
        Dim str As String = txt_bvalue.Text
        Dim str1 As String = txt_bindex.Text
        Dim str2 As String = txt_blength.Text
        Dim a As Integer = 0, b As Integer = 0, c As Integer = 0, ans As Integer = 0
        a = Convert.ToInt16(str)
        b = Convert.ToInt16(str1)
        c = Convert.ToInt16(str2)
 
        ans = Array.BinarySearch(arr, b, c, a)
        If ans = -1 Then
            MessageBox.Show("value not exist in array")
        Else
            MessageBox.Show(ans.ToString())
        End If
    End Sub
This simple code will search in the array.
 
Sorting:
Array class provides us a facility to sort the array. There are 17 overloaded method of this function.
To demonstrate make a window application. Drag one button on the form and press button.
Now write the following code on button click event:
C#
private void btn_sort_Click(object sender, EventArgs e)
        {
            arr.SetValue(1, 0);
            arr.SetValue(2, 1);
            arr.SetValue(3, 2);
            arr.SetValue(4, 3);
            arr.SetValue(5, 4);
            Array.Sort(arr);
            MessageBox.Show("array sorted");
        }
VB
Private Sub btn_sort_Click(ByVal sender As Object, ByVal e As EventArgs)
        arr.SetValue(1, 0)
        arr.SetValue(2, 1)
        arr.SetValue(3, 2)
        arr.SetValue(4, 3)
        arr.SetValue(5, 4)
        Array.Sort(arr)
        MessageBox.Show("array sorted")
    End Sub

this is the simple code to sort the array.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
 
            this.Text = "Devasp.net ARRAY Application";
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
 
        Me.Text = "Devasp.net ARRAY Application"
 End Sub
 

This simple article tells Binary Search and Sort in array using “ARRAY CLASS” in VB and C# .net 2.0

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to do Sorting using Bubble Sort algorithm in C# (.net 2.0)

Disclaimer - Privacy
© 2002-2017 DevASP.net