//写一系列的重载函数,使其对任意数组(int、char、string)均可排序。同时,在数组之后可增加int参数,使数组可指定排序范围。例如:{1,3,5,2,4},可对3、5、2排序,1和4不变。
public class MathTool
{
public MathTool ()
{
}
public void Sort (int start, int end, params int[] a) {
int temp = a[start];
for (int i = start; i < end; i++) {
for (int j = start; j < end; j++) {
if (temp > a [i + 1]) {
temp = a [i + 1];
a [i] = a [i + 1];
a [i + 1] = a [i];
}
}
}
}
public void Sort(int start, int end, params char[] a) {
char temp = a [start];
for (int i = start; i < end; i++) {
for (int j = start; j < end; j++) {
if (temp > a [i + 1]) {
temp = a [i + 1];
a [i] = a [i + 1];
a [i + 1] = a [i];
}
}
}
}
public void Sort(int start, int end, params string[] a) {
for (int i = start; i < end - 1; i++) {
for (int j = start; j < end - 1 - start; j++) {
if (a[j].CompareTo (a[j + 1]) == 1) {
string temp = a [i + 1];
a [i] = a [i + 1];
a [i + 1] = temp;
}
}
}
}
}
C#学习笔记之——一些应用
最新推荐文章于 2024-08-11 20:37:46 发布