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
 

How to do sorting using “Insertion Sort” algorithm in C# (.net 2.0)

Author: Zunnair
Download Source Code : 855_sort.zip

In this simple article you will learn how to do sorting using insertion sort in VB and C# .net 2.0.

 

Insertion sort algorithm uses method called insertion. By using this algorithm you can sort your array or collection according to a specified key.
How Insertion sort works:
Insertion sort explains its method by its name. This sorting algorithm uses insertion technique. It takes one element and compares it with each element of array, checks the greater and lesser elements of array than put the element on its place. Figure will clear this algorithm better.
Suppose that the array contains elements as shown in the figure (a)

2
8
9
15
 

New element is 3. The actual place of the 3 in array will be between 2 and 8. Because 3 is lesser than 8 and greater than 2.
So

2
3
8
9
15

For each element or iteration insertion sort starts checking from top to bottom and insert the new element at its place.
Complexity of Insertion Sort Algorithm:
This algorithm takes N square time in its worst and average case. So we can say that its complexity is BIG O of n square.
To demonstrate this make a window form based application. Drag one button and one list box on the form. Load an integer array in the list box and than press button to sort.
Now write the following code on Button Click event:
C#
private void btn_Sort_Click(object sender, EventArgs e)
        {
            InsertionSort();
            listBox_elements.Items.Clear();
            for (int x = 0; x < arr.Length; x++)
                listBox_elements.Items.Insert(x, arr.GetValue(x));
        }
VB
Private Sub btn_Sort_Click(ByVal sender As Object, ByVal e As EventArgs)
        InsertionSort()
        listBox_elements.Items.Clear()
        For x As Integer = 0 To arr.Length - 1
            listBox_elements.Items.Insert(x, arr.GetValue(x))
        Next
    End Sub
This is a simple code to call Insertion Sort () Function. After sorting, this code will load the sorted array in list box.
Now write the following code in Insertion Sort () Function:
C#
private void InsertionSort()
        {
            int temp = 0;
            int j=0,k=0;
            for (j = 1; j <=(arr.Length)-1; j++)
            {
                temp = (int)arr.GetValue(j);
                 k= j - 1;
                while(k>=0&& ((int)arr.GetValue(k))>temp)
                {
                    arr.SetValue(arr.GetValue(k), k + 1);
                    k = k - 1;
                }
                arr.SetValue(temp, k + 1);
            }
        }
VB
Private Sub InsertionSort()
        Dim temp As Integer = 0
        Dim j As Integer = 0, k As Integer = 0
        For j = 1 To (arr.Length) - 1
            temp = CInt(arr.GetValue(j))
            k = j - 1
            While k >= 0 AndAlso CInt(arr.GetValue(k)) > temp
                arr.SetValue(arr.GetValue(k), k + 1)
                k = k - 1
            End While
            arr.SetValue(temp, k + 1)
        Next
    End Sub
.
Now write the following code on FORM LOAD event:
C#
  private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "DEVASP INSERTION SORT APPLICATION";
            arr.SetValue(8, 0);
            arr.SetValue(9,1);
            arr.SetValue(2, 2);
            arr.SetValue(4, 3);
            arr.SetValue(7, 4);
            for (int i = 0; i < arr.Length; i++)
                listBox_elements.Items.Insert(i, arr.GetValue(i));          
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "DEVASP INSERTION SORT APPLICATION"
        arr.SetValue(8, 0)
        arr.SetValue(9, 1)
        arr.SetValue(2, 2)
        arr.SetValue(4, 3)
        arr.SetValue(7, 4)
        For i As Integer = 0 To arr.Length - 1
            listBox_elements.Items.Insert(i, arr.GetValue(i))
        Next
    End Sub
This simple article tells some how to do sorting using INSERTION SORT in VB and C# .net 2.0.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to do Sorting using Gnome Sort algorithm in C# (.net 2.0)

Disclaimer - Privacy
© 2002-2017 DevASP.net