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

Author: Zunnair
Download Source Code : 854_sort.zip

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

 

How Gnome sort works:
This algorithm moves its array pointer in two ways. There are conditions that if the pointer is at start it can not go back and if pointer is at end it means array has sorted. This algorithm compares the element if one is lesser than other it swaps the elements and move backward until the no swapping is done. When swapping it is moving back and no swapping done than it again moves forward and pointer reaches at end the array is sorted.
Complexity of Gnome 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)
        {
            GenomeSort();
            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)
        GenomeSort()
        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 Genome 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 GnomeSort()
        {
            int i = 0;
            int temp;
 
            while (i < arr.Length)
            {
                if (i == 0 || ((int)arr.GetValue(i-1)) <= ((int)arr.GetValue(i)))
                    i++;
 
                else
                {
                    temp = (int)arr.GetValue(i);
                    arr.SetValue(arr.GetValue(i - 1), i);
                    arr.SetValue(temp,--i);
                }
 
            }
        }
VB
Private Sub GnomeSort()
        Dim i As Integer = 0
        Dim temp As Integer
 
        While i < arr.Length
            If i = 0 OrElse CInt(arr.GetValue(i - 1)) <= CInt(arr.GetValue(i)) Then
                i += 1
            Else
 
                temp = CInt(arr.GetValue(i))
                arr.SetValue(arr.GetValue(i - 1), i)
                arr.SetValue(temp, System.Threading.Interlocked.Decrement(i))
 
            End If
        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 GNOME SORT APPLICATION";
 
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "DEVASP GNOME 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 GNOME sort in VB and C# .net 2.0.
 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Sort And Binary Search in array using “ARRAY CLASS” in VB and C# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net