如何修复我的代码以进行选择排序?

时间:2015-09-30 14:56:29

标签: c++

这是我实现选择排序的代码,但输出的顺序不正确。有人可以告诉我问题在哪里吗?

所以这是我实现选择排序的代码:

#include <iostream>
using namespace std;

class sort {
public:
    void ssort(string[], int[]);
    void print(string[], int);

};

// sorting function
// is the problem here?
void sort::ssort(string arr[], int numarray[]) {
    int min;

    for (int i = 0; i < 8; ++i)
    {
        min = i;
        //Loop through the array to find it
        for (int j = i + 1; j < 9; ++j)
        {
            if (numarray[j] < numarray[min])
            {
                //Found new minimum position, if present
                min = j;
            }
        }
        //Swap the values
        swap(arr[i], arr[min]);
    }
}

void sort::print(string arr[], int num) {
    for (int i = 0; i < num; ++i)
    {
        cout << arr[i] << endl;
    }
    cout << endl;
}

//main function
main() {
    sort s;
    int num = 8;
    string arr[8];
    string array[8];
    int numarray[8];


    cout << "Input 8 strings to sort: " << endl;
    for (int i = 0; i < 8; i++) {
        getline(cin, arr[i]);
    }
    cout << endl;

    for (int i = 0; i < 8; i++) {
        array[i] = arr[i].substr(0, 1);

        if (array[i] == "a")
            numarray[i] = 1;
        if (array[i] == "b")
            numarray[i] = 2;
        if (array[i] == "c")
            numarray[i] = 3;
        if (array[i] == "d")
            numarray[i] = 4;
        if (array[i] == "e")
            numarray[i] = 5;
        if (array[i] == "f")
            numarray[i] = 6;
        if (array[i] == "g")
            numarray[i] = 7;
        if (array[i] == "h")
            numarray[i] = 8;
        if (array[i] == "i")
            numarray[i] = 9;
        if (array[i] == "j")
            numarray[i] = 10;
        if (array[i] == "k")
            numarray[i] = 11;
        if (array[i] == "l")
            numarray[i] = 12;
        if (array[i] == "m")
            numarray[i] = 13;
        if (array[i] == "n")
            numarray[i] = 14;
        if (array[i] == "o")
            numarray[i] = 15;
        if (array[i] == "p")
            numarray[i] = 16;
        if (array[i] == "q")
            numarray[i] = 17;
        if (array[i] == "r")
            numarray[i] = 18;
        if (array[i] == "s")
            numarray[i] = 19;
        if (array[i] == "t")
            numarray[i] = 20;
        if (array[i] == "u")
            numarray[i] = 21;
        if (array[i] == "v")
            numarray[i] = 22;
        if (array[i] == "w")
            numarray[i] = 23;
        if (array[i] == "x")
            numarray[i] = 24;
        if (array[i] == "y")
            numarray[i] = 25;
        if (array[i] == "z")
            numarray[i] = 26;

    }

    cout << "The Initial Input: " << endl;
    s.print(arr, num);

    s.ssort(arr, numarray);

    cout << endl << "The input after sorting: " << endl;
    s.print(arr, num);
}

我该怎么办?

2 个答案:

答案 0 :(得分:3)

我通过快速阅读您的代码确定了三个问题:

  • 首先内循环条件错误

    void sort::ssort(string arr[], int numarray[]) {
      int min;
    
      for (int i = 0; i < 8; ++i) {
        min = i;
        //Loop through the array to find it
        for (int j = i + 1; j < 9; ++j) // Out-of-bound access on j == 8
    
  • 第二:您的ssort函数只会根据第一个字符对字符串进行排序。它并没有真正对字符串进行排序。

  • 第三个(这是一个很大的算法错误):交换numarray元素后,您没有交换arr向量元素。这意味着第一个之后的所有掉期(指数位于正确的位置)可能错误

    void sort::ssort(string arr[], int numarray[]) {
      int min;
    
      for (int i = 0; i < 8; ++i)
      {
        min = i;
        //Loop through the array to find it
        for (int j = i + 1; j < 8; ++j)
        {
          if (numarray[j] < numarray[min])
          {
              //Found new minimum position, if present
            min = j;
          }
        }
        //Swap the values
        swap(arr[i], arr[min]);
        // Also swap the comparison values
        swap(numarray[i], numarray[min]); // <-- this is missing!
      }
    }
    

改善提示:

  • 拿一本好的算法书并研究它
  • 抓住一个好的调试器并使用它

答案 1 :(得分:1)

您应该更新numarray以及arr,并且无法访问越界。

void sort::ssort(string arr[], int numarray[]) {
    int min;

    for (int i = 0; i < 8; ++i)
    {
        min = i;
        //Loop through the array to find it
        for (int j = i + 1; j < 8; ++j) // fix : 9 -> 8
        {
            if (numarray[j] < numarray[min])
            {
                //Found new minimum position, if present
                min = j;
            }
        }
        //Swap the values
        swap(arr[i], arr[min]);
        swap(numarray[i], numarray[min]); // add this line
    }
}