Using Lists to do a selectionSort function

R

Rishi Dhupar

Hi,

Been trying to figure out how to use iterators. Basically trying to
sort the list using iterators. Here are my functions, problem is it
doesn't seem to be sorting at all. I think it has to do with my
selectionSort adding and removing elements from the list on each for
loop.

Any suggestions on what to do.

Thanks,

RishiD

// returns an iterator pointing to the largest element in the range
[first, last)
template <typename T>
typename list<T>::iterator maxIter(typename list<T>::iterator first,
typename list<T>::iterator last)
{
list<T>::iterator tempIter = first;
list<T>::iterator max = first;

int dCounter = 0;
while (tempIter != last)
{
cout << "maxIter counter " << dCounter++ << endl;
if (*tempIter > *max)
{
//cout << "old max " << *max << " setting to " << *tempIter << endl;
max = tempIter;
}
tempIter++;
}

return max;
}

// use selection sort to order aList
template <typename T>
void selectionSort(list<T>& aList)
{
list<T>::iterator maxNumber;
list<T>::iterator passIter = aList.begin();
for (int i = 0; i < aList.size(); i++)
{
// returns iterator to the max number between passIter and
aList.end()
maxNumber = maxIter<T> (passIter, aList.end());

// store the max number, temporaryly
int temp = *maxNumber;

cout << "passIter " << *passIter << " maxNumber return " << temp <<
endl;

// remove item from aList
aList.erase(maxNumber);

// adds the value to front of aList, also implies passIter++
aList.push_front(temp);
}
}
 
R

RishiD

Bleh just figured it out. Hate when that happens right after I post.
Guess the problem was I needed to reverse the erase and push_front
instructions. So it pushed the new value on, then erased.

Anyways thanks.

RishiD
 
P

Peter Jansson

Rishi said:
Hi,

Been trying to figure out how to use iterators. Basically trying to
sort the list using iterators. Here are my functions, problem is it
doesn't seem to be sorting at all. I think it has to do with my
selectionSort adding and removing elements from the list on each for
loop.

Any suggestions on what to do.

Thanks,

RishiD

// returns an iterator pointing to the largest element in the range
[first, last)
template <typename T>
typename list<T>::iterator maxIter(typename list<T>::iterator first,
typename list<T>::iterator last)
{
list<T>::iterator tempIter = first;
list<T>::iterator max = first;

int dCounter = 0;
while (tempIter != last)
{
cout << "maxIter counter " << dCounter++ << endl;
if (*tempIter > *max)
{
//cout << "old max " << *max << " setting to " << *tempIter << endl;
max = tempIter;
}
tempIter++;
}

return max;
}

// use selection sort to order aList
template <typename T>
void selectionSort(list<T>& aList)
{
list<T>::iterator maxNumber;
list<T>::iterator passIter = aList.begin();
for (int i = 0; i < aList.size(); i++)
{
// returns iterator to the max number between passIter and
aList.end()
maxNumber = maxIter<T> (passIter, aList.end());

// store the max number, temporaryly
int temp = *maxNumber;

cout << "passIter " << *passIter << " maxNumber return " << temp <<
endl;

// remove item from aList
aList.erase(maxNumber);

// adds the value to front of aList, also implies passIter++
aList.push_front(temp);
}
}


Hi,

Why not use std::max_element(first,last) and std::sort(first,last) from
<alogorithm> instead?


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top