Several common sort algorithms in C #

Source: Internet
Author: User
Tags datetime min sort
sort | Algorithm C # several commonly used sorting algorithms:
1 Bubble Sort Method
1 Bubble Sort Method #region Bubble Sort method
2public void Sort (int[] list)
3{
4 long begintime = System.datetime.now.second*1000+system.datetime.now.millisecond;
5 WriteLine (BeginTime);
6 int j,temp;
7 j= 1;
8 while (j<list. Length))
9 {
for (int i=0;i<list. length-j;i++)
11 {
if (list[i]<list[i+1])
13 {
temp = List[i];
List[i] = list[i+1];
LIST[I+1] = temp;
17}
18}
j + +;
20}
Long endtime = System.datetime.now.second*1000+system.datetime.now.millisecond;
WriteLine (Endtime);
WriteLine (Endtime-begintime);
24}
25#endregion
2 Choice Sorting method
1 Choice Sorting Method #region selection method
2public void Sortchoice (int [] list)
3{
4 long begintime = System.DateTime.Now.Millisecond;
5 int min;
6 for (int i=0;i<list. length-1;i++)
7 {
8 min=i;
9 for (int j=i+1;j<list. length;j++)
10 {
One if (List[j]<list[min])
Min=j;
13}
int t=list[min];
List[min]=list[i];
list[i]=t;
17}
A long endtime = System.DateTime.Now.Millisecond;
WriteLine (BeginTime);
WriteLine (Endtime);
WriteLine (Endtime-begintime);
22}
23#endregion
3 Insert Sort method
1 Insertion Sort Method #region Insertion Sort method
2public void Sortinsert (int [] list)
3{
4 for (int i=1;i<list. length;i++)
5 {
6 int t=list[i];
7 int j=i;
8 while ((j>0) && (list[j-1]<t)
9 {
Ten list[j]=list[j-1];
One--j;
12}
list[j]=t;
14}
15}
16#endregion
4 Hill Sorting method
1 Hill sorting Method #region Hill sorting method
2public void Sortshell (int [] list)
3{
4 INT Inc;
5 for (inc=1;inc<=list. LENGTH/9;INC=3*INC+1);
6 for (; inc>0;inc/=3)
7 {
8 for (int i=inc+1;i<=list. LENGTH;I+=INC)
9 {
ten int t=list[i-1];
one int j=i;
while ((J>inc) && (list[j-inc-1]>t))
13 {
LIST[J-1]=LIST[J-INC-1];
J-=inc;
16}
list[j-1]=t;
18}
19}
20}
21#endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.