2012-03-17 43 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]); 
     } 
    } 
}