You are here:

C++/Question about selection sort function


Dear Dave,

I'm working on a program that involves working with list iterators. It gives the user some options to manipulate the lists (like insert a number, find a number, etc.).My problem is with the option to sort the list. My selction_sort function is accessing the list, but I it's not sorting it correctly. I think my flaw is after the call to the min function. I have provided three functions that I think you may want to look at to understand where I'm coming from. The selection_sort function is from my list class and the other two are from my list_iterator class. I'm using visual c++ on a Dell PC with winXP. I hope you can help. Thank you for your time.

void List::selection_sort()
  if (!head)
  if (!head->get_next())

  List_node_ptr sorted = NULL;

  List_iterator iter1 = head;
  List_iterator iter2 = head->get_next();
  List_node_ptr prev = NULL;

  while (iter2){


List_iterator List_iterator::min()
  if (current){
    List_node_ptr smallest = current;
    List_node_ptr temp = current;
     while (temp->next){
     temp = temp->next;
     if (temp->datum < smallest->datum)
         smallest = temp;
     return smallest;

     return NULL;

bool List_iterator::swap(List_iterator &other)
 if (current && *(other)) {
   int temp = current->datum;
   current->datum = *other;
   *other = temp;
   return true;
   return false;

Jerod , Thank you for your question. I'm David, not 'Dave', by the way.

I regret that I am unable to do debugging for people due to the volume of questions received. This kind of problem is usually easy to solve by stepping through the code with your debugger, looking at the values of the variables until you see something wrong. This should show you the exact line of the bug.

David Spector


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 All rights reserved.