当前位置: 移动技术网 > IT编程>开发语言>c# > C#冒泡法排序算法实例分析

C#冒泡法排序算法实例分析

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#冒泡法排序算法。分享给大家供大家参考。具体实现方法如下:

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; 
      } 
    } 
  } 
}

泛型版本:

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; 
      } 
    } 
  } 
} 

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网