Hi,
I have a list of a datatype tag_t (similar to int). I need to sort this list using quicksort. This is what I tried.
I get the list to be sorted from a map and call quicksort on it.
Code:
std::list<tag_t> unsortedList = mpIter->second;
Quicksort(unsortedList, 0, unsortedList.size()-1);
My quicksort function is
Code:
void Quicksort(std::list < tag_t > &tempList, int first, int last)
{
int pivot;
if (first < last)
{
pivot = Pivot(tempList, first, last);
Quicksort(tempList, first, pivot - 1);
Quicksort(tempList, pivot + 1, last);
}
}
Code:
int Pivot(std::list < tag_t > &tempList, int first, int last)
{
int iStatus = ITK_ok;
int p = first;
list < tag_t > ::iterator listIter = tempList.begin();
tag_t tempTag = *listIter;
tag_t firstTag = *listIter;
int iComparison = 0;
for (int i = first + 1; i <= last; i++)
{
date_t date1;
date_t date2;
/*the tag_t datatypes actually corresponds to date values, so I get the date values
and compare them; if iComparison !=1 then date1 is earlier than date2. I am sorting
the tags by ascending order of their dates*/
iStatus = get_lastorder_date(tempTag, date1);
SIEMENS_LOG_ERROR;
iStatus = get_lastorder_date(*listIter, date2);
SIEMENS_LOG_ERROR;
iStatus = POM_compare_dates(date1, date2, &iComparison);
SIEMENS_LOG_ERROR;
if (iComparison != 1)
{
listIter++;
p++;
Swap(tempTag, *listIter);
}
}
Swap(*listIter, firstTag);
return p;
}
Code:
void Swap( tag_t &v1, tag_t &v2 )
{
tag_t tmpVal;
tmpVal = v1;
v1 = v2;
v2 = tmpVal;
}
Even after calling the Quicksort function, the order of the unsortedList <tag_t> is unchanged.
Can someone please help me figure out the issue.
Thanks.