C # several common sorting algorithms:

Source: Internet
Author: User
C # several common sorting algorithms:
1. Bubble Sorting
1. bubble sort method # region bubble sort method
2 public 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 {
10 for (int I = 0; I <list. Length-j; I ++)
11 {
12 if (list [I] <list [I + 1])
13 {
14 temp = list [I];
15 list [I] = list [I + 1];
16 list [I + 1] = temp;
17}
18}
19 j ++;
20}
21 long endtime = System. DateTime. Now. Second * 1000 + System. DateTime. Now. Millisecond;
22 WriteLine (endtime );
23 WriteLine (endtime-begintime );
24}
25 # endregion
2. Select sorting method
1. Select the sorting method # region select the sorting method
2 public 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 {
11 if (list [j] <list [min])
12 min = j;
13}
14 int t = list [min];
15 list [min] = list [I];
16 list [I] = t;
17}
18 long endtime = System. DateTime. Now. Millisecond;
19 WriteLine (begintime );
20 WriteLine (endtime );
21 WriteLine (endtime-begintime );
22}
23 # endregion
3 insert sorting
1 insert sorting method # region insert sorting method
2 public 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 {
10 list [j] = list [J-1];
11 -- j;
12}
13 list [j] = t;
14}
15}
16 # endregion
4. Hill sorting
1. Hill sort method # region Hill sort method
2 public 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 {
10 int t = list [I-1];
11 int j = I;
12 while (j> inc) & (list [j-inc-1]> t ))
13 {
14 list [J-1] = list [j-inc-1];
15 j-= inc;
16}
17 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.