Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字母气泡排序数组_C++_Sorting - Fatal编程技术网

C++ 字母气泡排序数组

C++ 字母气泡排序数组,c++,sorting,C++,Sorting,如果输入名称的数量,我应该使用冒泡排序按字母顺序列出它们。然而,我似乎无法获得正确的算法,因为我的输出是以某种随机顺序给出的。我的示例输入是 5 Alice Hawking John Smith Stephen Hawking Alice Cooper Jean Smith 代码是 int main() { int number; char char1; char char2; int measure; int name1; int n

如果输入名称的数量,我应该使用冒泡排序按字母顺序列出它们。然而,我似乎无法获得正确的算法,因为我的输出是以某种随机顺序给出的。我的示例输入是

5 
Alice Hawking 
John Smith 
Stephen Hawking 
Alice Cooper 
Jean Smith
代码是

int main() {
    int number;
    char char1;
    char char2;
    int measure;
    int name1;
    int name2;
    int flag = 1;
    int count = 0, ter = 0;
    string tmp;

    cin >> number;

    string list[number+1][2];

    for (int i = 0; i < number; i++) {
        cin >> list[i][0] >> list[i][1];
    }

    // sorting first names
    for (int k = 0; k <= number; k++) {
        for (int i = 0; i < number - 1; i++) {
            // j cannot go beyond the length of the smallest first name
            // so measure will be the measure of the smallest first name
            if (list[i][0].size() < list[i+1][0].size()) {
                measure = list[i][0].size();
            } else {
                measure = list[i+1][0].size();
            }

            // convert the each letter of the string by converting 
            // string to char to int
            // flag is to indicate if the two comparisons are not the same
            // so that the if loop does not continue 
            for (int j = 0; flag && j < measure; j++) {
                char1 = list[i][0].at(j);
                char2 = list[i+1][0].at(j);
                name1 = (int)char1;
                name2 = (int)char2;
                if (name1 > name2) {
                    tmp = list[i][0];
                    list[i][0] = list[i+1][0];
                    list[i+1][0] = tmp;
                    flag = 0;
                }
            }
            flag = 1;
        }
    }

    // sorting last names
    for (int k = 0; k <= number; k++) {
        for (int i = 0; i < number - 1; i++) {
            // j cannot go beyond the length of the smallest last name
            // so measure will be the measure of the smallest last name
            if (list[i][1].size() < list[i+1][1].size()) {
                measure = list[i][0].size();
            } else {
                measure = list[i+1][0].size();
            }

            for (int j = 0; flag && j < measure; j++) {
                char1 = list[i][1].at(j);
                char2 = list[i+1][1].at(j);
                name1 = (int)char1;
                name2 = (int)char2;
                if (name1 > name2) {
                    tmp = list[i][1];
                    list[i][1] = list[i+1][1];
                    list[i+1][1] = tmp;
                    flag = 0;
                }
            }
            flag = 1;
        }
    }


    for (int t = 0; t < number; t++) {
        cout << list[t][0] << " " << list[t][1] << endl;
    }
}
intmain(){
整数;
char-char1;
char-char2;
整数测度;
国际名称1;
国际名称2;
int标志=1;
int计数=0,ter=0;
串tmp;
cin>>数量;
字符串列表[编号+1][2];
for(int i=0;i>列表[i][0]>>列表[i][1];
}
//名字排序
for(int k=0;k name 2){
tmp=列表[i][0];
列表[i][0]=列表[i+1][0];
列表[i+1][0]=tmp;
flag=0;
}
}
flag=1;
}
}
//姓氏排序
for(int k=0;k name 2){
tmp=列表[i][1];
列表[i][1]=列表[i+1][1];
清单[i+1][1]=tmp;
flag=0;
}
}
flag=1;
}
}
for(int t=0;tcout这里有一些东西要看

1比较字符串

有什么理由不只是使用
字符串
比较来比较名称吗?从
//j到
标志=
(交换除外)的整个代码可以替换为

if (list[i][0] > list[i+1][0])
2在两个循环中比较第一个和最后一个

最好在同一个循环中检查姓氏和名字。类似于:

if (list[i][0] > list[i+1][0] || (list[i][0] == list[i+1][0] && list[i][1] > list[i+1][1]) {
}
注意:如果要按姓氏排序,请更改
0
1

3交换


目前,您正在独立于姓氏交换名字。这将导致您的名字混淆。同时交换它们。

太棒了,我不知道字符串比较是这样工作的。非常感谢!您为我省去了很多麻烦。您可以将子功能拆分为
min
Swap
字符串\u比较