C# Code to implement Optimized Bubble Sort

Kundnani.Rt
Posted by Kundnani.Rt under C# category on | Points: 40 | Views : 1824
The following code demonstrates implementation of Optimized Bubble Sort:

static void OptimizedBubbleSort(IComparable[] array)
{
int i, j;
IComparable temp;
bool flag = false;
for (i = array.Length - 1; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (array[j].CompareTo(array[j + 1]) > 0)
{
flag = true;
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
if (flag == false)
break;
}

for (i = 0; i < array.Length; i++)
Console.Write(array[i]+"\t");
}

Comments or Responses

Login to post response