c#实现的3种排序算法

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif namespace  ConsoleApplication1
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    class Program
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        static void Main(string[] args)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10ExpandedSubBlockStart.gifContractedSubBlock.gif            int[] iarrary = new int[] dot.gif{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
11InBlock.gif           //mpsort(iarrary);
12InBlock.gif           // xzsort(iarrary);
13InBlock.gif            crsort(iarrary); 
14InBlock.gif            for (int n = 0; n < iarrary.Length; n++)
15ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
16InBlock.gif                Console.WriteLine("{0}",iarrary[n]);
17ExpandedSubBlockEnd.gif            }

18InBlock.gif            Console.Read();
19ExpandedSubBlockEnd.gif        }

20InBlock.gif        public static void mpsort(int [] list)//冒泡排序
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            int i, j, temp;
23InBlock.gif            bool exchange;
24InBlock.gif            for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                exchange = false;
27InBlock.gif                for (j = 0; j < list.Length-i;j++ )
28ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
29InBlock.gif                    if(list[j]>list[j+1])
30ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
31InBlock.gif                        temp = list[j];
32InBlock.gif                        list[j] = list[j + 1];
33InBlock.gif                        list[j + 1= temp;
34InBlock.gif                        exchange = true;
35ExpandedSubBlockEnd.gif                    }

36ExpandedSubBlockEnd.gif                }

37InBlock.gif                if (!exchange)
38ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
39InBlock.gif                    break;
40ExpandedSubBlockEnd.gif                }

41ExpandedSubBlockEnd.gif            }

42ExpandedSubBlockEnd.gif        }

43InBlock.gif        public static void xzsort(int[] list)//选择排序
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif            int i,j,temp,min;
46InBlock.gif            for (i = 0; i < list.Length; i++)
47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
48InBlock.gif                min = i;
49InBlock.gif                for (j = i + 1; j < list.Length;j++ )
50ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
51InBlock.gif                    if (list[min] > list[j])
52ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
53InBlock.gif                        min = j;
54ExpandedSubBlockEnd.gif                    }

55ExpandedSubBlockEnd.gif                }

56InBlock.gif                temp = list[min];
57InBlock.gif                list[min] = list[i];
58InBlock.gif                list[i] = temp;
59ExpandedSubBlockEnd.gif            }

60ExpandedSubBlockEnd.gif        }

61InBlock.gif        public static void crsort(int[] list)//插入排序
62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
63InBlock.gif            int i,j,temp;
64InBlock.gif            for (i = 1; i < list.Length; i++)
65ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
66InBlock.gif                temp = list[i];
67InBlock.gif                j = i;
68InBlock.gif                while((j > 0)&&(list[j-1]>temp))
69ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
70InBlock.gif                    list[j]=list[j-1];
71InBlock.gif                    --j;
72ExpandedSubBlockEnd.gif                }
 
73InBlock.gif                list[j]=temp;
74ExpandedSubBlockEnd.gif            }

75ExpandedSubBlockEnd.gif        }

76InBlock.gif      
77ExpandedSubBlockEnd.gif    }

78ExpandedBlockEnd.gif}

79 None.gif

转载于:https://www.cnblogs.com/hymxtang/archive/2007/08/07/846361.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值