C # Sorting Algorithm (reproduced)
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 the data structure to learn the language.
And algorithms.
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 the data structure to learn the language.
And algorithms.
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,
Help it implement it.
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
And does not implement polymorphism. Come and help implement it.
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 ();
}
}
}
**************************************** *************************
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 the data structure and
Algorithm.
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 the data structure and
Algorithm.
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.
It is implemented.
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, and
No polymorphism is implemented. Let's help implement it.
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 ();
}
}
}
**************************************** **************************************** *
In fact, any element can be sorted as long as it can be compared with each other. For example, you can add a bunch of strings to it and then sort the strings in sort,
The sorting rule is consistent with the "size comparison" rule of the string.
If you want to sort a bunch of structures or classes, you only need to implement the icomparable interface. This interface indicates
The instance can compare the size with something else. Eg:
Class myrecord: icomparable
{
Public int num;
Public String description;
Public int compareto (Object OBJ)
{
Myrecord REC;
Rec = OBJ as myrecord;
// If the object to be compared is a myrecord class
If (REC! = NULL)
{
Return num. compareto (Rec. Num); // The size depends on the num of both parties.
}
Else
{
Return-1; // otherwise it is always smaller than this.
}
}
}
In this way, after myrecord is added to the arraylist, it can be sorted by the num size. Note that the arraylist. sort is sorted by a litre
If you need a descending result, you just need to take the return value as a negative value. eg: Return-num. compareto (Rec. Num );
In addition, the return-1 must be described as follows:
1. the return value of compareto has the following meanings:
> 0 indicates this> OBJ
= 0 indicates this = OBJ
<0 indicates this <OBJ
2. The compareto parameter obj is not assumed to be of the same type as this, so you must make your own judgment. Here the else clause is for processing type
Different situations.
3. When the types are different, you can also consider throw exception. However, this may cause unnecessary program termination exceptions.
Return-1. So what does return-1 mean? Because sort is always in ascending order, it is usually easier to pay attention
The previous items must be compareto small. Therefore, we need to mention the myrecord project as much as possible. Therefore, as long as obj is not myrec
-1 is returned.
4. Although in theory, arr. Sort allows sorting between different types of data, this is not recommended. Because it's hard for you to take care of sort.
Call the compareto of the myrecord object or the compareto Of The OBJ object. If the OBJ object always wants to be at the beginning, it may cause confusion. Or
If OBJ always throws an exception when the types are inconsistent, the program will terminate unexpectedly. Therefore, try not to mix different types in the arraylist that requires sort
.
5. When using sort, make sure that there are two items:
A. This arraylist cannot have no elements; otherwise, an exception will be thrown (although this is a Ms bug, you do not perform the Count = 0 check ).
Check, others will think this is your bug ).
B. Each element in this arraylist implements the icomparable interface, or you provide an object for sort to implement the icomparer interface. No
It also causes an exception. All predefined types of the system implement the icomparable interface. For icomparer, I suggest you leave it alone unless
You want to sort by num later, sort by description later, and sort by something else later. So I will not describe it here
If you have this requirement, let me know.
6. There is no need to worry about performance issues. It is usually fast enough. If you find performance problems, look for other reasons first. At least, you should miss me
In this way, write the class myrecord instead of structure myrecord.
**************************************** **************************************** *
A complicated example may give you some more deliberate thoughts.
Using system;
Namespace examplemodel
{
Public class bubblesorter // bubble sort
{
Static public void sort (object [] sortarray, compareop rhslhsgreater)
{
For (INT I = 0; I <sortarray. length; I ++)
{
For (Int J = 0; j <I; j ++)
{
If (rhslhsgreater (sortarray [I], sortarray [J])
{
Object temp = sortarray [I];
Sortarray [I] = sortarray [J];
Sortarray [J] = temp;
}
}
}
}
}
Public class employee
{
Private string name;
Private decimal salary;
Public Employee (string name, decimal salary ){
This. Name = Name;
This. Salary = salary;
// Add the constructor code here
}
Public override string tostring ()
{
Return string. Format (name + ", {0}", salary );
}
Public static bool rhslhsgreater (Object LHS, object RHs)
{
Employee LHS = (employee) LHS; // type conversion
Employee RHS = (employee) RHS;
Return (RHS. salary> LHS. Salary )? True: false;
}
// Add class code here
}
Public Delegate bool compareop (Object LHS, object RHs); // delegate
Public class mainentrypoint
{
Public static void main (string [] ARGs)
{
Employee [] employees =
{
New Employee ("Karli Watson", 20000 ),
New Employee ("Bill Gates", 10000 ),
New Employee ("Peng song", 25000 ),
New Employee ("Mortimer", (decimal) 1000000.38 ),
New Employee ("arabel Jones", 23000 ),
New Employee ("Avon from 'blaks'", 50000)
};
Compareop employeecompareop = new compareop (employee. rhslhsgreater );
Bubblesorter. Sort (employees, employeecompareop );
For (INT I = 0; I <employees. length; I ++)
Console. writeline (employees [I]. tostring ());
// Add the main program here
}
}
}
You can sort custom classes for the program!
**************************************** **************************************** *