Four sort algorithms for C #: Bubble sort, select sort, insert sort, and Hill sort

Source: Internet
Author: User
Tags insert min sort
Insert | sort | algorithm

This article describes the four sort algorithms for C #: Bubble sort, select sort, insert sort, and Hill sort

Bubble sort

Using System;

Namespace Bubblesorter

{public class Bubblesorter

{public void Sort (int [] list)

{int i,j,temp;

BOOL Done=false;

J=1;

while (j

{done=true;

For (i=0;i

{

if (list[i]>list[i+1])

{

Done=false;

Temp=list[i];

LIST[I]=LIST[I+1];

List[i+1]=temp;

} }

j + +;}

} }

public class MainClass

{public static void Main ()

{

Int[] Iarrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};

Bubblesorter sh=new Bubblesorter ();

Sh. Sort (iarrary);

for (int m=0;m

Console.Write ("{0}", Iarrary[m]);

Console.WriteLine ();

} }

}

Select sort

Using System;


Namespace Selectionsorter

{public class Selectionsorter

{private int min;

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;

} }

}

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

Console.Write ("{0}", Iarrary[m]);

Console.WriteLine ();

} }

}

Insert Sort

Using System;

Namespace Insertionsorter

{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,13,3,6,10,55,98,2,87,12,34,75,33,47};

Insertionsorter ii=new Insertionsorter ();

Ii. Sort (iarrary);

for (int m=0;m

Console.Write ("{0}", Iarrary[m]);

Console.WriteLine ();

} }

}

Hill sort

The hill sort is to insert the component segments into the sort.

Using System;

Namespace Shellsorter

{

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,13,6,10,55,99,2,87,12,34,75,33,47};

Shellsorter sh=new Shellsorter ();

Sh. Sort (iarrary);

for (int m=0;m

Console.Write ("{0}", Iarrary[m]);

Console.WriteLine ();

} }

}




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.