选择排序

 1         /// <summary>
 2         /// 选择排序
 3         /// 原理:每次找出最小的元素排在最前面
 4         /// </summary>
 5         /// <param name="arr"></param>
 6         /// <returns></returns>
 7         public static int[] SelectionSort(int[] ary)
 8         {
 9             int m = 0;
10             for (int i = 0; i < ary.Length; i++)
11             {
12                 for (int j = 1; j < ary.Length - i; j++)
13                 {
14                     if (ary[m] < ary[j])
15                     {
16                         m = j;
17                     }
18                 }
19                 int k = ary.Length - i - 1;
20                 if (m != k)
21                 {
22                     int t = ary[m];
23                     ary[m] = ary[k];
24                     ary[k] = t;
25                 }
26                 m = 0;
27             }
28             return ary;
29         }

 

posted @ 2012-12-27 17:51  宁静.致远  阅读(145)  评论(0编辑  收藏  举报