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

Author: Zunnair
Download Source Code : 857_sort.zip

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

 

Cocktail sort belongs to exchanging element category of sorting algorithms.
How Cocktail sort works:
This algorithm uses bubble sort technique but in both way. It compares elements and shift lighter on top and heavier at bottom. By using this algorithm you can sort your array or collection according to a specified key.
Complexity of Cocktail Sort Algorithm:
This algorithm takes N square time in its worst case. So we can say that its complexity is BIG O of n square. Its complexity is same as bubble sort but in some cases it can perform faster.
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)
        {
            CockTailSort();
            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)
        CockTailSort()
        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 Cocktail Sort () Function. After sorting, this code will load the sorted array in list box.
Now write the following code in Cocktail Sort () Function:
C#
private void CockTailSort()
        {
            int temp = 0;
            int exchange = 0;
            int j=0,k=0;
            do
            {
            exchange=0;
                  for(j=(arr.Length)-1;j>0;--j)
                 {
                if((int)arr.GetValue(j-1) > (int)arr.GetValue(j)) {
                temp = (int)arr.GetValue(j-1);
                arr.SetValue(arr.GetValue(j),j-1);
                arr.SetValue(temp,j);
                exchange = 1;
                     }
                  }
         
             for (j = 1; j < arr.Length; ++j)
                  {
                        if ((int)arr.GetValue(j - 1) > (int)arr.GetValue(j))
                        {
                        temp = (int)arr.GetValue(j - 1);
                        arr.SetValue(arr.GetValue(j), j - 1);
                        arr.SetValue(temp, j);
                         exchange = 1;
                        }
                   }
           
            } while(Convert.ToBoolean(exchange));
        }
VB
Private Sub CockTailSort()
        Dim temp As Integer = 0
        Dim exchange As Integer = 0
        Dim j As Integer = 0, k As Integer = 0
        Do
            exchange = 0
            For j = (arr.Length) - 1 To 1 Step -1
                If CInt(arr.GetValue(j - 1)) > CInt(arr.GetValue(j)) Then
                    temp = CInt(arr.GetValue(j - 1))
                    arr.SetValue(arr.GetValue(j), j - 1)
                    arr.SetValue(temp, j)
                    exchange = 1
                End If
            Next
 
            For j = 1 To arr.Length - 1
                If CInt(arr.GetValue(j - 1)) > CInt(arr.GetValue(j)) Then
                    temp = CInt(arr.GetValue(j - 1))
                    arr.SetValue(arr.GetValue(j), j - 1)
                    arr.SetValue(temp, j)
                    exchange = 1
                End If
 
            Next
        Loop While Convert.ToBoolean(exchange)
    End Sub
.
Now write the following code on FORM LOAD event:
C#
 private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "DEVASP COCKTAIL 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 COCKTAIL 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 Cocktail sort in VB and C# .net 2.0.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Array operations (Reverse, Clear Values, Get Values) using (Static Functions) “ARRAY CLASS” in VB and C# (.net 2.0) (Part 3).

Disclaimer - Privacy
© 2002-2017 DevASP.net