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 Shell Sort algorithm in C# (.net 2.0)

Author: Zunnair
Download Source Code : 858_sort.zip

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

 

Shell sort is improvement of bubble and insertion sort. It is one of the fastest sorting algorithms but it is not useful for large number of data. But it can perform well in small number of data. Shell sort use insertion technique.
How Shell sort works:
Shell sort divides the array in chunks and sort that chunks and again made the array. It can start from five elements chunk than sort every chunk than divides in 3 element chunks and sort them until the number of element in each chunk does not become one. When it becomes one it makes the array by each chunk, at this time array is sorted.
Complexity of Shell Sort Algorithm:
Its complexity belongs from exponential class. It is a fast algorithm. Its complexity is
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)
        {
            ShellSort();
            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)
        ShellSort()
        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 Shell Sort () Function. After sorting, this code will load the sorted array in list box.
Now write the following code in Bubble Sort () Function:
C#
private void ShellSort()
        {
            int stop, swap, limit, temp;
            int i = (int)(arr.Length / 2);
            while (i > 0)
            {
                stop = 0;
                limit = arr.Length - i;
                while (stop == 0)
                {
                    swap = 0;
                    for (int k = 0; k
                    {
                        if ((int)arr.GetValue(k) > (int)arr.GetValue(k+i))
                        {
                            temp = (int)arr.GetValue(k);
                            arr.SetValue(arr.GetValue(k + i), k); ;
                            arr.SetValue(temp,k+i);
                            swap = k;
                        }
                   }
                    limit = swap - i;
                    if (swap == 0)
                        stop = 1;
                }
                i = (int)(i / 2);
            }
        }
VB
Private Sub ShellSort()
        Dim [stop] As Integer, swap As Integer, limit As Integer, temp As Integer
        Dim i As Integer = CInt((arr.Length / 2))
        While i > 0
            [stop] = 0
            limit = arr.Length - i
            While [stop] = 0
                swap = 0
                For k As Integer = 0 To limit - 1
                    If CInt(arr.GetValue(k)) > CInt(arr.GetValue(k + i)) Then
                        temp = CInt(arr.GetValue(k))
                        arr.SetValue(arr.GetValue(k + i), k)
 
                        arr.SetValue(temp, k + i)
                        swap = k
                    End If
                Next
                limit = swap - i
                If swap = 0 Then
                    [stop] = 1
                End If
            End While
            i = CInt((i / 2))
        End While
    End Sub
This is simple code to sort.
Now write the following code on FORM LOAD event:
C#
 private void Form1_Load(object sender, EventArgs e)
        {                 
            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));
            this.Text = "DEVASP SHELL SORT APPLICATION";
 
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "DEVASP SHELL 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 Shell sort in VB and C# .net 2.0.
 
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to do Sorting using “Cocktail Sort” algorithm in C# (.net 2.0)

Disclaimer - Privacy
© 2002-2017 DevASP.net