c#实现的3种排序算法
 1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4 namespace ConsoleApplication1
namespace ConsoleApplication1
5 {
{
6 class Program
    class Program
7 {
    {
8 static void Main(string[] args)
        static void Main(string[] args)
9 {
        {
10 int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};
            int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};
11 //mpsort(iarrary);
           //mpsort(iarrary);
12 // xzsort(iarrary);
           // xzsort(iarrary);
13 crsort(iarrary);
            crsort(iarrary); 
14 for (int n = 0; n < iarrary.Length; n++)
            for (int n = 0; n < iarrary.Length; n++)
15 {
            {
16 Console.WriteLine("{0}",iarrary[n]);
                Console.WriteLine("{0}",iarrary[n]);
17 }
            }
18 Console.Read();
            Console.Read();
19 }
        }
20 public static void mpsort(int [] list)//冒泡排序
        public static void mpsort(int [] list)//冒泡排序
21 {
        {
22 int i, j, temp;
            int i, j, temp;
23 bool exchange;
            bool exchange;
24 for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
            for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
25 {
            {
26 exchange = false;
                exchange = false;
27 for (j = 0; j < list.Length-i;j++ )
                for (j = 0; j < list.Length-i;j++ )
28 {
                {
29 if(list[j]>list[j+1])
                    if(list[j]>list[j+1])
30 {
                    {
31 temp = list[j];
                        temp = list[j];
32 list[j] = list[j + 1];
                        list[j] = list[j + 1];
33 list[j + 1] = temp;
                        list[j + 1] = temp;
34 exchange = true;
                        exchange = true;
35 }
                    }
36 }
                }
37 if (!exchange)
                if (!exchange)
38 {
                {
39 break;
                    break;
40 }
                }
41 }
            }
42 }
        }
43 public static void xzsort(int[] list)//选择排序
        public static void xzsort(int[] list)//选择排序
44 {
        {
45 int i,j,temp,min;
            int i,j,temp,min;
46 for (i = 0; i < list.Length; i++)
            for (i = 0; i < list.Length; i++)
47 {
            {
48 min = i;
                min = i;
49 for (j = i + 1; j < list.Length;j++ )
                for (j = i + 1; j < list.Length;j++ )
50 {
                {
51 if (list[min] > list[j])
                    if (list[min] > list[j])
52 {
                    {
53 min = j;
                        min = j;
54 }
                    }
55 }
                }
56 temp = list[min];
                temp = list[min];
57 list[min] = list[i];
                list[min] = list[i];
58 list[i] = temp;
                list[i] = temp;
59 }
            }
60 }
        }
61 public static void crsort(int[] list)//插入排序
        public static void crsort(int[] list)//插入排序
62 {
        { 
63 int i,j,temp;
            int i,j,temp;
64 for (i = 1; i < list.Length; i++)
            for (i = 1; i < list.Length; i++)
65 {
            {
66 temp = list[i];
                temp = list[i];
67 j = i;
                j = i;
68 while((j > 0)&&(list[j-1]>temp))
                while((j > 0)&&(list[j-1]>temp))
69 {
                {
70 list[j]=list[j-1];
                    list[j]=list[j-1];
71 --j;
                    --j;
72 }
                } 
73 list[j]=temp;
                list[j]=temp;
74 }
            }
75 }
        }
76 
      
77 }
    }
78 }
}
79
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4
 namespace ConsoleApplication1
namespace ConsoleApplication15
 {
{6
 class Program
    class Program7
 {
    {8
 static void Main(string[] args)
        static void Main(string[] args)9
 {
        {10
 int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};
            int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};11
 //mpsort(iarrary);
           //mpsort(iarrary);12
 // xzsort(iarrary);
           // xzsort(iarrary);13
 crsort(iarrary);
            crsort(iarrary); 14
 for (int n = 0; n < iarrary.Length; n++)
            for (int n = 0; n < iarrary.Length; n++)15
 {
            {16
 Console.WriteLine("{0}",iarrary[n]);
                Console.WriteLine("{0}",iarrary[n]);17
 }
            }18
 Console.Read();
            Console.Read();19
 }
        }20
 public static void mpsort(int [] list)//冒泡排序
        public static void mpsort(int [] list)//冒泡排序21
 {
        {22
 int i, j, temp;
            int i, j, temp;23
 bool exchange;
            bool exchange;24
 for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
            for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序25
 {
            {26
 exchange = false;
                exchange = false;27
 for (j = 0; j < list.Length-i;j++ )
                for (j = 0; j < list.Length-i;j++ )28
 {
                {29
 if(list[j]>list[j+1])
                    if(list[j]>list[j+1])30
 {
                    {31
 temp = list[j];
                        temp = list[j];32
 list[j] = list[j + 1];
                        list[j] = list[j + 1];33
 list[j + 1] = temp;
                        list[j + 1] = temp;34
 exchange = true;
                        exchange = true;35
 }
                    }36
 }
                }37
 if (!exchange)
                if (!exchange)38
 {
                {39
 break;
                    break;40
 }
                }41
 }
            }42
 }
        }43
 public static void xzsort(int[] list)//选择排序
        public static void xzsort(int[] list)//选择排序44
 {
        {45
 int i,j,temp,min;
            int i,j,temp,min;46
 for (i = 0; i < list.Length; i++)
            for (i = 0; i < list.Length; i++)47
 {
            {48
 min = i;
                min = i;49
 for (j = i + 1; j < list.Length;j++ )
                for (j = i + 1; j < list.Length;j++ )50
 {
                {51
 if (list[min] > list[j])
                    if (list[min] > list[j])52
 {
                    {53
 min = j;
                        min = j;54
 }
                    }55
 }
                }56
 temp = list[min];
                temp = list[min];57
 list[min] = list[i];
                list[min] = list[i];58
 list[i] = temp;
                list[i] = temp;59
 }
            }60
 }
        }61
 public static void crsort(int[] list)//插入排序
        public static void crsort(int[] list)//插入排序62
 {
        { 63
 int i,j,temp;
            int i,j,temp;64
 for (i = 1; i < list.Length; i++)
            for (i = 1; i < list.Length; i++)65
 {
            {66
 temp = list[i];
                temp = list[i];67
 j = i;
                j = i;68
 while((j > 0)&&(list[j-1]>temp))
                while((j > 0)&&(list[j-1]>temp))69
 {
                {70
 list[j]=list[j-1];
                    list[j]=list[j-1];71
 --j;
                    --j;72
 }
                } 73
 list[j]=temp;
                list[j]=temp;74
 }
            }75
 }
        }76
 
      77
 }
    }78
 }
}79

 
                    
                     
                    
                 
                    
                 


 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号