C #'s four sorting algorithms

Source: Internet
Author: User

This article introduces four sorting methods for C #.Algorithm: Bubble sorting, selection sorting, insert sorting, and Hill sorting

Bubble Sorting

Using system;

Namespace bubblesorter

{Public class bubblesorter

{Public void sort (INT [] list)

{Int I, j, temp;

Bool done = false;

J = 1;

While (j <list. Length )&&(! Done ))

{Done = true;

For (I = 0; I <list. Length-J; 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 };

Bubblesorter SH = new bubblesorter ();

Sh. Sort (iarrary );

For (INT m = 0; m <iarrary. length; 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 <list. Length-1; I ++)

{Min = I;

For (Int J = I + 1; j <list. length; j ++)

{If (list [J] <list [Min])

Min = J;

}

Int T = list [Min];

List [Min] = list [I];

List [I] = T;

}}

}

Public class mainclass

{Public static void main ()

{

Int [] iarrary = new int };

Selectionsorter Ss = new selectionsorter ();

SS. Sort (iarrary );

For (INT m = 0; m <iarrary. length; 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 <list. length; 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 };

Insertionsorter II = new insertionsorter ();

Ii. Sort (iarrary );

For (INT m = 0; m <iarrary. length; m ++)

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

Console. writeline ();

}}

}
 

 

Hill sorting

Hill sorting is to segment the group for insertion sorting.

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 };

Shellsorter SH = new shellsorter ();

Sh. Sort (iarrary );

For (INT m = 0; m <iarrary. length; 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.