# C#常用排序算法

575人阅读 评论(0)

 ///         /// 冒泡排序         ///         /// 进行排序的整型数组         public static void BubbleSorter(int[] list)         {             int i, j, temp;             bool done = false;//判断是否已经排序过             j = 1;             while ((j < list.Length) && (!done))             {                 done = true;                 for (i = 0; i < list.Length - j;i++)                 {                     if (list[i] > list[i + 1])                     {                         done = false;                         //交换位置                         temp = list[i];                         list[i] = list[i + 1];                         list[i + 1] = temp;                     }                 }                 j++;             }         }

 ///         /// 选者最小值排序法         ///         /// 进行排序的整型数组         public static void SelectMinSorter(int[] list)         {             int min, temp;             for (int i = 0; i < list.Length; i++)             {                 min = i;                 for (int j = i + 1; j < list.Length; j++)                 {                     if (list[j] < list[min])                         min = j;                 }                 temp = list[min];                 list[min] = list[i];                 list[i] = temp;             }         }           ///         /// 选者最大值排序法         ///         /// 进行排序的整型数组         public static void SelectMaxSorter(int[] list)         {             int max, temp;             for (int i = 0; i < list.Length; i++)             {                 max = i;                 for (int j = i + 1; j < list.Length; j++)                 {                     if (list[j] > list[max])                         max = j;                 }                 temp = list[max];                 list[max] = list[i];                 list[i] = temp;             }         }

 ///         /// 插入排序         ///         /// 进行排序的整型数组         public static void InsertSorter(int[] list)         {             int j,temp;             for (int i = 1; i < list.Length; i++)             {                 j =  i;                 temp = list[i];                 while ((j > 0) && (list[j - 1] > temp))                 {                     list[j] = list[j - 1];                     --j;                 }                 list[j] = temp;             }         }

 ///         /// 希尔排序         ///         /// 进行排序的整型数组         public static void ShellSorter(int[] list)         {             int inc;             for (inc = 1; inc < list.Length / 9; inc = 3 * inc + 1) ;             for (; inc > 0; inc /= 3)             {                 for (int i = inc + 1; i <= list.Length; i += inc)                 {                     int temp = list[i - 1];                     int j = i;                     while ((j > inc) && (list[j - inc - 1] > temp))                     {                         list[j - 1] = list[j - inc - 1];                         j -= inc;                     }                     list[j - 1] = temp;                 }             }         }

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：129080次
• 积分：2145
• 等级：
• 排名：第17707名
• 原创：84篇
• 转载：5篇
• 译文：0篇
• 评论：26条
文章分类
评论排行