2012-03-17 45 views
0

我想按照字母顺序排列名称的二维数组,但我无法缝合以使其工作。按字母顺序排列2D Char阵列?

我在字母上使用了气泡排序,这是对名称的第一个字母进行排序,但是3个名字以相同的字母开始,并且它们仍然没有顺序。

我试图googleing之类的东西,但每婷说,使用向量或字符串变量..但我仅限于使用二维字符数组..

任何想法?

下面是代码我此刻的作品近:

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?)

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

+0

你被允许使用'strcmp'? – 2012-03-17 12:50:40

回答

1

你似乎不明白空气泡排序正确。首先,你应该只是比较相邻的元素,其次,你需要检查超出第一个字符是否匹配两个元素。我做了必要的修改,以及正常工作的代码的相关部分是:

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次迭代的循环你并不需要输出数组。最后一次迭代只会产生垃圾值。

+0

有趣。,。但无法编译:( – aJynks 2012-03-17 13:15:15

+0

你需要包括它的cstring库工作。另外,我假设你有一个正确定义的交换功能,适用于字符串我已经使用它的方式 – 2012-03-17 13:18:05

+0

http:// ideone .COM/GnTPt – 2012-03-17 13:22:11

1

试着依靠标准图书馆来为你做繁重的工作,你正在写的是与std::cout真的C,不鼓励。

#include <vector> 
#include <iostream> 
#include <iterator> 
#include <algorithm> 

int main() 
{ 
    std::vector<std::string> > heroes { 
     "Captain America", "Thor", "Wolverine", "Cyclops", 
     "Goliath", "Beast", "Angel", "Colossus", "Hulk", 
     "Quicksilver", "Ironman" 
    }; 

    std::sort(heroes.begin(), heroes.end()); 

    std::copy(heroes.begin(), heroes.end(), 
     std::ostream_iterator<std::string>(std::cout, ", ")); 
    return 0; 
} 

请注意,如果你没有C,那么++ 11则需要使用手动添加元素的矢量:

std::vector<std::string> > heroes; 
heroes.push_back("Captain America"); 
... 
+0

正如我所说,我被允许使用除cray之外的任何东西 – aJynks 2012-03-17 12:55:46

+0

@aJynks这一定是有史以来最愚蠢的限制。我的同情。 – 2012-03-17 12:57:02

+0

@aJynks你在哪里说的?什么是克雷?如果你不能使用标准库,那么你不写C++。 – 111111 2012-03-17 12:58:56

0

使用STRCMP功能&冒泡排序方法:

char temp[1][17]; 
int size = 11; 
for(int i=1; i<size; i++) 
{ 
    for(int j=0; j<size-i;j++) 
    { 
     if(strcmp(heroes[j],heroes[j+1]) > 0) 
     { 
      strcpy(heroes[0], heroes[j+1]); 
      strcpy(heroes[j+1], heroes[j]); 
      strcpy(heroes[j], heroes[0]); 
     } 
    } 
}