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