2017-09-21 4 views
-1

On m'a donné un pseudo-code et j'ai à peu près la forme correcte, mais je ne comprends pas pourquoi cela me donne un débordement de pile à chaque fois que j'essaye d'exécuter le tri rapide dans Visual Studio . Voici la fonction que j'ai faite.C++ Quick Sort me donnant Stack Overflow

template <typename T> 
void quickSort(T list[], int lowerBound, int upperBound) 
{ 
int i = lowerBound; 
int j = upperBound; 
T tmp; 
T pivot = list[(lowerBound + upperBound)]/2; 

while (i <= j) 
{ 
    while (list[i] < pivot) 
    { 
     i = i + 1; 
    } 

    while (list[j] > pivot) 
    { 
     j = j - 1; 
    } 

    if (i <= j) 
    { 
     tmp = list[i]; 
     list[i] = list[j]; 
     list[j] = tmp; 
     i = i + 1; 
     j = j - 1; 
    } 
} 
if (lowerBound < j) 
    quickSort(list, lowerBound, j); 
if (i < upperBound) 
    quickSort(list, i, upperBound); 
} 

Merci!

+1

j'étais légèrement confus. Je pensais que votre tri rapide vous a toujours dirigé vers ce site. C'est un signe dont j'ai besoin pour dormir. – DrZoo

+1

ne doit pas pivoter être list [(lowerBound + upperBound)/2]? – thebenman

+0

@thebenman Vous aviez absolument raison. Je vous remercie! – DavidM

Répondre

2

changement T pivot = list[(lowerBound + upperBound)]/2; à T pivot = list[(lowerBound + upperBound)/2];