一、快速排序
void Quick(int[] arr,int leftIndex,int rightIndex)
{
if (leftIndex < rightIndex)
{
int X = arr[leftIndex];
int i = leftIndex;
int j = rightIndex;
while (i < j)
{
while (i < j)
{
if (arr[j]< X)
{
arr[i] = arr[j];
break;
}else
{
j--;
}
}
while (i< j)
{
if (arr[i] > X )
{
arr[j] = arr[i];
break;
}else
{
i++;
}
}
}
arr[i] = X;
Quick(arr,leftIndex,i- 1);
Quick(arr,i+ 1,rightIndex);
}
二、插入排序
public void paixu(int[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
int value = arr[i];
bool isInsert = false;
for (int j = i -1 ; j >= 0; j--)
{
if (arr[j] > value)
{
arr[j + 1] = arr[j];
}else
{
arr[j+ 1] = value;
isInsert = true;
break;
}
}
if (isInsert == false)
{
arr[0] = value;
}
}
}
三、选择排序
void xuanZe(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
int min = arr[i];
int minIndex = i;
for (int j = i + 1 ; j < arr.Length; j++)
{
if (arr[j] < min)
{
min = arr[j];
minIndex = j;
}
}
if (minIndex != i)
{
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}