C # Sorting Algorithm

Source: Internet
Author: User

C # algorithm -- (1) Select sorting

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 };
SelectionSorter ss = new SelectionSorter ();
Ss. Sort (iArrary );
For (int m = 0; m <= 13; m ++)
Console. WriteLine ("{0}", iArrary [m]);

}
}
C # algorithm -- (2) Insert sorting

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 };
InsertionSorter ii = new InsertionSorter ();
Ii. Sort (iArrary );
For (int m = 0; m <= 13; m ++)
Console. WriteLine ("{0}", iArrary [m]);
}
}
C # algorithm -- (3) Hill sorting

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 };
ShellSorter sh = new ShellSorter ();
Sh. Sort (iArrary );
For (int m = 0; m <= 13; m ++)
Console. WriteLine ("{0}", iArrary [m]);
}
}

C # algorithm -- (4) Fast sorting

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 timeout;
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;
Vertex = list [mid];
Swap (ref list [low], ref list [mid]);
L = low + 1;
R = high;
Do
{
While (l <= r & list [l]

L ++;
While (list [r]> = tables)
R --;
If (l
Swap (ref list [l], ref list [r]);
} While (l
List [low] = list [r];
List [r] = cursor;
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 };
QuickSorter q = new QuickSorter ();
Q. Sort (iArrary, 0, 13 );
For (int m = 0; m <= 13; m ++)
Console. WriteLine ("{0}", iArrary [m]);
}
}

}

Related Article

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.