Bubble Sorting
I used C # To develop a Bubble Sorting Algorithm. Hope to bring some benefits to learners of C # language. Don't forget, it takes a lot of effort to learn data structures and algorithms to learn languages.
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
I used C # To develop a selection sorting algorithm. Hope to bring some benefits to learners of C # language. Don't forget, it takes a lot of effort to learn data structures and algorithms to learn languages.
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
Insert a sorting algorithm. If you want to improve your C # programming skills, we can discuss with each other. For example, the following program does not implement polymorphism.
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 refers to grouping and insertion sorting. For friends who want to improve C # programming ability, we can discuss with each other. For example, the following program does not implement polymorphism.
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 ();
}
}
}