2012-03-17 12 views
0

名前の2次元配列をアルファベット順にソートしようとしていますが、動作させるためにシームできません。2次元文字配列をアルファベット順にソートする?

私は文字にバブルソートを使用しています。これは名前の最初の文字をソートしていますが、名前のうち3文字は同じ文字で始まり、順不同です。 ..

任意のアイデアを

は私がgoogleingとか試してみましたが、すべてティンは、ベクトルまたは文字列変数を使用することを言います..しかし、私は2Dのchar配列を使用することに限定されていますか?ここで

はほとんど機能し、現時点で私が持っているコードは次のとおりです。

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); 
} 

[OK]を、私はそれが働いて得ました!ここで

http://ideone.com/ugLZ7

コードは(どのように私はところでこのフォームのコードを投稿できますか?)...である

それはほぼ正確にTEH同じですが、完全な文字列の比較とコピーを使用しています。

+0

あなたは 'strcmp'を使用することを許可されていますか? –

答えて

1

をあなたが理解していないようですbubble-正しく並べ替えます。まず、隣接する要素だけを比較することになっています.2つ目の要素が一致する場合は、最初の文字を超えてチェックする必要があります。私は、必要な修正を行い、正常に動作し、コードの関連部分は次のとおりです。

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

+0

あなたはそれが動作するためにcstringライブラリを含める必要がありますまた、私はあなたがそれを使用した方法で文字列のために働く、適切に定義されたスワップ関数を持っていると仮定しています –

+0

http:// ideone .com/GnTPt –

1

標準ライブラリに頼りにしてみてください。あなたが書いているものは、実際にはstd::coutであり、推奨されません。

#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

私が言ったように、私は何かを使用することを許可されていませんでした。 – aJynks

+0

@aJynksこれはこれまでずっと愚かな制限でなければなりません。私の同情。 –

+0

@aJynksあなたはそれをどこで言ったのですか? &クレイは何ですか?また、標準ライブラリを使用できない場合は、C++を記述していません。 – 111111

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]); 
     } 
    } 
} 
関連する問題