二次配列の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件)
- 最新から表示
- 回答順に表示
No.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)の中ではどうすればいいのでしょうか
aris-wizさん本当にありがとうございます. 大変参考になりました!!
早速やってみます.わからないところありましたらまた聞かせて下さい.助けました^-^.
No.2
- 回答日時:
>上記のプログラムを例にして
うーん、途中までしかない上に、このプログラムって
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次元配列をどのように並べ変えたいの?
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]は同じ機能を働かせるのかわからなくて聞かせていただきました.
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
if と配列の組み合わせ
-
シグマ公式・・・C言語
-
C言語での引数の省略方法
-
std::set<int> で、ある値が何...
-
単語の出現回数を数え、出現回...
-
比較回数と交換回数表示について
-
double型の値を、一桁ずつint型...
-
Cでボリュームコントロールを制...
-
C言語での奇数の和
-
関数について
-
c言語 〇×ゲーム
-
因数分解を行うプログラムについて
-
数字列を3桁ごとにカンマで区切...
-
アスタリスクで正方形
-
関数のコピー
-
ラップ関数とはどんなものですか?
-
剰余演算を論理演算と加減算に...
-
ColorをRGBで指定する方法
-
ゴールドバッハの予想のプログラム
-
VB6.0でコンピュータ名の取得
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
「指定されたキャストは有効で...
-
C言語での引数の省略方法
-
#define _CRT_SECURE_NO_WARNIN...
-
AtCoderABC135の問題Cについて
-
C言語 エラーの原因がわからな...
-
複数桁10進数の*桁目だけを抽出...
-
【C++】関数ポインタの使い方
-
実数の整数部,小数部の取得
-
ラップ関数とはどんなものですか?
-
if と配列の組み合わせ
-
return 1L
-
read関数をノンブロッキングで...
-
(int *)の意味
-
std::set<int> で、ある値が何...
-
Win32APIで作るコンボボックス...
-
C++でvectorにテキストファイル...
-
「{ } で囲むだけ」は正しい?
-
足して100になるような乱数のア...
-
Arduinoのプログラムにエラーが...
-
課題でつまってます・・・
おすすめ情報