ÍêÜÌ bß Ð - 福岡工業大学 · 2014-03-26 · ÍêÜÌ bß ÐÍ ! ß Ð g ^ p ß Ð ÍÂ...
Post on 06-Aug-2020
1 Views
Preview:
TRANSCRIPT
PC
PC
D 5
[1] C
2009
200
Linear Search Sequential Search
(a)
(b)
1
search1
for
for
2
0 i
n i
1 i
search2
for
for
for
search3
3
URL createRandamDigitsFile.zip
createRandamDigitsFile.c dataFile.c dataFile.h
http://goo.gl/q5ITXp
4
Visual Studio->Visual Studio Tools
cl /EHsc createRandamDigitsFile.c dataFile.c
createRandamDigitsFile.c
1365 136500 1365
PC
URL experiment1.zip experiment1.c
measureTime.c measureTime.h dataFile.c dataFile.h
experiment1.c main search1
search.c search.h
http://goo.gl/BCO9kf
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment1.c dataFile.c measureTime.c search.c
experiment11.exe
PC
for
experiment1.c main search2
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment1.c dataFile.c measureTime.c search.c
experiment12.exe
5
PC for
experiment1.c main search3
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment1.c dataFile.c measureTime.c search.c
experiment13.exe
PC
6
PC
7
8
for
9
10
PC
for
n
11
100 for
n
[1] C
pp.64-69 2009
12
/**********************************プログラム概要:探索アルゴリズムライブラリヘッダ作成日/作成者:2011.4.26/杉田 薫**********************************/#ifndef _search_H#define _search_H/**********************************関数名:int search1( const int a[], int n, int key)機能:要素数nの配列aからkeyと一致する要素を線形探索入力:配列( const int a[] ) 要素数(int n) 探索キー(int key)出力:なし返却値:探索キーと一致する要素の位置(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int search1( const int a[], int n, int key);
/**********************************関数名:int search2( const int a[], int n, int key)機能:要素数nの配列aからkeyと一致する要素を線形探索(第2版)入力:配列( const int a[] ) 要素数(int n) 探索キー(int key)出力:なし返却値:探索キーと一致する要素の位置(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int search2( const int a[], int n, int key);
/**********************************関数名:int search3( int a[], int n, int key)機能:要素数nの配列aからkeyと一致する要素を線形探索(番兵法)入力:配列( int a[] ) 要素数(int n) 探索キー(int key)出力:なし返却値:探索キーと一致する要素の位置(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int search3( int a[], int n, int key);
#endif /* _search_H */
1123456789
10111213141516171819202122232425262728293031323334353637383940414243
search.h
13
/**********************************プログラム概要:探索アルゴリズムライブラリ作成日/作成者:2011.4.26/杉田 薫**********************************/#include<stdio.h>#include "search.h"
/**********************************関数名:int search1( const int a[], int n, int key)機能:要素数nの配列aからkeyと一致する要素を線形探索入力:配列( const int a[] ) 要素数(int n) 探索キー(int key)出力:なし返却値:探索キーと一致する要素の位置(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int search1( const int a[], int n, int key){ /* 変数宣言 */ int i = 0; /* ループ用変数 */ while( 1 ) { if( i == n ) return -1; if( a[i] == key ) return i; i++; } /* End of while */ } /* End of Function search1 */
/**********************************関数名:int search2( const int a[], int n, int key)機能:要素数nの配列aからkeyと一致する要素を線形探索(第2版)入力:配列( const int a[] ) 要素数(int n) 探索キー(int key)出力:なし返却値:探索キーと一致する要素の位置(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int search2( const int a[], int n, int key){ /* 変数宣言 */ int i; /* ループ用変数 */ for( i = 0; i < n; i++ ) if( a[i] == key ) return i; return -1;} /* End of Function search2 */
/**********************************関数名:int search3( int a[], int n, int key)機能:要素数nの配列aからkeyと一致する要素を線形探索(番兵法)入力:配列( int a[] ) 要素数(int n)
1123456789
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
search.c
14
探索キー(int key)出力:なし返却値:探索キーと一致する要素の位置(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int search3( int a[], int n, int key){ /* 変数宣言 */ int i = 0; /* ループ用変数 */ if( a[n-1] == key ) return n -1; else a[n-1] = key; while( 1 ) { if(a[i] == key ) break; i++; } /* End of while */ return ( (i == n - 1) ? -1: i);} /* End of Function search3 */
26263646566676869707172737475767778798081828384858687
search.c
15
Binary Search
a
b
16
bin_search 2
17
#
undef _EX_2 # define _EX_2
URL createRandamDigitsFile.zip
createRandamDigitsFile.c dataFile.c dataFile.h
createRandamDigitsFile.c # undef _EX_2
# define _EX_2
http://goo.gl/q5ITXp
Visual Studio->Visual Studio Tools
cl /EHsc createRandamDigitsFile.c dataFile.c
createRandamDigitsFile.c
1365 136500 1365
PC
URL experiment2.zip experiment21.c
experiment22.c measureTime.c measureTime.h dataFile.c dataFile.h
search.c search.h
bin_search int_cmp
http://goo.gl/HUAABd
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment21.c dataFile.c measureTime.c search.c
experiment21.exe
PC
18
s
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment22.c dataFile.c measureTime.c search.c
experiment22.exe
PC
s
19
PC
20
s
21
s
22
PC
n
n
n
23
100
n
[1] C
pp.70-83 2009
24
/**********************************プログラム概要:探索アルゴリズムライブラリヘッダ作成日/作成者:2011.4.26/杉田 薫**********************************/#ifndef _search_H#define _search_H
/**********************************関数名:int bin_search( const int a[], int n, int key)機能:要素数nの配列aからkeyと一致する要素を2分探索入力:配列( const int a[] ) 要素数(int n) 探索キー(int key)出力:なし返却値:探索キーと一致する要素の位置(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int bin_search( const int a[], int n, int key);
/**********************************関数名:int int_cmp( const int *a, const int *b)機能:要素数nの配列aからkeyと一致する要素を2分探索入力:整数( const int *a ) 整数(const int *b)出力:なし返却値:処理結果(int)備考:bsearch関数,qsort関数で利用作成日/作成者:2011.4.26/杉田 薫**********************************/int int_cmp( const int *a, const int *b);
#endif /* _search_H */
1123456789
1011121314151617181920212223242526272829303132
search.h
25
/**********************************プログラム概要:探索アルゴリズムライブラリ作成日/作成者:2011.4.26/杉田 薫**********************************/#include<stdio.h>#include "search.h"
/**********************************関数名:int bin_search( const int a[], int n, int key)機能:要素数nの配列aからkeyと一致する要素を2分探索入力:配列( int a[] ) 要素数(int n) 探索キー(int key)出力:なし返却値:探索キーと一致する要素の位置(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int bin_search( const int a[], int n, int key){ /* 変数宣言 */ int pl = 0; /* 探索範囲先頭の添字 */ int pr = n - 1; /* 末尾の添字 */ int pc; do{ pc = ( pl + pr ) / 2; if( a[pc] == key ) /* 探索成功 */ return pc; else if( a[pc] < key ) pl = pc +1; else pr = pc - 1; } while( pl <= pr ); return -1;} /* End of Function bin_search */
/**********************************関数名:int int_cmp( const int *a, const int *b)機能:2変数の大きさを比較(qsort用)入力:整数( const int *a ) 整数(const int *b)出力:なし返却値:処理結果(int)備考:bsearch関数,qsort関数で利用作成日/作成者:2011.4.26/杉田 薫**********************************/int int_cmp( const int *a, const int *b){ if( *a > *b ) return 1; else if( *a < *b ) return -1; else return 0;} /* End of Function int_cmp */
1123456789
101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
search.c
26
Hashing
Chaining Open Addressing
Open Hashing
27
a
b
c
Closed Hashing
28
a
b
c
29
30
a
i=0
i<size
i++
b
p!=0
c
p!=0
d
p!=0
a
b
c
d
31
a
b
c
d
32
a
i=0
i<size
i++
b
i=0
p->stat != Empty && i < h->size
i++
c
i=0
i<size
i++
33
a
b
c
d
34
a
b
c
d
35
URL createRandamDigitsFile.zip
createRandamDigitsFile.c dataFile.c dataFile.h
http://goo.gl/q5ITXp
Visual Studio->Visual Studio Tools
cl /EHsc createRandamDigitsFile.c dataFile.c
createRandamDigitsFile.c
1365 136500 1365
PC
URL experiment3.zip experiment31.c
experiment32.c measureTime.c measureTime.h dataFile.c dataFile.h
hash31.c hash31.h
http://goo.gl/psaoJP
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment31.c dataFile.c measureTime.c hash31.c
experiment31.exe
PC
s
36
hash32.c hash32.h
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment32.c dataFile.c measureTime.c hash32.c
experiment32.exe
PC
s
37
PC
38
s
39
s
40
n
n
41
100
Chaining Open Addressing
[1] C
pp.84-97 2009
42
/**********************************プログラム概要:ハッシュによる探索アルゴリズムライブラリヘッダファイル名:hash31.h作成日/作成者:2011.4.27/杉田 薫**********************************/#ifndef _hash_H#define _hash_H
/*** プロトタイプ宣言 ***//* 定数定義 */#define NO 1 /* 番号(入力用)) */#define NAME 2 /* 氏名(入力用)) */
/* メニュー識別用 */typedef enum{ Term, Insert, Delete, SrchNo, Dump} Menu;
/* 構造体定義 *//* データ */typedef struct{ int no; /* 番号 */ char name[10]; /* 氏名 */} Data;
/* ノード */typedef struct __node{ Data data; /* データ */ struct __node *next; /* 後続ノードのポインタ */} Node;
/* ハッシュ表 */typedef struct{ int size; /* ハッシュ表の大きさ */ Node **table; /* ハッシュ表の先頭要素へのポインタ */} Hash;
/**********************************関数名:int hash( int key, int num )機能:ハッシュ関数(keyのハッシュ値を返す)入力:key( int key) 基準値(int num)出力:なし返却値:ハッシュ値(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数に基準値を追加**********************************/int hash( int key, int num );
/**********************************関数名:void SetNode( Node *n, Data x, Node *next)機能:各ノードのメンバ変数に値を設定入力:ノード(Node *n) データ(Data x) 後続ノードへのポインタ(Node *next)出力:なし返却値:なし
1123456789
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
hash31.h
43
作成日/作成者:2011.4.26/杉田 薫**********************************/void SetNode( Node *n, Data x, Node *next);
/**********************************関数名:int InitHash( Hash *h, int size )機能:ハッシュ表の初期化入力:ハッシュ表( Hash *h ) ハッシュ表の大きさ(int size)出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int InitHash( Hash *h, int size );
/**********************************関数名:void TermHash( Hash *h )機能:ハッシュ表の解放入力:ハッシュ表(Hash *h)出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void TermHash( Hash *h );
/**********************************関数名:Node *SearchNode( Hash *h, Data x )機能:データ探索入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:探索結果(Node *)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/Node *SearchNode( Hash *h, Data x );
/**********************************関数名:int InsertNode( Hash *h, Data x )機能:データ追加入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/int InsertNode( Hash *h, Data x );
/**********************************関数名:int DeleteNode( Hash *h, Data x )機能:データ削除入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/int DeleteNode( Hash *h, Data x );
26263646566676869707172737475767778798081828384858687888990919293949596979899
100101102103104105106107108109110111112113114115116117118119120121122
hash31.h
44
/**********************************関数名:void DumpHash( Hash *h )機能:ハッシュ表のダンプ入力:ハッシュ表( Hash *h )出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void DumpHash( Hash *h );
/**********************************関数名:void PrintData( Data x )機能:データ表示入力:データ( Data x )出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void PrintData( Data x );
/**********************************関数名:Data Read( char *message, int sw )機能:データ入力入力:データ入力時に表示する操作名( char *message ) メッセージの表示フラグ(int sw)出力:なし返却値:データ(Data)作成日/作成者:2011.4.26/杉田 薫**********************************/Data Read( char *message, int sw );
/**********************************関数名:Menu SelectMenu( void )機能:メニュー選択入力:なし出力:なし返却値:メニュー識別子(Menu)作成日/作成者:2011.4.26/杉田 薫**********************************/Menu SelectMenu( void );
/**********************************関数名:int DataFileRead( char *filename, Hash *h )機能:ハッシュ表にファイルからデータを読み込む入力:ファイル名(char *filename) ハッシュ表( Hash *h )出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考:テキスト以外の関数を追加**********************************/int DataFileRead( char *filename, Hash *h );
#endif /* _hash_H */
3123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
hash31.h
45
/**********************************プログラム概要:探索アルゴリズムライブラリ作成日/作成者:2011.4.26/杉田 薫**********************************/#include<stdio.h>#include<stdlib.h>#include <time.h>#include "hash31.h"
/**********************************関数名:int hash( int key, int num )機能:ハッシュ関数(keyのハッシュ値を返す)入力:key( int key) 基準値(int num)出力:なし返却値:ハッシュ値(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数に基準値を追加**********************************/int hash( int key, int num ){ return (key % num);} /* End of Function hash */
/**********************************関数名:void SetNode( Node *n, Data x, Node *next)機能:各ノードのメンバ変数に値を設定入力:ノード(Node *n) データ(Data x) 後続ノードへのポインタ(Node *next)出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void SetNode( Node *n, Data x, Node *next){ n->data = x; n->next = next;} /* End of Function SetNode */
/**********************************関数名:int InitHash( Hash *h, int size )機能:ハッシュ表の初期化入力:ハッシュ表( Hash *h ) ハッシュ表の大きさ(int size)出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int InitHash( Hash *h, int size ){ /* 変数宣言 */ int i; /* ループ用変数 */ h->size = 0; if( ( h->table = calloc( size, sizeof( Node *) ) ) == NULL ) return 0; h->size = size; for( i = 0; i < size; i++ ) h->table[i] = NULL;
1123456789
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
hash31.c
46
return 1;} /* End of Function InitHash */
/**********************************関数名:void TermHash( Hash *h )機能:ハッシュ表の解放入力:ハッシュ表(Hash *h)出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void TermHash( Hash *h ){ /* 変数宣言 */ int i; /* ループ用変数 */ Node *p; Node *next; for( i = 0 ; i < h->size ; i++ ) { p = h->table[ i ]; while( p != NULL ) { next = p->next; free( p ); p = next; } /* End of while */ } /* End of for i */ free( h->table ); } /* End of Function TermHash */
/**********************************関数名:Node *SearchNode( Hash *h, Data x )機能:データ探索入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:探索結果(Node *)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/Node *SearchNode( Hash *h, Data x ){ /* 変数宣言 */ int key = hash( x.no, h->size ); /* 探索データのハッシュ値 */ Node *p = h->table[ key ]; /* ノード */ while( p !=NULL ) { if( p->data.no == x.no ) return p; p = p->next; } /* End of while p */ return NULL;} /* End of Function SearchNode */
/**********************************関数名:int InsertNode( Hash *h, Data x )
26263646566676869707172737475767778798081828384858687888990919293949596979899
100101102103104105106107108109110111112113114115116117118119120121122
hash31.c
47
機能:データ追加入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/int InsertNode( Hash *h, Data x ){ /* 変数宣言 */ int key = hash(x.no, h->size ); /* 追加するデータのハッシュ値 */ Node *p = h->table[ key ]; /* ノード */ Node *temp; while( p != NULL ) { /* 登録済みキーチェック */ if( p->data.no == x.no ) return 1; p = p->next; } /* End of while p */ if( ( temp = ( Node *)calloc( 1, sizeof( Node ) ) )== NULL ) return 2; /* 追加ノードに値を設定 */ SetNode( temp, x, h->table[ key ] ); h->table[ key ] = temp; return 0;} /* End of Function InsertNode */
/**********************************関数名:int DeleteNode( Hash *h, Data x )ファイル名:hash31.h機能:データ削除入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/int DeleteNode( Hash *h, Data x ){ /* 変数宣言 */ int key = hash( x.no, h->size ); /* 削除するデータのハッシュ値 */ Node *p = h->table[ key ]; /* ノード */ Node **pp = &h->table[ key ]; /* ノードのポインタ */ while( p != NULL ) { if( p->data.no == x.no ) { *pp = p->next; free( p ); return 0; } pp = &p->next;
3123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
hash31.c
48
p = p->next; } /* End of while p */
return 1;} /* End of Function InsertNode */
/**********************************関数名:void DumpHash( Hash *h )機能:ハッシュ表のダンプ入力:ハッシュ表( Hash *h )出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void DumpHash( Hash *h ){ /* 変数宣言 */ int i; /* ループ用 */ Node *p; /* ノード */ for( i = 0; i < h->size; i++ ) { p = h->table[ i ]; printf("%02d: ", i ); while( p != NULL ) { printf("-> %d (%s)\t", p->data.no, p->data.name); p = p->next; } /* End of while p */ putchar('\n'); } /* End of for i */} /* End of Function DumpHash */
/**********************************関数名:void PrintData( Data x )機能:データ表示入力:データ( Data x )出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void PrintData( Data x ){ printf("-> %d %s\n ", x.no, x.name);} /* End of Function PrintData */
/**********************************関数名:Data Read( char *message, int sw )機能:データ入力入力:データ入力時に表示する操作名( char *message ) メッセージの表示フラグ(int sw)出力:なし返却値:データ(Data)作成日/作成者:2011.4.26/杉田 薫**********************************/Data Read( char *message, int sw ){ Data temp;
4184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
hash31.c
49
printf("%sするデータを入力して下さい", message ); if( sw & NO ) { printf("番号:"); scanf("%d", &temp.no ); } if( sw & NAME ) { printf("名前:"); scanf("%s", temp.name ); } return temp;} /* End of Function Read */
/**********************************関数名:Menu SelectMenu( void )機能:メニュー選択入力:なし出力:なし返却値:メニュー識別子(Menu)作成日/作成者:2011.4.26/杉田 薫備考:ファイル読込を追加**********************************/Menu SelectMenu( void ){ /* 変数定義 */ int i; int ch; do{ printf("(1)追加 (2)削除 (3)探索 (4)ダンプ (0)終了:"); scanf("%d", &ch ); } while( ch < Term || ch > Dump );
return ((Menu)ch);} /* End of Function SelectMenu */
/**********************************関数名:int DataFileRead( char *filename, Hash *h )機能:ハッシュ表にファイルからデータを読み込む入力:ファイル名(char *filename) ハッシュ表( Hash *h )出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考:テキスト以外の関数を追加**********************************/int DataFileRead( char *filename, Hash *h ){ /* 変数定義 */ FILE *fp; /* ファイルポインタ */ int dummy; /* 乱数の個数を確認するためのダミー変数 */ Data x; /* データ */ int num =0; /* データ個数 */ int i; /* ループ用 */ int j; /* ループ用 */ int result; /* 処理結果 */
5245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
hash31.c
50
/* 乱数の初期化 */ srand((unsigned)time(NULL));
/* 処理内容の表示 */ printf("%sからデータを読み込みます\n", filename); /* データ数の初期化 */ num = 0; /* ファイルオープン */ fp = NULL; fp = fopen(filename, "r"); if( fp == NULL ) { printf("ファイルオープンに失敗しました\n"); return -1; } /* データ個数の確認 */ while( fscanf(fp, "%d", &dummy) != EOF ) { num++; } /* End of while */ /* 乱数の個数の表示 */ printf("%d個のデータを読み込みます。\n", num); /* ファイルポインタを先頭に戻す */ rewind( fp ); /* ハッシュ表の初期化 */ InitHash( h, num); for( i = 0; i < num; i++ ) { /* ファイルからデータを読み込む */ fscanf(fp, "%d", &x.no ); /* 名前を乱数で生成 */ for( j = 0 ; j < 10 ; j++ ) { x.name[j] = 0x41 + rand() % 24; } /* End of for j */ /* ハッシュ表にデータを追加 */ result = InsertNode( h, x ); if( result ) { printf("追加に失敗しました(%s)", (result == 1) ? "登録済" : "メモリ不足"); /* ファイルクローズ */ fclose(fp); fp = NULL; return -1; } } /* End of for i */
6306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
hash31.c
51
/* ファイルクローズ */ fclose(fp); fp = NULL;
return 0;} /* End of Function FileRead */
7367368369370371372373374375
hash31.c
52
/**********************************プログラム概要:ハッシュによる探索アルゴリズムライブラリヘッダファイル名:hash31.h作成日/作成者:2011.4.27/杉田 薫**********************************/#ifndef _hash_H#define _hash_H
/*** プロトタイプ宣言 ***//* 定数定義 */#define NO 1 /* 番号(入力用)) */#define NAME 2 /* 氏名(入力用)) */
/* メニュー識別用 */typedef enum{ Term, Insert, Delete, SrchNo, Dump} Menu;
/* 要素の状態 */typedef enum{Occupied, Empty, Deleted} Status;
/* 構造体定義 *//* データ */typedef struct{ int no; /* 番号 */ char name[10]; /* 氏名 */} Data;
/* 要素 */typedef struct{ Data data; /* データ */ Status stat; /* 要素の状態 */} Bucket;
/* ハッシュ表 */typedef struct{ int size; /* ハッシュ表の大きさ */ Bucket *table; /* ハッシュ表の先頭要素へのポインタ */} Hash;
/**********************************関数名:int hash( int key, int num )機能:ハッシュ関数(keyのハッシュ値を返す)入力:key( int key) 基準値(int num)出力:なし返却値:ハッシュ値(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数に基準値を追加**********************************/int hash( int key, int num );
1123456789
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
hash32.h
53
/**********************************関数名:int rehash( int key, int num )機能:再ハッシュ関数(keyのハッシュ値を返す)入力:key( int key) 基準値(int num)出力:なし返却値:ハッシュ値(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数に基準値を追加**********************************/int rehash( int key, int num );
/**********************************関数名:void SetBucket( Bucket *n, Data x, Status stat )機能:ノードの各メンバに値を設定入力:要素( Bucket *n ) データ( Data x ) 要素の状態( Status stat )出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数に基準値を追加**********************************/void SetBucket( Bucket *n, Data x, Status stat );
/**********************************関数名:int InitHash( Hash *h, int size )機能:ハッシュ表の初期化入力:ハッシュ表( Hash *h ) ハッシュ表の大きさ(int size)出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫**********************************/int InitHash( Hash *h, int size );
/**********************************関数名:void TermHash( Hash *h )機能:ハッシュ表の解放入力:ハッシュ表(Hash *h)出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void TermHash( Hash *h );
/**********************************関数名:Bucket *SearchBucket( Hash *h, Data x )機能:データ探索入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:探索結果(Bucket *)作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/Bucket *SearchBucket( Hash *h, Data x );
/**********************************関数名:int InsertBucket( Hash *h, Data x )機能:データ追加
26263646566676869707172737475767778798081828384858687888990919293949596979899
100101102103104105106107108109110111112113114115116117118119120121122
hash32.h
54
入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:処理結果(int)作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/int InsertBucket( Hash *h, Data x );
/**********************************関数名:int DeleteBucket( Hash *h, Data x )機能:データ削除入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:処理結果(int)作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/int DeleteBucket( Hash *h, Data x );
/**********************************関数名:void DumpHash( Hash *h )機能:ハッシュ表のダンプ入力:ハッシュ表( Hash *h )出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void DumpHash( Hash *h );
/**********************************関数名:void PrintData( Data x )機能:データ表示入力:データ( Data x )出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void PrintData( Data x );
/**********************************関数名:Data Read( char *message, int sw )機能:データ入力入力:データ入力時に表示する操作名( char *message ) メッセージの表示フラグ(int sw)出力:なし返却値:データ(Data)作成日/作成者:2011.5.4/杉田 薫**********************************/Data Read( char *message, int sw );
/**********************************関数名:Menu SelectMenu( void )機能:メニュー選択入力:なし出力:なし返却値:メニュー識別子(Menu)作成日/作成者:2011.4.26/杉田 薫**********************************/Menu SelectMenu( void );
3123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
hash32.h
55
/**********************************関数名:int DataFileRead( char *filename, Hash *h )機能:ハッシュ表にファイルからデータを読み込む入力:ファイル名(char *filename) ハッシュ表( Hash *h )出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考:テキスト以外の関数を追加**********************************/int DataFileRead( char *filename, Hash *h );
#endif /* _hash_H */
4184185186187188189190191192193194195196197
hash32.h
56
/**********************************プログラム概要:探索アルゴリズムライブラリ作成日/作成者:2011.4.26/杉田 薫**********************************/#include<stdio.h>#include<stdlib.h>#include <time.h>#include "hash32.h"
/**********************************関数名:int hash( int key, int num )機能:ハッシュ関数(keyのハッシュ値を返す)入力:key( int key) 基準値(int num)出力:なし返却値:ハッシュ値(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数に基準値を追加**********************************/int hash( int key, int num ){ return (key % num);} /* End of Function hash */
/**********************************関数名:int rehash( int key, int num )機能:再ハッシュ関数(keyのハッシュ値を返す)入力:key( int key) 基準値(int num)出力:なし返却値:ハッシュ値(int)作成日/作成者:2011.4.26/杉田 薫備考:テキストの関数に基準値を追加**********************************/int rehash( int key, int num ){ return (( key + 1 ) % num);} /* End of Function rehash */
/**********************************関数名:void SetBucket( Bucket *n, Data x, Status stat )機能:ノードの各メンバに値を設定入力:要素( Bucket *n ) データ( Data x ) 要素の状態( Status stat )出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数に基準値を追加**********************************/void SetBucket( Bucket *n, Data x, Status stat ){ n->data = x; /* データ */ n->stat =stat; /* 要素の状態 */} /* End of Function SetBucket */
/**********************************関数名:int InitHash( Hash *h, int size )機能:ハッシュ表の初期化入力:ハッシュ表( Hash *h ) ハッシュ表の大きさ(int size)
1123456789
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
hash32.c
57
出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考: "h->table[i].stat"を"h->table[i]->stat"に訂正**********************************/int InitHash( Hash *h, int size ){ /* 変数宣言 */ int i; /* ループ用変数 */ h->size = 0; if( ( h->table = calloc( size, sizeof( Bucket) ) ) == NULL ) return 0; h->size = size; for( i = 0; i < size; i++ ){ h->table[i].stat = Empty;} return 1;} /* End of Function InitHash */
/**********************************関数名:void TermHash( Hash *h )機能:ハッシュ表の解放入力:ハッシュ表(Hash *h)出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void TermHash( Hash *h ){ free( h->table ); } /* End of Function TermHash */
/**********************************関数名:Bucket *SearchBucket( Hash *h, Data x )機能:データ探索入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:探索結果(Bucket *)作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/Bucket *SearchBucket( Hash *h, Data x ){ /* 変数宣言 */ int i; /* ループ用 */ int key = hash( x.no, h->size ); /* 探索データのハッシュ値 */ Bucket *p = &h->table[ key ]; /* 要素 */ for( i = 0; p->stat != Empty && i < h->size; i++ ) { if( p->stat == Occupied && p->data.no == x.no ) return p; key = rehash( key, h->size ); p = &h->table[key]; } /* End of for i */ return NULL;} /* End of Function SearchBucket */
26263646566676869707172737475767778798081828384858687888990919293949596979899
100101102103104105106107108109110111112113114115116117118119120121122
hash32.c
58
/**********************************関数名:int InsertBucket( Hash *h, Data x )機能:データ追加入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:処理結果(int)作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/int InsertBucket( Hash *h, Data x ){ /* 変数宣言 */ int i; /* ループ用変数 */ int key = hash(x.no, h->size ); /* 追加するデータのハッシュ値 */ Bucket *p = &h->table[ key ]; /* 要素 */ /* キーの登録済みチェック */ if( SearchBucket( h, x ) ) return 1; for( i = 0; i < h->size; i++ ) { if( (p->stat == Empty) || (p->stat == Deleted) ) { SetBucket( p, x, Occupied ); return 0; } key = rehash( key, h->size ); p = &h->table[ key ]; } /* End of for i */ /* ハッシュが満杯 */ return 2;} /* End of Function InsertBucket */
/**********************************関数名:int DeleteBucket( Hash *h, Data x )機能:データ削除入力:ハッシュ表( Hash *h ) データ(Data x)出力:なし返却値:処理結果(int)作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数hashに引数h->sizeを追加**********************************/int DeleteBucket( Hash *h, Data x ){ /* 変数宣言 */ Bucket *p = SearchBucket( h, x ); /* 要素 */ /* 要素の有効性チェック */ if( p == NULL ) return 1; /* 要素が存在しない */ /* 対象要素の状態を"Deleted"に設定 */ p->stat = Deleted; return 0;
3123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
hash32.c
59
} /* End of Function DeleteBucket */
/**********************************関数名:void DumpHash( Hash *h )機能:ハッシュ表のダンプ入力:ハッシュ表( Hash *h )出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void DumpHash( Hash *h ){ /* 変数宣言 */ int i; /* ループ用 */ for( i = 0; i < h->size; i++ ) { printf("%02d: ", i ); switch( h->table[i].stat ) { case Occupied: printf("%d (%s)\n", h->table[i].data.no, h->table[i].data.name); break; case Empty: printf("-- 未登録 --\n"); break; case Deleted: printf("-- 削除済 --\n"); break; } } /* End of for i */} /* End of Function DumpHash */
/**********************************関数名:void PrintData( Data x )機能:データ表示入力:データ( Data x )出力:なし返却値:なし作成日/作成者:2011.4.26/杉田 薫**********************************/void PrintData( Data x ){ printf("%d %s\n ", x.no, x.name);} /* End of Function PrintData */
/**********************************関数名:Data Read( char *message, int sw )機能:データ入力入力:データ入力時に表示する操作名( char *message ) メッセージの表示フラグ(int sw)出力:なし返却値:データ(Data)作成日/作成者:2011.4.26/杉田 薫**********************************/Data Read( char *message, int sw ){ Data temp;
4184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
hash32.c
60
printf("%sするデータを入力して下さい", message ); if( sw & NO ) { printf("番号:"); scanf("%d", &temp.no ); } if( sw & NAME ) { printf("名前:"); scanf("%s", temp.name ); } return temp;} /* End of Function Read */
/**********************************関数名:Menu SelectMenu( void )機能:メニュー選択入力:なし出力:なし返却値:メニュー識別子(Menu)作成日/作成者:2011.4.26/杉田 薫備考:ファイル読込を追加**********************************/Menu SelectMenu( void ){ /* 変数定義 */ int i; int ch; do{ printf("(1)追加 (2)削除 (3)探索 (4)ダンプ (0)終了:"); scanf("%d", &ch ); } while( ch < Term || ch > Dump );
return ((Menu)ch);} /* End of Function SelectMenu */
/**********************************関数名:int DataFileRead( char *filename, Hash *h )機能:ハッシュ表にファイルからデータを読み込む入力:ファイル名(char *filename) ハッシュ表( Hash *h )出力:なし返却値:処理結果(int)作成日/作成者:2011.4.26/杉田 薫備考:テキスト以外の関数を追加**********************************/int DataFileRead( char *filename, Hash *h ){ /* 変数定義 */ FILE *fp; /* ファイルポインタ */ int dummy; /* 乱数の個数を確認するためのダミー変数 */ Data x; /* データ */ int num =0; /* データ個数 */ int i; /* ループ用 */ int j; /* ループ用 */
5245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
hash32.c
61
int result; /* 処理結果 */
/* 乱数の初期化 */ srand((unsigned)time(NULL));
/* 処理内容の表示 */ printf("%sからデータを読み込みます\n", filename); /* データ数の初期化 */ num = 0; /* ファイルオープン */ fp = NULL; fp = fopen(filename, "r"); if( fp == NULL ) { printf("ファイルオープンに失敗しました\n"); return -1; } /* データ個数の確認 */ while( fscanf(fp, "%d", &dummy) != EOF ) { num++; } /* End of while */ /* 乱数の個数の表示 */ printf("%d個のデータを読み込みます。\n", num); /* ファイルポインタを先頭に戻す */ rewind( fp ); /* ハッシュ表の初期化 */ InitHash( h, num); for( i = 0; i < num; i++ ) { /* ファイルからデータを読み込む */ fscanf(fp, "%d", &x.no ); /* 名前を乱数で生成 */ for( j = 0 ; j < 10 ; j++ ) { x.name[j] = 0x41 + rand() % 24; } /* End of for j */ /* ハッシュ表にデータを追加 */ result = InsertBucket( h, x ); if( result ) { printf("追加に失敗しました(%s)", (result == 1) ? "登録済" : "メモリ不足"); /* ファイルクローズ */ fclose(fp); fp = NULL; return -1; }
6306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
hash32.c
62
} /* End of for i */ /* ファイルクローズ */ fclose(fp); fp = NULL;
return 0;} /* End of Function FileRead */
7367368369370371372373374375376
hash32.c
63
Babble Sort
a
b
2
bubble1
64
a
i=0
i<n-1
i++
b
j=n-1
j > i
j--
65
bubble2
a
i=0
i<n-1
i++
b
j=n-1
j > i
j--
66
bubble3
a
k<n-1
b
j=n-1
j > k
j--
67
68
URL createRandamDigitsFile.zip
createRandamDigitsFile.c dataFile.c dataFile.h
createRandamDigitsFile.c # define _EX_2
# undef _EX_2
http://goo.gl/q5ITXp
Visual Studio->Visual Studio Tools
cl /EHsc createRandamDigitsFile.c dataFile.c
createRandamDigitsFile.c
1365 136500 1365
PC
URL experiment4.zip experiment4.c
measureTime.c measureTime.h dataFile.c dataFile.h
http://goo.gl/2BSDza
sort.c sort.h experiment4.c main
bubble1
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment4.c dataFile.c measureTime.c sort.c
experiment41.exe
PC experiment41.exe
experiment41.exe
69
s
experiment4.c main bubble1 bubble2
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment4.c dataFile.c measureTime.c sort.c
experiment42.exe
PC experiment42.exe
experiment42.exe
s
experiment4.c main bubble1 bubble2
bubble3
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment4.c dataFile.c measureTime.c sort.c
experiment43.exe
PC experiment43.exe
experiment43.exe
s
70
PC
71
s
72
s
73
s
74
n
[1] C
pp.154-161 2009
75
/**********************************プログラム概要:ソートアルゴリズムライブラリヘッダ作成日/作成者:2011.5.4/杉田 薫**********************************/#ifndef _sort_H#define _sort_H
/**********************************関数名:void bubble1( int a[], int n )機能:バブルソート入力:配列( int a[] ) 要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void bubble1( int a[], int n );
/**********************************関数名:void bubble2( int a[], int n )機能:バブルソート(第2版:交換回数による打ち切り)入力:配列( int a[] ) 要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void bubble2( int a[], int n );
/**********************************関数名:void bubble3( int a[], int n )機能:バブルソート(第3版:操作範囲を限定)入力:配列( int a[] ) 要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void bubble3( int a[], int n );
#endif /* _sort_H */
1123456789
1011121314151617181920212223242526272829303132333435363738394041
sort.h
76
/**********************************プログラム概要:ソートアルゴリズムライブラリ作成日/作成者:2011.5.4/杉田 薫**********************************/#include<stdio.h>#include<stdlib.h>#include <math.h>#include "sort.h"#include "dataFile.h"
/* マクロ関数定義 */#define swap( type, x, y ) do { type t = x; x = y; y = t; } while( 0 )
/**********************************関数名:void bubble1( int a[], int n )機能:バブルソート入力:配列( int a[] ) 要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void bubble1( int a[], int n ){ /* 変数宣言 */ int i; /* ループ用変数 */ int j; /* ループ用変数 */ for( i = 0; i < n - 1; i++ ) { for( j = n -1 ; j > i ; j-- ) if( a[ j - 1 ] > a[ j ] ) swap( int, a[ j - 1], a[ j ] ); } /* End of for i */ } /* End of Function bubble1 */
/**********************************関数名:void bubble2( int a[], int n )機能:バブルソート(第2版:交換回数による打ち切り)入力:配列( const int a[] ) 要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void bubble2( int a[], int n ){ /* 変数宣言 */ int i; /* ループ用変数 */ int j; /* ループ用変数 */ int exchg; /* 交換回数 */ for( i = 0; i < n - 1; i++ ) { exchg = 0; for( j = n -1 ; j > i ; j-- ) if( a[ j - 1 ] > a[ j ] ){ swap(int, a[ j - 1], a[ j ] ); exchg++; }
1123456789
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
sort.c
77
/* 交換が実行されなかった場合は終了 */ if( exchg ==0 ) break; } /* End of for i */ } /* End of Function bubble2 */
/**********************************関数名:void bubble3( int a[], int n )機能:バブルソート(第3版:操作範囲を限定)入力:配列( int a[] ) 要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void bubble3( int a[], int n ){ /* 変数宣言 */ int k = 0; /* ソート済範囲 */ int j; /* ループ用変数 */ int last; /* 最後に交換した位置 */ while( k < n - 1 ) { last = n - 1; for( j = n -1 ; j > k ; j-- ) if( a[ j - 1 ] > a[ j ] ){ swap(int, a[ j - 1], a[ j ] ); last = j; } k = last; } /* End of while k */ } /* End of Function bubble3 */
26263646566676869707172737475767778798081828384858687888990919293949596979899
sort.c
78
Selection Sort
selection
79
a
i=0
i<n-1
i++
b
j=i+1
j < n
j++
80
URL createRandamDigitsFile.zip
createRandamDigitsFile.c dataFile.c dataFile.h
createRandamDigitsFile.c # define _EX_2
# undef _EX_2
http://goo.gl/q5ITXp
Visual Studio->Visual Studio Tools
cl /EHsc createRandamDigitsFile.c dataFile.c
createRandamDigitsFile.c
PC
URL experiment5.zip experiment5.c
measureTime.c measureTime.h dataFile.c dataFile.h
http://goo.gl/xOfQWU
sort.c sort.h
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment5.c dataFile.c measureTime.c sort.c
experiment5.exe
PC experiment5.exe
experiment5.exe
81
s
PC
82
s
83
[1] C
pp.162-167 2009
84
/**********************************プログラム概要:ソートアルゴリズムライブラリヘッダ作成日/作成者:2011.5.4/杉田 薫**********************************/#ifndef _sort_H#define _sort_H
/**********************************関数名:void selection( int a[], int n )機能:単純選択ソート入力:配列( int a[] ) 要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void selection( int a[], int n );
#endif /* _sort_H */
1123456789
10111213141516171819
sort.h
85
/**********************************プログラム概要:ソートアルゴリズムライブラリ作成日/作成者:2011.5.4/杉田 薫**********************************/#include<stdio.h>#include<stdlib.h>#include <math.h>#include "sort.h"#include "dataFile.h"
/* マクロ関数定義 */#define swap( type, x, y ) do { type t = x; x = y; y = t; } while( 0 )
/**********************************関数名:void selection( int a[], int n )機能:単純選択ソート入力:配列( int a[] ) 要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void selection( int a[], int n ){ /* 変数宣言 */ int i; /* ループ用変数 */ int j; /* ループ用変数 */ int min; /* 最小値 */ for( i = 0; i <n - 1; i++ ) { min = i; for( j = i + 1 ; j < n ; j++ ) if( a[ j ] < a[ min ] ) min = j; swap(int, a[ i ], a[ min ] ); } /* End of for i */ } /* End of Function selection */
1123456789
1011121314151617181920212223242526272829303132333435363738394041
sort.c
86
Quick Sort
87
quick1
a
a[pl]<x
b
a[pr] > x
88
89
quick2
a
ptr > 0
ptr--
b
a[pl] < x
c
a[pr] x
d
pl <= pr x
90
91
URL createRandamDigitsFile.zip
createRandamDigitsFile.c dataFile.c dataFile.h
createRandamDigitsFile.c # define _EX_2
# undef _EX_2
http://goo.gl/q5ITXp
Visual Studio->Visual Studio Tools
cl /EHsc createRandamDigitsFile.c dataFile.c
createRandamDigitsFile.c
1365 136500 1365
PC
URL experiment6.zip experiment6.c
measureTime.c measureTime.h dataFile.c dataFile.h
http://goo.gl/pWvkKX
sort.c sort.h experiment6.c main
quick1
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment6.c dataFile.c measureTime.c sort.c
experiment61.exe
PC experiment61.exe
experiment61.exe
92
s
experiment6.c main quick1 quick2
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment6.c dataFile.c measureTime.c sort.c
experiment62.exe
PC experiment62.exe
experiment62.exe
s
experiment6.c main quick1 quick2
qsort
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment6.c dataFile.c measureTime.c sort.c
experiment63.exe
PC experiment63.exe
experiment63.exe
s
93
PC
94
s
95
s
96
s
97
[1] C
pp.174-185 2009
98
/**********************************プログラム概要:ソートアルゴリズムライブラリヘッダ作成日/作成者:2011.5.4/杉田 薫**********************************/#ifndef _sort_H#define _sort_H
/**********************************関数名:void quick1( int a[], int left, int right )機能:クイックソート入力:配列( int a[] ) 左カーソル(int left) 右カーソル(int right)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void quick1( int a[], int left, int right );
/**********************************関数名:int quick2( int a[], int left, int right )機能:クイックソート(非再帰版)入力:配列( int a[] ) 左カーソル(int left) 右カーソル(int right)出力:なし返却値:処理結果( int )作成日/作成者:2011.5.4/杉田 薫備考:テキストの関数の引数に"int num"を追加**********************************/int quick2( int a[], int left, int right );
/**********************************関数名:int int_cmp( const int *a, const int *b)機能:要素数nの配列aからkeyと一致する要素を2分探索入力:整数( const int *a ) 整数(const int *b)出力:なし返却値:処理結果(int)備考:bsearch関数,qsort関数で利用作成日/作成者:2011.4.26/杉田 薫**********************************/int int_cmp( const int *a, const int *b);
#endif /* _sort_H */
1123456789
101112131415161718192021222324252627282930313233343536373839404142434445
sort.h
99
/**********************************プログラム概要:ソートアルゴリズムライブラリ作成日/作成者:2011.5.4/杉田 薫修正日/修正者:2012.1.13/杉田 薫 関数「quick2」のスタックサイズ不足に対応**********************************/#include<stdio.h>#include<stdlib.h>#include <math.h>#include "sort.h"#include "dataFile.h"
/* マクロ関数定義 */#define swap( type, x, y ) do { type t = x; x = y; y = t; } while( 0 )
/**********************************関数名:void quick1( int a[], int left, int right )機能:クイックソート入力:配列( int a[] ) 左カーソル(int left) 右カーソル(int right)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/void quick1( int a[], int left, int right ){ int pl = left; /* 左カーソル */ int pr = right; /* 右カーソル */ int x = a[ (pl +pr) / 2 ]; /* 枢軸は中央の要素 */ do{ while( a[pl] < x ) pl++; while( a[pr] > x ) pr--; if( pl <= pr ) { swap( int, a[ pl ], a[ pr ] ); pl++; pr--; } } while( pl <=pr ); if( left < pr ) quick1( a, left, pr ); if( pl < right ) quick1( a, pl, right ); } /* End of Function quick1 */
/**********************************関数名:int quick2( int a[], int left, int right )機能:クイックソート(非再帰版)入力:配列( int a[] ) 左カーソル(int left) 右カーソル(int right)出力:なし返却値:処理結果( int )作成日/作成者:2011.5.4/杉田 薫修正日/修正者:2012.1.13/杉田 薫 スタックサイズ不足に対応するため,「スタックサイズの計算」を修正備考:テキストの関数の引数に"int num"を追加
1123456789
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
sort.c
100
**********************************/int quick2( int a[], int left, int right ){ /* 変数定義 */ int ptr = 0; /* スタックポインタ */ int *lstack; /* 分割すべき先頭要素の添字のスタック */ int *lstackP; /* 分割すべき先頭要素の添字のスタック */ int *rstack; /* 分割すべき末尾要素の添字のスタック */ int *rstackP; /* 分割すべき末尾要素の添字のスタック */ long stackSize; /* スタックサイズ */ int pl; /* 左カーソル */ int pr; /* 右カーソル */ int x; /* 中央要素 */ /* スタックサイズの計算 */ stackSize = (right - left + 1) / 2; /* スタックのメモリ確保 */ lstack = NULL; lstack =( int *)malloc( sizeof( int ) * stackSize ); if( lstack == NULL ) { printf("スタックのメモリ確保に失敗しました。\n"); return 1; } rstack = NULL; rstack =( int *)malloc( sizeof( int ) * stackSize ); if( rstack == NULL ) { printf("スタックのメモリ確保に失敗しました。\n"); free( lstack ); lstack = NULL; return 1; } lstackP = lstack +ptr; *lstackP = left; rstackP = rstack +ptr; *rstackP = right; ptr++; while( ptr-- > 0 ) { /* 左カーソルの設定 */ lstackP = lstack + ptr; pl = left = *lstackP; /* 右カーソルの設定 */ rstackP = rstack + ptr; pr = right = *rstackP; /* 中央の要素の設定 */ x = a[ ( left + right ) /2 ]; /* 中央の要素よりも小さい値が左側,大きい値が右側にくるように値を入れ替える */ do{ while( a[ pl ] < x ) pl++; while( a[ pr ] > x ) pr--;
26263646566676869707172737475767778798081828384858687888990919293949596979899
100101102103104105106107108109110111112113114115116117118119120121122
sort.c
101
if( pl <= pr ) { swap( int, a[pl], a[pr] ); pl++; pr--; } } while( pl <= pr ); if( left < pr ) { /* 先頭側のグループの範囲をプッシュ */ lstackP = lstack + ptr; *lstackP = left; rstackP = rstack + ptr; *rstackP = pr; ptr++; } if( pl < right ) { /* 末尾側のグループの範囲をプッシュ */ lstackP = lstack + ptr; *lstackP = pl; rstackP = rstack + ptr; *rstackP = right; ptr++; } } /* End of while ptr */ free( lstack ); lstack = NULL; free( rstack ); rstack = NULL; return 0;} /* End of Function quick2 */
/**********************************関数名:int int_cmp( const int *a, const int *b)機能:2変数の大きさを比較(qsort用)入力:整数( const int *a ) 整数(const int *b)出力:なし返却値:処理結果(int)備考:bsearch関数,qsort関数で利用作成日/作成者:2011.4.26/杉田 薫**********************************/int int_cmp( const int *a, const int *b){ if( *a > *b ) return 1; else if( *a < *b ) return -1; else return 0;} /* End of Function int_cmp */
3123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
sort.c
102
Hearp Sort
a
b
c
d
103
104
downheap
parent = left
parent < (right + 1) / 2
parent = child
105
106
heapsort1
a
i = (n – 1 )/2
i >= 0
i--
b
i = n – 1
i >= 0
i--
107
URL createRandamDigitsFile.zip
createRandamDigitsFile.c dataFile.c dataFile.h
createRandamDigitsFile.c # define _EX_2
# undef _EX_2
http://goo.gl/q5ITXp
Visual Studio->Visual Studio Tools
108
cl /EHsc createRandamDigitsFile.c dataFile.c
createRandamDigitsFile.c
1365 136500 1365
PC
URL experiment7.zip experiment7.c
measureTime.c measureTime.h dataFile.c dataFile.h
http://goo.gl/TGrKN0
sort.c sort.h
Visual Studio->Visual Studio Tools
cl /EHsc -DWIN32 -TC experiment7.c dataFile.c measureTime.c sort.c
experiment7.exe
PC experiment7.exe
experiment7.exe
s
109
PC
110
s
111
[1] C
pp.190-199 2009
112
/**********************************プログラム概要:ソートアルゴリズムライブラリヘッダ作成日/作成者:2011.5.4/杉田 薫**********************************/#ifndef _sort_H#define _sort_H
/**********************************関数名:static void downheap( int a[], int left, int right )機能:配列のヒープ化入力:配列( int a[] ) 左カーソル(int left) 右カーソル(int right)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/static void downheap( int a[], int left, int right );
/**********************************関数名:void heapsort1( int a[], int n )機能:ヒープソート入力:配列( int a[] ) 配列の要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫備考:stdlibにheapsortが存在するため,heapsort1に関数名を変更**********************************/void heapsort1( int a[], int n );
#endif /* _sort_H */
1123456789
1011121314151617181920212223242526272829303132
sort.h
113
/**********************************プログラム概要:ソートアルゴリズムライブラリ作成日/作成者:2011.5.4/杉田 薫**********************************/#include<stdio.h>#include<stdlib.h>#include <math.h>#include "sort.h"#include "dataFile.h"
/* マクロ関数定義 */#define swap( type, x, y ) do { type t = x; x = y; y = t; } while( 0 )
/**********************************関数名:int int_cmp( const int *a, const int *b)機能:2変数の大きさを比較(qsort用)入力:整数( const int *a ) 整数(const int *b)出力:なし返却値:処理結果(int)備考:bsearch関数で利用作成日/作成者:2011.4.26/杉田 薫**********************************/int int_cmp( const int *a, const int *b){ if( *a > *b ) return 1; else if( * a < *b ) return -1; else return 0;} /* End of Function int_cmp */
/**********************************関数名:static void downheap( int a[], int left, int right )機能:配列のヒープ化入力:配列( int a[] ) 左カーソル(int left) 右カーソル(int right)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫**********************************/static void downheap( int a[], int left, int right ){ /* 変数宣言 */ int temp = a[ left ]; /* 根 */ int child; /* 子 */ int parent; /* 親 */ int cl; /* 左の子 */ int cr; /* 右の子 */ for( parent = left; parent < ( right +1 ) / 2; parent = child ) { /* 左の子の位置設定 */ cl = parent * 2 + 1; /* 右の子の位置設定 */ cr = cl + 1; /* 子の設定 */
1123456789
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
sort.c
114
child = ( cr <= right && a[ cr ] > a[ cl ] ) ? cr : cl; if( temp >= a[ child ] ) break; a[ parent ] = a[child ]; } /* End of for parent */ a[ parent ] = temp; } /* End of Function downheap */
/**********************************関数名:void heapsort1( int a[], int n )機能:ヒープソート入力:配列( int a[] ) 配列の要素数(int n)出力:なし返却値:なし作成日/作成者:2011.5.4/杉田 薫備考:stdlibにheapsortが存在するため,heapsort1に関数名を変更**********************************/void heapsort1( int a[], int n ){ /* 変数宣言 */ int i; for( i = ( n - 1 ) / 2; i >= 0; i -- ) downheap( a, i, n - 1 ); for( i = n - 1; i > 0; i-- ) { swap( int, a[ 0 ], a[ i ] ); downheap( a, 0, i - 1 ); } /* End of for i */} /* End of Function heapsort1 */
26263646566676869707172737475767778798081828384858687888990919293949596
sort.c
115
top related