直接选择排序

直接选择排序,算法时间复杂度也是N^2, 其思想和冒泡有点相似,但是区别在于冒泡找到就交换,直接选择排序先找到位置最后在交换。
 1 class  Program
 2 ExpandedBlockStart.gifContractedBlock.gif     {
 3        static void Main(string[] args)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif            int[] a = 72162113843326 };
 6            PrintArray(a);
 7            SelectSort(a);
 8            PrintArray(a);
 9
10        }

11
12        private static void SelectSort(int[] array)
13ExpandedSubBlockStart.gifContractedSubBlock.gif        {
14            int k = 0;
15            for (int i = 0; i < array.Length; i++)
16ExpandedSubBlockStart.gifContractedSubBlock.gif            {
17                k = i;
18                for (int j = i + 1; j < array.Length; j++)
19ExpandedSubBlockStart.gifContractedSubBlock.gif                {
20                    if (array[k] > array[j])
21ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
22                        k = j;
23                    }

24                }

25                if (i!=k)
26ExpandedSubBlockStart.gifContractedSubBlock.gif                {
27                    int temp = array[i];
28                    array[i] = array[k];
29                    array[k] = temp;
30                }

31            }

32        }

33
34        private static void PrintArray(int[] array)
35ExpandedSubBlockStart.gifContractedSubBlock.gif        {
36            string result = string.Empty;
37            for (int i = 0; i < array.Length; i++)
38ExpandedSubBlockStart.gifContractedSubBlock.gif            {
39                result += array[i].ToString() + " ";
40            }

41            Console.WriteLine(result.Trim());
42        }

43    }

转载于:https://www.cnblogs.com/CharlesLiu/archive/2009/09/16/1567965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值