あなたの習慣について教えてください!!

二次配列のqsortについて分かる方に教えて頂きたいのですが

一段落のプログラムを載らせていただきました.count3[j][i]をバブルソートで降順でやってみましたが高速が要求されるため,qsortを使ってやり直したいのですが (ちなみにcount1[j][i],count2[j][i]は前で定義してあります.count4[j][i]にはiの順番を記憶するための二次配列です) 

ぜひともよろしくおねがいします.

int ind_near_search(int j,int t)
{
int i,var_num,count3[IND][VAR],count4[IND][VAR],temp1,temp2,num=0,m=0;

for(i=0;i<VAR;i++){
if(individual[j].x[i]==1){ //変数が1と0の場合分け
count2[j][i]=t-count[j][i];
}else{
count2[j][i]=count[j][i];
}
if(individual[j].x[i]==1){ //全てcount3に値を入れる
count3[j][i]=count2[j][i];
}else{
count3[j][i]=count[j][i];
}
}

for(i=0;i<VAR;i++){

count4[j][i]=num++;
}

for(m=0;m<VAR-1;m++){
for(i=0;i<VAR;i++){ //バブルソートにより降順に並べ換え
if(count3[j][i]<count3[j][i+1]){
temp1=count3[j][i];
count3[j][i]=count3[j][i+1];
count3[j][i+1]=temp1;


temp2=count4[j][i]; //count4にはcount3の並べ替え後の対応する番号を入れる
count4[j][i]=count4[j][i+1];
count4[j][i+1]=temp2;
}
}
}
for(i=0;i<VAR;i++){
var_num=count4[j][i]; //count4の大きい順番からその番号をvar_numに渡す
if(individual[j].x[var_num]==0){//0と1の場合分け
individual[j].x[var_num]=1;
}else{
individual[j].x[var_num]=0;
}

A 回答 (3件)

>qsortにしたらどうやってcount4[j][i]は同じ機能を働かせるのか


なんとなく判りました。
qsortは置換処理をqsort内でやっているので、
今回のような他の配列に対する入れ替えは出来ないでしょう。
クイックソートを自前で実装するかどうしてもqsortの
入れ替えその物を利用したいなら配列を別にするのではなく、
count3とcount4を構造体に纏めれば可能でしょう。

#include <stdio.h>
#include <stdlib.h>
struct hoge{
 int cnt3;
 int cnt4;
};
//比較はcnt3で比較
int compare_int(const struct hoge *a, const struct hoge *b)
{
 return (a->cnt3 - b->cnt3);
}

int main( void )
{
 int i,j;
 struct hoge a[5][5] = {
  {{ 1, 0 },{ 4, 1 },{ 2, 2 },{ 3, 3 },{ 5, 4 }},
  {{ 21, 5 },{ 24, 6 },{ 22, 7 },{ 23, 8 },{ 25, 9 }},
  {{ 31, 10 },{ 34, 11 },{ 32, 12 },{ 33, 13 },{ 35, 14 }},
  {{ 51, 20 },{ 54, 21 },{ 52, 22 },{ 53, 23 },{ 55, 24 }},
  {{ 41, 30 },{ 44, 31 },{ 42, 32 },{ 43, 33 },{ 45, 34 }},
 };

 for ( i = 0; i < 5; i++ ){
  for ( j = 0; j < 5; j ++ ){
   printf( "[%3d:%3d]", a[i][j].cnt3, a[i][j].cnt4 );
  }
  printf( "\n" );
 }
 printf( "\n" );
 for (i = 0; i < 5; i++) {
  qsort(a[i], 5, sizeof(struct hoge), (int (*)(const void*, const void*))compare_int);
 }
 for ( i = 0; i < 5; i++ ){
  for ( j = 0; j < 5; j ++ ){
   printf( "[%3d:%3d]", a[i][j].cnt3, a[i][j].cnt4 );
  }
  printf( "\n" );
 }
 printf( "\n" );
 return 0;
}

この回答への補足

度々申し訳ありませんが
int compare_intは呼び出し関数の中ではどう宣言したらいいのでしょうか よろしくお願いします.例えば上記のint ind_near_search(int j,int t)の中ではどうすればいいのでしょうか

補足日時:2007/11/15 15:57
    • good
    • 0
この回答へのお礼

aris-wizさん本当にありがとうございます. 大変参考になりました!!
早速やってみます.わからないところありましたらまた聞かせて下さい.助けました^-^.

お礼日時:2007/11/14 21:15

>上記のプログラムを例にして


うーん、途中までしかない上に、このプログラムって
qsortとあんまり関係ない気がしますが。。。

#include <stdio.h>
#include <stdlib.h>
int compare_int(const int *a, const int *b)
{
return *a - *b;
}
int main( void )
{
 int i,j;
 int a[5][10] = {
  { 10, 15, 18, 11, 13, 19, 12, 17, 16, 14 },
  { 2, 5, 6, 1, 4, 0, 7, 8, 9, 3 },
  { 33, 34, 35, 37, 32, 30, 31, 38, 36, 39 },
  { 22, 25, 23, 26, 21, 24, 20, 27, 28, 29 },
  { 43, 44, 45, 47, 42, 40, 41, 48, 46, 49 },
 };
 for ( i = 0; i < 5; i++ ){
  for ( j = 0; j < 10; j ++ ){
   printf( "%3d", a[i][j] );
  }
  printf( "\n" );
 }
 printf( "\n" );
 for (i = 0; i < 5; i++) {
  qsort(a[i], 9, sizeof(int), (int (*)(const void*, const void*))compare_int);
 }
 for ( i = 0; i < 5; i++ ){
  for ( j = 0; j < 10; j ++ ){
   printf( "%3d", a[i][j] );
  }
  printf( "\n" );
 }
 printf( "\n" );
 return 0;
}
これでも2次元配列のソートです。
こういうことではなくて??
2次元配列をどのように並べ変えたいの?
    • good
    • 0
この回答へのお礼

aris-wizさん レスありがとうございます.2次配列のqsortへの適用の参考になりましたが 実は上記のプログラムのcount3[j][i]はバブルソートでソートしていて、count3[j][i]ソートされた後のi個変数の順番を記憶するためのcount4[j][i]です.バブルソートだと上の書き方でソートしたあとでも対応するcount3[j][i]のiの番号はcount4[j][i]が記憶できるのですが qsortにしたらどうやってcount4[j][i]は同じ機能を働かせるのかわからなくて聞かせていただきました.

お礼日時:2007/11/14 16:00

>qsortについて分かる方に教えて頂きたいのですが


えっと、ご質問はなんでしょうか?
qsortって標準ライブラリの関数ですが、
何がわからないのでしょうか?

この回答への補足

すいませんが えっと,qsortはどうやって二次配列に適用するのでしょうか 宣言の仕方とかはわからないのですが 上記のプログラムを例にして教えていただければと思いますがよろしくおねがいします.

補足日時:2007/11/14 15:07
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!