dimanche 30 mars 2014

Quicksort Infinite Loop Issue in C++


Vote count:

0




I have extensively tested the swap and partition function for the following code and they appear to be correct; however, when I try to execute quicksort it goes into an infinite loop. I'm assuming the issue happens from my returned value in partition, but I am not entirely sure what it is.



void swap_G(int *begin, int *end)
{
int temp = *begin;
*begin = *end;
*end = temp;
return;
}

int* partition(int* begin, int* end)
{
if (begin >= end) {
return begin;
}

int pivot = *end;
int* temp_Begin = begin, *temp_End = end;
temp_End--;

while (temp_Begin < temp_End)
{
while (*temp_Begin < pivot && temp_Begin < temp_End)
{
temp_Begin++;
}
while (*temp_End > pivot && temp_End > temp_Begin)
{
temp_End--;
}
swap_G(temp_Begin, temp_End);
}
swap_G(end, temp_Begin);
return temp_Begin;
}

void quicksort_G(int* begin, int* end)
{
if (begin >= end)
{
return;
}
int* mid = partition(begin, end);
quicksort_G(begin, --mid);
quicksort_G(mid + 1, end);
}


asked 27 secs ago






Aucun commentaire:

Enregistrer un commentaire