Быстрая сортировка с подсчетом операций сравнения - C#
Формулировка задачи:
Помогите пжста разобраться с проблемой ...
прога сортирует массив сортировками шелла и быстрой, подсчитывает кол-во операций!
Для шелла все работает, а быстрая не хочет считать операции т.к. рекурсия!
Сделала переменную ( в которую записываем кол-во операций) статической - не помогает
Как поступить?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Global
{
public static int nforq = 0; /*считает кол-во операций для быстрой сортировки ( считает не правильно ) */
}
class Program
{
static void Main(string[] args)
{
int n = 20;
int low = 0, high = 0;
Random rnd = new Random();
int[] mas = new int[n];
for (int i = 0; i < n; ++i)
{
mas[i] = rnd.Next(-100, 100);
Console.Write(mas[i] + " ");
}
Console.Write("\n");
Console.Write("Сортировкой Шелла:\n");
shellSort(mas, n);
Console.Write("\n");
Console.Write("Быстрой сортировкой:\n");
qSort(mas, low, high, n, Global.nforq);
}
static void shellSort(int[] mas, int n) // Шелла ( всё ок )
{
int nforshell = 0;
int step = mas.Length / 2;
while (step > 0)
{
int i, j;
for (i = step; i < mas.Length; i++)
{
int value = mas[i];
for (j = i - step; (j >= 0) && (mas[j] < value); j -= step)
mas[j + step] = mas[j];
mas[j + step] = value;
nforshell += 1;
}
step /= 2;
nforshell += 1;
}
for (int i = 0; i < n; ++i)
{
Console.Write(mas[i] + " ");
}
Console.Write("\n");
Console.Write(nforshell + "<- Количество операций\n");
Console.Write("-----------------------");
}
static void qSort(int[] mas, int low, int high, int n, int nforq) // Быстрая сорт
{
int i = low;
int j = high;
int x = mas[(low + (high - low) / 2)];
do
{
while (mas[i] < x)
++i;
nforq += 1;
while (x < mas[j])
--j;
nforq += 1;
if (i <= j)
{
int t = mas[i];
mas[i] = mas[j];
mas[j] = t;
++i;
--j;
nforq += 1;
}
nforq += 1;
} while (i <= j);
if (low < j)
qSort(mas, low, j, n, Global.nforq);
nforq += 1;
if (i < high)
qSort(mas, i, high, n, Global.nforq);
nforq += 1;
for (int m = 0; m < n; ++m)
{
Console.Write(mas[m] + " ");
}
Console.Write("\n" + nforq + "<-Количество операций\n");
}
}
}Решение задачи: «Быстрая сортировка с подсчетом операций сравнения»
textual
Листинг программы
nforq += 1;