SoFunction
Updated on 2025-03-06

C# bubble method sorting algorithm example analysis

This article describes the C# bubble sorting algorithm. Share it for your reference. The specific implementation method is as follows:

static void BubbleSort(IComparable[] array) 
{ 
  int i, j; 
  IComparable temp; 
  for (i =  - 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 =  - 1; i > 0; i--) 
  { 
    for (int j = 0; j < i; j++) 
    { 
      IComparable current = list[j]; 
      IComparable next = list[j + 1]; 
      if ((next) > 0) 
      { 
        list[j] = next; 
        list[j + 1] = current; 
      } 
    } 
  } 
} 

I hope this article will be helpful to everyone's C# programming.