All infos - Быстрая сортировка C# пример.
13
нояб

Быстрая сортировка C# пример.

static void Main()
{
	int[] numbers = { 4, -12, 3, 31, 0, 7, 4, 14, 90, 5 };
	numbers = QuickSort(numbers, 0, numbers.Length - 1);
	foreach(var e in numbers)
	{
		Console.Write(e + ", ");
	}
}
private static int[] QuickSort(int[] numbers, int minIndex, int maxIndex)
{
	if (minIndex >= maxIndex) return numbers;

	int pivot = FindPivot(numbers, minIndex, maxIndex);
	QuickSort(numbers, minIndex, pivot-1);
	QuickSort(numbers, pivot+1, maxIndex);
return numbers;
}

private static int FindPivot(int[] numbers, int minIndex, int maxIndex)
{
	int pivot = minIndex -1;
	int temp = 0;
	for (int i = minIndex; i < maxIndex; i++)
	{
		if (numbers[i] < numbers[maxIndex])
		{
			pivot++;
			temp = numbers[pivot];
			numbers[pivot] = numbers[i];
			numbers[i] = temp;
		}
	}

	pivot++;
	temp = numbers[pivot];
	numbers[pivot] = numbers[maxIndex];
	numbers[maxIndex] = temp;
	return pivot;
}

Share this post