C#排序演算法

來源:互聯網
上載者:User
 

C#演算法--(一)選擇排序

using System;
public class SelectionSorter
{
// public enum comp {COMP_LESS,COMP_EQUAL,COMP_GRTR};
private int min;
// private int m=0;
public void Sort(int [] list)
{
for(int i=0;i

{
min=i;
for(int j=i+1;j

{
if(list[j]

min=j;
}
int t=list[min];
list[min]=list[i];
list[i]=t;
// Console.WriteLine("{0}",list[i]);
}

}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
SelectionSorter ss=new SelectionSorter();
ss.Sort(iArrary);
for(int m=0;m<=13;m++)
Console.WriteLine("{0}",iArrary[m]);

}
}
C#演算法--(二)插入排序

using System;
public class InsertionSorter
{
public void Sort(int [] list)
{
for(int i=1;i

{
int t=list[i];
int j=i;
while((j>0)&&(list[j-1]>t))
{
list[j]=list[j-1];
--j;
}
list[j]=t;
}

}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
InsertionSorter ii=new InsertionSorter();
ii.Sort(iArrary);
for(int m=0;m<=13;m++)
Console.WriteLine("{0}",iArrary[m]);
}
}
C#演算法--(三)希爾排序

using System;
public class ShellSorter
{
public void Sort(int [] list)
{
int inc;
for(inc=1;inc<=list.Length/9;inc=3*inc+1);
for(;inc>0;inc/=3)
{
for(int i=inc+1;i<=list.Length;i+=inc)
{
int t=list[i-1];
int j=i;
while((j>inc)&&(list[j-inc-1]>t))
{
list[j-1]=list[j-inc-1];
j-=inc;
}
list[j-1]=t;
}
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
ShellSorter sh=new ShellSorter();
sh.Sort(iArrary);
for(int m=0;m<=13;m++)
Console.WriteLine("{0}",iArrary[m]);
}
}

C#演算法--(四)快速排序

using System;

namespace QuickSorter
{
public class QuickSorter
{
private void Swap(ref int l,ref int r)
{
int s;
s=l;
l=r;
r=s;
}
public void Sort(int [] list,int low,int high)
{
int pivot;
int l,r;
int mid;
if(high<=low)
return;
else if(high==low+1)
{
if(list[low]>list[high])
Swap(ref list[low],ref list[high]);
return;
}
mid=(low+high)>>1;
pivot=list[mid];
Swap(ref list[low],ref list[mid]);
l=low+1;
r=high;
do
{
while(l<=r&&list[l]

l++;
while(list[r]>=pivot)
r--;
if(l
Swap(ref list[l],ref list[r]);
}while(l
list[low]=list[r];
list[r]=pivot;
if(low+1
Sort(list,low,r-1);
if(r+1
Sort(list,r+1,high);
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
QuickSorter q=new QuickSorter();
q.Sort(iArrary,0,13);
for(int m=0;m<=13;m++)
Console.WriteLine("{0}",iArrary[m]);
}
}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.