Issue with Alphabetical Selection Sort program

本小妞迷上赌 提交于 2019-12-12 19:03:21

问题


I'm having an issue with a homework problem involving the Selection Sort concept. We were given a skeleton code within which we need to complete the bool compare(...) and void selectionsort(...) functions, which I have done. Then, running the program should sort the strings given in main() alphabetically, and prints them in alphabetical order after printing the initial strings. However, mine doesn't sort it alphabetically and after trying to change multiple things I am stuck on figuring out why.

Please note the only things I am allowed to edit are the compare and selectionsort methods, nothing else. We are required to use the compare method (which returns true if the first string comes sooner in the alphabet than the other string, did that) within the selectionsort method in order to compare them, rather than a simple comparison < > = statement.

I believe my problem has something to do with that if statement within the for loop but I'm having a lot of trouble figuring out the correct way to do it. Any help would be much appreciated!

#include<iostream>
using namespace std;

bool compare(char *str1, char *str2, int strLen1, int strLen2) { // complete this method
    int small;
    if(strLen1 > strLen2)
        small = strLen2;
    else
        small = strLen1;

    //compare lexicographic values
    for(int i=0; i<small;i++){
        if(str1[i] < str2[i])
            return true;
        else if (str2[i] < str1[i])
            return false;
    }

    if (strLen1 < strLen2)
        return true;
    else
        return false;
}

void selectionsort(char **strings, int numStrings, int *eachStringLen) { // complete this method
    /* strings = jagged array
    numStrings = numRows
    eachStringLen = numColumnsInEachRow*/

    for(int i=0; i<numStrings-1;i++){
        int minIndex = i;
        if(compare(strings[i], strings[i+1], eachStringLen[i], eachStringLen[i+1]) == false){
        for(int j=i+1;j<numStrings;j++){
            if(strings[j]<strings[minIndex])
                minIndex = j;
        }
    }

        //swap strings[minIndex] and strings[i]
        char *tempA = strings[i];
        strings[i] = strings[minIndex];
        strings[minIndex] = tempA;

        int tempB = eachStringLen[i];
        eachStringLen[i] = eachStringLen[minIndex];
        eachStringLen[minIndex] = tempB;

    }//end for i
}//end selectionsort

int main() {
    char str0[] = { 'a', 'b', 'c' };
    char str1[] = { 'x', 'y', 'z', 'w' };
    char str2[] = { 'x', 'y', 'z', 'a', 'b' };
    char str3[] = { 'a', 'b', 'c', 'd', 'x' };
    char str4[] = { 'w', 'x', 'c', 'd', 'x' };
    char str5[] = { 'a', 'b', 'c', 'x', 'y' };
    char str6[] = { 'a', 'a', 'c' };
    char str7[] = { 'w', 'x', 'c', 'd', 'x' };
    char str8[] = { 'a', 'b', 'c', 'x'};
    char *strings[] = { str0, str1, str2, str3, str4, str5, str6, str7, str8 };
    int eachStringLength[] = { sizeof(str0) / sizeof(char), sizeof(str1)
            / sizeof(char), sizeof(str2) / sizeof(char), sizeof(str3)
            / sizeof(char), sizeof(str4) / sizeof(char), sizeof(str5)
            / sizeof(char), sizeof(str6) / sizeof(char), sizeof(str7)
            / sizeof(char), sizeof(str8)
            / sizeof(char) };
    int numStrings = 9;
    cout << "*** Original Strings ***" << endl;
    for (int i = 0; i < numStrings; i++) {
        for (int j = 0; j < eachStringLength[i]; j++) {
            cout << strings[i][j];
        }
        cout << endl;
    }
    selectionsort(strings, numStrings, eachStringLength);
    cout << endl << "*** Sorted Strings ***" << endl;
    for (int i = 0; i < numStrings; i++) {
        for (int j = 0; j < eachStringLength[i]; j++) {
            cout << strings[i][j];
        }
        cout << endl;
    }
}

回答1:


Your sorting logic is slightly off. You need two loops as selection sort is O(N^2), and no comparison sort can be faster than O(NlogN).

The outer loop should iterate over each index i. The inner loop should find the smallest element in the range i to N and swap it with the element at i.

I would also suggest you use std::swap to make your code more readable.

Thus the meat of your code should look like:

for(int i=0; i<numStrings;++i){
    int minIndex = i;
    for(int j=i+1;j<numStrings;++j){
        if(compare(strings[j], strings[minIndex], eachStringLen[j], eachStringLen[minIndex])){
            minIndex = j;
        }
    }

    std::swap(strings[i], strings[minIndex]);
    std::swap(eachStringLen[i], eachStringLen[minIndex]);
}//end for i

Note you can write your outer loop to continue on i<numStrings instead of i<numStrings-1 because the last element will necessarily swap with itself. But both exit conditions are ok.

The output is:

*** Original Strings ***
abc
xyzw
xyzab
abcdx
wxcdx
abcxy
aac
wxcdx
abcx

*** Sorted Strings ***
aac
abc
abcdx
abcx
abcxy
wxcdx
wxcdx
xyzab
xyzw


来源:https://stackoverflow.com/questions/56086677/issue-with-alphabetical-selection-sort-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!