简体   繁体   中英

Writing a selection sort for STL list?

I am writing a particular c++ program to use selection sort on an STL list as it is required by my professor.

I am using Netbeans 9.2. Currently, I got stuck with my algorithm. For the first few times, the program compiles but the list after selection always ends up with the same values(say it was supposed to be 99, 24, 15, 80, 27, it would always be 1, 1, 1, 1, 2 after the sort). Now the algorithm just straight up wouldn't compile. I am relatively new to coding. Could someone please tell me what I've gotten wrong and how I should go about it? Much thanks!

Here's my code:

void selectionSort(list<short> l, int size) {
    list<short>::iterator it1;
    list<short>::iterator it2;
    list<short>::iterator it3;
    short min, temp; 
    for(it1 = l.begin(); it1 != l.end(); it1++) {
        temp = min = *it1;
        it2 = it1;
        for(it2 = it1; it2 != l.end(); it2++) {
            if(*it2 < min) {
                min = *it2;
                it3 = it2;
            }    
        }
        *it1 = min;
        *it3 = temp;
        //Increment the first counter at the end
        temp = min = *it1;
    }
}

There is a bug in your code which will lead to a crash. I've fixed it in the following code.

But I still don't know why it outputs strange numbers like 1, 1, 1, 1, 2. Maybe it's caused by the rest of your code. It would be helpful if more code or information can be provided.

void selectionSort(list<short> l, int size) {
    list<short>::iterator it1;
    list<short>::iterator it2;
    list<short>::iterator it3;
    short min, temp;
    for(it1 = l.begin(); it1 != l.end(); it1++) {
        temp = min = *it1;
        it2 = it1;
        it3 = l.end();  // NOTE: to fix the bug
        for(it2 = it1; it2 != l.end(); it2++) {
            if(*it2 < min) {
                min = *it2;
                it3 = it2;
            }
        }
        if (it3 != l.end()) {  // NOTE: to fix the bug
            *it1 = min;
            *it3 = temp;
        }  // NOTE: to fix the bug
        //Increment the first counter at the end
        temp = min = *it1;  // NOTE: This is unnecessary
    }
}

You're passing the parameter l by value, not by reference. That's why the manipulation you do within the function selectionSort does nothing to change the list. Here's a version that works:

#include <iostream>
#include <list>

void selectionSort(std::list<short>& l) {
  std::list<short>::iterator it1;
  std::list<short>::iterator it2;
  std::list<short>::iterator it3;
    short min, temp;
    for(it1 = l.begin(); it1 != l.end(); it1++) {
        temp = min = *it1; 
        it3 = l.end();
        for(it2 = it1; it2 != l.end(); it2++) {
            if(*it2 < min) {
                min = *it2;
                it3 = it2;
            }
        }
        if (it3 != l.end()) {
            *it1 = min;
            *it3 = temp;
        }
    }
}

int main()
{
  std::list<short> mylist= {10,1,8,13,14,7,6,5,18,9,19,12,17,15,4,2};
  selectionSort(mylist);
  std::list<short>::iterator it;
  std::cout << "elements in list\n";
  for (it = mylist.begin(); it != mylist.end(); it++) {
      std::cout << *it << std::endl;
    }
  return 0;
}

In short, you need to add an & after the type of l in the declaration of your selectionSort function.

Update

I removed the size parameter from the code because you're not using it, and really don't need it anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM