Example Analysis of C Bubble Sorting Algorithm
- 2021-07-09 09:05:51
- OfStack
In this paper, the sorting algorithm of C # bubbling method is described with examples. Share it for your reference. The specific implementation method is as follows:
static void BubbleSort(IComparable[] array)
{
int i, j;
IComparable temp;
for (i = array.Length - 1; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (array[j].CompareTo(array[j + 1]) > 0)
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
Generic version:
static void BubbleSort<T>(IList<T> list) where T : IComparable<T>
{
for (int i = list.Count - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
IComparable current = list[j];
IComparable next = list[j + 1];
if (current.CompareTo(next) > 0)
{
list[j] = next;
list[j + 1] = current;
}
}
}
}
I hope this article is helpful to everyone's C # programming.