C#几种常用的排序算法:
1 冒泡排序法
 1 #region 冒泡排序法
#region 冒泡排序法
2 public void Sort(int[] list)
public void Sort(int[] list)
3 {
{
4 long begintime = System.DateTime.Now.Second*1000+System.DateTime.Now.Millisecond;
    long begintime = System.DateTime.Now.Second*1000+System.DateTime.Now.Millisecond;
5 WriteLine(begintime);
    WriteLine(begintime);
6 int j,temp;
    int j,temp;
7 j= 1;
    j= 1;
8 while((j<list.Length))
    while((j<list.Length))
9 {
    {
10 for(int i=0;i<list.Length -j;i++)
        for(int i=0;i<list.Length -j;i++)
11 {
        {
12 if(list[i]<list[i+1])
            if(list[i]<list[i+1])
13 {
            {
14 temp = list[i];
                temp = list[i];
15 list[i] = list[i+1];
                list[i] = list[i+1];
16 list[i+1] = temp;
                list[i+1] = temp;
17 }
            }
18 }
        }
19 j++;
        j++;
20 }
    }
21 long endtime = System.DateTime.Now.Second*1000+System.DateTime.Now.Millisecond;
    long endtime = System.DateTime.Now.Second*1000+System.DateTime.Now.Millisecond;
22 WriteLine(endtime);
    WriteLine(endtime);
23 WriteLine(endtime-begintime);
    WriteLine(endtime-begintime);
24 }
}
25 #endregion
#endregion
 #region 冒泡排序法
#region 冒泡排序法2
 public void Sort(int[] list)
public void Sort(int[] list)3
 {
{4
 long begintime = System.DateTime.Now.Second*1000+System.DateTime.Now.Millisecond;
    long begintime = System.DateTime.Now.Second*1000+System.DateTime.Now.Millisecond;5
 WriteLine(begintime);
    WriteLine(begintime);6
 int j,temp;
    int j,temp;7
 j= 1;
    j= 1;8
 while((j<list.Length))
    while((j<list.Length))9
 {
    {10
 for(int i=0;i<list.Length -j;i++)
        for(int i=0;i<list.Length -j;i++)11
 {
        {12
 if(list[i]<list[i+1])
            if(list[i]<list[i+1])13
 {
            {14
 temp = list[i];
                temp = list[i];15
 list[i] = list[i+1];
                list[i] = list[i+1];16
 list[i+1] = temp;
                list[i+1] = temp;17
 }
            }18
 }
        }19
 j++;
        j++;20
 }
    }21
 long endtime = System.DateTime.Now.Second*1000+System.DateTime.Now.Millisecond;
    long endtime = System.DateTime.Now.Second*1000+System.DateTime.Now.Millisecond;22
 WriteLine(endtime);
    WriteLine(endtime);23
 WriteLine(endtime-begintime);
    WriteLine(endtime-begintime);24
 }
}25
 #endregion
#endregion
2 选择排序法
 1 #region 选择排序法
#region 选择排序法
2 public void SortChoice(int [] list)
public void SortChoice(int [] list)
3 {
{
4 long begintime = System.DateTime.Now.Millisecond;
    long begintime = System.DateTime.Now.Millisecond;
5 int min;
    int min;
6 for(int i=0;i<list.Length-1;i++)
    for(int i=0;i<list.Length-1;i++)
7 {
    {
8 min=i;
        min=i;
9 for(int j=i+1;j<list.Length;j++)
        for(int j=i+1;j<list.Length;j++)
10 {
        {
11 if(list[j]<list[min])
            if(list[j]<list[min])
12 min=j;
                min=j;
13 }
        }
14 int t=list[min];
        int t=list[min];
15 list[min]=list[i];
        list[min]=list[i];
16 list[i]=t;
        list[i]=t;
17 }
    }
18 long endtime = System.DateTime.Now.Millisecond;
    long endtime = System.DateTime.Now.Millisecond;
19 WriteLine(begintime);
    WriteLine(begintime);
20 WriteLine(endtime);
    WriteLine(endtime);
21 WriteLine(endtime-begintime);
    WriteLine(endtime-begintime);
22 }
}
23 #endregion
#endregion
 #region 选择排序法
#region 选择排序法2
 public void SortChoice(int [] list)
public void SortChoice(int [] list)3
 {
{4
 long begintime = System.DateTime.Now.Millisecond;
    long begintime = System.DateTime.Now.Millisecond;5
 int min;
    int min;6
 for(int i=0;i<list.Length-1;i++)
    for(int i=0;i<list.Length-1;i++)7
 {
    {8
 min=i;
        min=i;9
 for(int j=i+1;j<list.Length;j++)
        for(int j=i+1;j<list.Length;j++)10
 {
        {11
 if(list[j]<list[min])
            if(list[j]<list[min])12
 min=j;
                min=j;13
 }
        }14
 int t=list[min];
        int t=list[min];15
 list[min]=list[i];
        list[min]=list[i];16
 list[i]=t;
        list[i]=t;17
 }
    }18
 long endtime = System.DateTime.Now.Millisecond;
    long endtime = System.DateTime.Now.Millisecond;19
 WriteLine(begintime);
    WriteLine(begintime);20
 WriteLine(endtime);
    WriteLine(endtime);21
 WriteLine(endtime-begintime);
    WriteLine(endtime-begintime);22
 }
}23
 #endregion
#endregion
3 插入排序法
 1 #region 插入排序法
#region 插入排序法
2 public void SortInsert(int [] list)
public void SortInsert(int [] list)
3 {
{
4 for(int i=1;i<list.Length;i++)
    for(int i=1;i<list.Length;i++)
5 {
    {
6 int t=list[i];
        int t=list[i];
7 int j=i;
        int j=i;
8 while((j>0)&&(list[j-1]<t))
        while((j>0)&&(list[j-1]<t))
9 {
        {
10 list[j]=list[j-1];
            list[j]=list[j-1];
11 --j;
            --j;
12 }
        }
13 list[j]=t;
        list[j]=t;
14 }
    }
15 }
}
16 #endregion
#endregion
 #region 插入排序法
#region 插入排序法2
 public void SortInsert(int [] list)
public void SortInsert(int [] list)3
 {
{4
 for(int i=1;i<list.Length;i++)
    for(int i=1;i<list.Length;i++)5
 {
    {6
 int t=list[i];
        int t=list[i];7
 int j=i;
        int j=i;8
 while((j>0)&&(list[j-1]<t))
        while((j>0)&&(list[j-1]<t))9
 {
        {10
 list[j]=list[j-1];
            list[j]=list[j-1];11
 --j;
            --j;12
 }
        }13
 list[j]=t;
        list[j]=t;14
 }
    }15
 }
}16
 #endregion
#endregion
4 希尔排序法
 1 #region 希尔排序法
#region 希尔排序法
2 public void SortShell(int [] list)
public void SortShell(int [] list)
3 {
{
4 int inc;
    int inc;
5 for(inc=1;inc<=list.Length/9;inc=3*inc+1);
    for(inc=1;inc<=list.Length/9;inc=3*inc+1);
6 for(;inc>0;inc/=3)
    for(;inc>0;inc/=3)
7 {
    {
8 for(int i=inc+1;i<=list.Length;i+=inc)
        for(int i=inc+1;i<=list.Length;i+=inc)
9 {
        {
10 int t=list[i-1];
            int t=list[i-1];
11 int j=i;
            int j=i;
12 while((j>inc)&&(list[j-inc-1]>t))
            while((j>inc)&&(list[j-inc-1]>t))
13 {
            {
14 list[j-1]=list[j-inc-1];
                list[j-1]=list[j-inc-1];
15 j-=inc;
                j-=inc;
16 }
            }
17 list[j-1]=t;
            list[j-1]=t;
18 }
        }
19 }
    }
20 }
}
21 #endregion
#endregion
 #region 希尔排序法
#region 希尔排序法2
 public void SortShell(int [] list)
public void SortShell(int [] list)3
 {
{4
 int inc;
    int inc;5
 for(inc=1;inc<=list.Length/9;inc=3*inc+1);
    for(inc=1;inc<=list.Length/9;inc=3*inc+1);6
 for(;inc>0;inc/=3)
    for(;inc>0;inc/=3)7
 {
    {8
 for(int i=inc+1;i<=list.Length;i+=inc)
        for(int i=inc+1;i<=list.Length;i+=inc)9
 {
        {10
 int t=list[i-1];
            int t=list[i-1];11
 int j=i;
            int j=i;12
 while((j>inc)&&(list[j-inc-1]>t))
            while((j>inc)&&(list[j-inc-1]>t))13
 {
            {14
 list[j-1]=list[j-inc-1];
                list[j-1]=list[j-inc-1];15
 j-=inc;
                j-=inc;16
 }
            }17
 list[j-1]=t;
            list[j-1]=t;18
 }
        }19
 }
    }20
 }
}21
 #endregion
#endregion 
                    
                     
                    
                 
                    
                 

 
        


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