按Alpha顺序排序2D Char数组? [英] Sorting a 2D Char Array in Alpha order?

查看:105
本文介绍了按Alpha顺序排序2D Char数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按字母顺序对2D名称数组进行排序,但是我无法缝隙使它正常工作。

I am trying to sort a 2D array of names into alphabetical order, but I can not seam to get it to work.

我正在使用冒泡排序字母,这很好地对名称的第一个字母进行了排序,但是其中三个名称以相同的字母开头,但它们仍然乱序。

I am using a bubble sort on the letters, and this is sorting the 1st letter of the names fine, but 3 of the names start with the same letter and they are still out of order.

我有尝试谷歌搜索和东西,但每一个ting说使用向量或字符串变量..但我仅限于使用2d char数组。.

I have tried googleing and stuff but every ting says to use vectors or string variables.. but I am limited to using 2d char arrays..

有什么想法吗?

这是我目前可以正常使用的代码:

Here is the code I have at the moment that works nearly:

using namespace std;

int main (){

    char heroes[11][17] = { "Captain America", "Thor", "Wolverine", "Cyclops", "Goliath", "Beast", "Angel", "Colossus", "Hulk", "Quicksilver", "Ironman"};

    cout<<"Printing the array as is"<<endl<<endl;

    for (int i=0; i<12; i++){
        cout<<heroes[i]<<endl;
    }

    cout<<endl<<"Ordering the heroes in Alphabetical order"<<endl<<endl;

    char temp = NULL;
    // bubble sort
    for(int i=0;i<11;i++){
        for(int j=0; j<(11-1); j++){
            if (heroes[i][0] < heroes[j][0]){
                for (int k=0; k<17-1; k++){
                    swap(heroes[i][k], heroes[j][k]);
                }
            }
        }
    }

    cout<<"Printing the array Sorted"<<endl<<endl;

    for (int i=0; i<12; i++){
        cout<<heroes[i]<<endl;
    }

    // Pause
    cout<<endl<<endl<<endl<<"Please Close Console Window"<<endl;
    cin.ignore('\n', 1024);
    return(0);
}

好,我知道了!!

http://ideone.com/ugLZ7

这里是代码...(我如何在此表单上发布代码btw?)

Here is the code... (how do i post code on this form btw?)

它几乎完全相同,但使用完整字符串比较和副本。

It is nearly exactly teh same but using complete string comparisons and copies.

推荐答案

您似乎还没有正确理解冒泡排序。首先,您应该只比较相邻的元素,其次,您需要检查第一个字符以外的两个元素是否匹配。我进行了必要的修改,并且正常工作的代码的相关部分为:

You don't seem to have understood bubble-sort correctly. Firstly, you are supposed to be comparing adjacent elements only, and secondly, you need to check beyond the first character if matches for two elements. I made the necessary modifications, and the relevant part of the properly working code is:

int n=11,k,l;
for(int i=0;i<n-1;i++){
    for(int j=0; j<n-i-1; j++){
        l = min(strlen(heroes[j]),strlen(heroes[j+1]));
        for(k=0;k<l;++k)
            if(heroes[j+1][k]<heroes[j][k]){ swap(heroes[j],heroes[j+1]); break; }
            else if(heroes[j+1][k]>heroes[j][k]) break;
        if(k==l and strlen(heroes[j])>strlen(heroes[j+1]))
            swap(heroes[j],heroes[j+1]);
        }
    }

PS:您不需要输出数组使用具有12个迭代的for循环。最后一次迭代只会产生垃圾值。

PS : You don't need to output the array using for loops that have 12 iterations. The last iteration just produces garbage values.

这篇关于按Alpha顺序排序2D Char数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆