Переполнение стека при рекурсивной сортировке - C#
Формулировка задачи:
Вот код,который читает массив из файла и делает сортировку рекурсией
Но код не работает - происходит переполнение стека, как это исправить?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Sortir_neft
{
class Program
{
static void quicksort(ref int[] a, long l,long r)
{
long x = a[(int)Math.Ceiling((double)(l + r) / 2)];
long i = l;
long j = r;
while (i<=j)
{
while (a[i]<x)i++;
while (a[j]>x)j--;
if (i <= j)
{
int buf = a[i];
a[i] = a[j];
a[j] = buf;
i += 1;
j -= 1;
}
}
if (j > 1) quicksort(ref a, l, j);
if (r > i) quicksort(ref a, i, r);
}
static void Main(string[] args)
{
string[]s=System.IO.File.ReadAllLines("Input.txt");
int n, i;
n = int.Parse(s[0]);
int[] nums =s[1].Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => int.Parse(x)).ToArray();
StreamWriter fsr = null;
fsr = new StreamWriter("output.txt");
quicksort(ref nums, 0, n-1);
foreach (int x in nums)
fsr.Write(x+" ");
}
}
}
помогите кто-нибудь
Решение задачи: «Переполнение стека при рекурсивной сортировке»
textual
Листинг программы
if (j > l) quicksort(ref a, l, j);