c#排序方式
1.插入排序
for (int i = 0; i < arr.Length; i++)
{
int temp = arr[i];
int j = i;
while ((j>0)&&(arr[j-1]>temp))
{
arr[j] = arr[j - 1];
--j;
}
arr[j] = temp;
}
2.冒泡排序
int j, temp;
for (int i = 0; i < arr.Length-1; i++)
{
j = i + 1;
id:
if (arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
goto id;
}
else
{
if (j<(arr.Length-1))
{
j++;
goto id;
}
}
}
3.选择排序
int min;
for (int i = 0; i < arr.Length-1; i++)
{
min = i;
for (int j = i+1; j < arr.Length; j++)
{
if (arr[j]<arr[min])
{
min = j;
}
}
int t = arr[min];
arr[min] = arr[i];
arr[i] = t;
}
下面是一些c#提供的排序方法
1.升序排序
Array.Sort();
例一(int):
// sort
int arrayint[] intArray = new int[5] { 8, 10, 2, 6, 3 };
Array.Sort(intArray);
// write array
foreach (int i in intArray) Console.Write(i + " "); // output: 2 3 6 8 10
例二(string):
/ sort string array
string [] stringArray = new string[ 5 ] { "Banana" , "Watermelon" , "Tomato" , "Water" , "Apple" };
Array.Sort( stringArray );
// write array
foreach ( string str in stringArray) Console .Write ( str + " " ); // output: Apple Banana Tomato Water Watermelon
2.反转排序
list.Reverse();
int[] MyArray = new string[]{1,2,3,4,5}
Array.Reverse(Myarray);
foreach(int item in Myarray){
print(item);
}
//return 5 ,4 ,3 ,2, 1