C# 求快速排序法的代码!

2025-01-07 23:54:35
推荐回答(3个)
回答1:

///


/// 快速排序法
///

///
///
///
public static void Sort(ref int[] data, int start, int end)
{
if (start >= end) return;
if (start + 1 == end)
{
if (data[start] > data[end])
Swap(ref data, start, end);

return;
}

int indexL = start + 1, indexR = end;
while (indexL < indexR)
{
// Get from left
while (indexL <= end && data[start] >= data[indexL])
indexL++;

// Get from right
while (indexR > start && data[start] < data[indexR])
indexR--;

if (indexL < indexR)
{
Swap(ref data, indexR, indexL);
}
}

if (indexL - 1 != start)
Swap(ref data, start, indexL - 1);

Sort(ref data, start, indexL - 2);
Sort(ref data, indexL, end);
}

回答2:

int k;
int[] array = new int[10] { 100,5,6,87,26,33,77,99,10,88};

for (int i = 0; i < 10; i++)
{
for (int j = i; j < 10; j++)
{
if (array[i] < array[j])
{
k = array[i];
array[i] = array[j];
array[j] = k;
}
}
}
for(int i = 0; i < 10;i++)
Console.Write(array[i]+"\t");
Console.ReadLine();

回答3:

冒泡,很简单