A common.h include #include <stdio.h> #include <time.h> #define MAXN int A[MAXN], n; double start,end; void inputdata(

Similar documents
‚æ2›ñ C„¾„ê‡Ìš|

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

:30 12:00 I. I VI II. III. IV. a d V. VI

Gauss

:30 12:00 I. I VI II. III. IV. a d V. VI

DA13

Microsoft Word - 計算科学演習第1回3.doc

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

C言語によるアルゴリズムとデータ構造

Taro-再帰関数Ⅲ(公開版).jtd

‚æ4›ñ

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

XMPによる並列化実装2

[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±

r11.dvi

ohp11.dvi

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

PowerPoint プレゼンテーション

1 return main() { main main C 1 戻り値の型 関数名 引数 関数ブロックをあらわす中括弧 main() 関数の定義 int main(void){ printf("hello World!!\n"); return 0; 戻り値 1: main() 2.2 C main

I117 7 School of Information Science, Japan Advanced Institute of Science and Technology

Taro-最大値探索法の開発(公開版

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

演習課題No12

Microsoft Word - no206.docx

Cプログラミング - 第8回 構造体と再帰

関数の呼び出し ( 選択ソート ) 選択ソートのプログラム (findminvalue, findandreplace ができているとする ) #include <stdio.h> #define InFile "data.txt" #define OutFile "sorted.txt" #def

Taro-再帰関数Ⅱ(公開版).jtd

1 4 2 EP) (EP) (EP)

USB ID TA DUET 24:00 DUET XXX -YY.c ( ) XXX -YY.txt() XXX ID 3 YY ID 5 () #define StudentID 231

Taro-スタック(公開版).jtd

O(N) ( ) log 2 N

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

練習&演習問題

kiso2-09.key

関数の呼び出し ( 選択ソート ) 選択ソートのプログラム (findminvalue, findandreplace ができているとする ) #include <stdiu.h> #define InFile "data.txt" #define OutFile "surted.txt" #def

C 2 / 21 1 y = x 1.1 lagrange.c 1 / Laglange / 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 float x[10], y[10]; 7 float xx, pn, p; 8 in

(search: ) [1] ( ) 2 (linear search) (sequential search) 1

untitled

#define N1 N+1 double x[n1] =.5, 1., 2.; double hokan[n1] = 1.65, 2.72, 7.39 ; double xx[]=.2,.4,.6,.8,1.2,1.4,1.6,1.8; double lagrng(double xx); main

Taro-リストⅠ(公開版).jtd

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

file:///D|/C言語の擬似クラス.txt

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

Taro-プログラミングの基礎Ⅱ(公

Microsoft Word - no15.docx

新・明解C言語 ポインタ完全攻略

£Ã¥×¥í¥°¥é¥ß¥ó¥°(2018) - Âè10²ó – ¿¹à¼°¤Îɾ²Á¡§¥¢¥ë¥´¥ê¥º¥à¤Î²þÁ± –


[ 1] 1 Hello World!! 1 #include <s t d i o. h> 2 3 int main ( ) { 4 5 p r i n t f ( H e l l o World!! \ n ) ; 6 7 return 0 ; 8 } 1:

x h = (b a)/n [x i, x i+1 ] = [a+i h, a+ (i + 1) h] A(x i ) A(x i ) = h 2 {f(x i) + f(x i+1 ) = h {f(a + i h) + f(a + (i + 1) h), (2) 2 a b n A(x i )

comment.dvi

QR

1 ( ) 1.1 (convert.sh) (18GHz 26GHz) C (convert.c, convert1.c) mesure-ryudai convert.sh #!/bin/sh # file1 file1= ls -1 $1 # file1 data for data in $fi

( ) 1 1: 1 #include <s t d i o. h> 2 #include <GL/ g l u t. h> 3 #include <math. h> 4 #include <s t d l i b. h> 5 #include <time. h>

C ( ) C ( ) C C C C C 1 Fortran Character*72 name Integer age Real income 3 1 C mandata mandata ( ) name age income mandata ( ) mandat

18 C ( ) hello world.c 1 #include <stdio.h> 2 3 main() 4 { 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.o

C言語による数値計算プログラミング演習

PowerPoint Presentation

Taro-2分探索木Ⅱ(公開版).jtd

プログラミング方法論 II 第 14,15 回 ( 担当 : 鈴木伸夫 ) 問題 17. x 座標と y 座標をメンバに持つ構造体 Point を作成せよ 但し座標 は double 型とする typedef struct{ (a) x; (b) y; } Point; 問題 18. 問題 17 の

Microsoft Word - ICPC2014_DomG.docx

9 8 7 (x-1.0)*(x-1.0) *(x-1.0) (a) f(a) (b) f(a) Figure 1: f(a) a =1.0 (1) a 1.0 f(1.0)

新・明解C言語で学ぶアルゴリズムとデータ構造

Krylov (b) x k+1 := x k + α k p k (c) r k+1 := r k α k Ap k ( := b Ax k+1 ) (d) β k := r k r k 2 2 (e) : r k 2 / r 0 2 < ε R (f) p k+1 :=

Prog1_15th

Taro-リストⅢ(公開版).jtd

P06.ppt

C のコード例 (Z80 と同機能 ) int main(void) { int i,sum=0; for (i=1; i<=10; i++) sum=sum + i; printf ("sum=%d n",sum); 2

21 B92 B92 Quantum cryptography simulator

ex01.dvi

r07.dvi

P02.ppt

inst.c

program.dvi

ohp07.dvi

Microsoft Word - C.....u.K...doc

kiso2-06.key

C¥×¥í¥°¥é¥ß¥ó¥° ÆþÌç

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology

untitled

AutoTuned-RB

ex14.dvi

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

lexex.dvi

ープのロープ長以下であれば実現可能である ケース 3: 3 本のロープの杭の位置を点 P 1 = (x 1, y 1, 0), 点 P 2 = (x 2, y 2, 0), 点 P 3 = (x 3, y 3, 0) とする 点 P 1 = (x 1, y 1, 0), 点 P 2 = (x 2,

r08.dvi

() () (parse tree) ( (( ) * 50) ) ( ( NUM 10 + NUM 30 ) * NUM 50 ) ( * ) ( + ) NUM 50 NUM NUM (abstract syntax tree, AST) ( (( ) * 5

1.ppt

untitled

¥×¥í¥°¥é¥ß¥ó¥°±é½¬I Exercise on Programming I [1zh] ` `%%%`#`&12_`__~~~ alse

joho12.ppt

橡Pro PDF

P05.ppt

2 [1] 7 5 C 2.1 (kikuchi-fem-mac ) input.dat (cat input.dat type input.dat ))

Microsoft Word - no15.docx

PowerPoint プレゼンテーション

() / (front end) (back end) (phase) (pass) 1 2

スライド 1

Transcription:

2 065762A 19 7 13 1 2 2.1 common.h include #include <stdio.h> #include <time.h> #define MAXN 1000000 int A[MAXN], n; double start,end; void inputdata(void) int i; // printf(" "); scanf("%d",&n); // printf(" "); for(i=1;i<=n;i++) scanf("%d",&a[i]); return; 1

void printdata(void) printf("%07d %10.4f\n", n, end - start); return; void swap(int x,int y) //A[x] A[y] int temp; temp=a[x]; A[x]=A[y]; A[y]=temp; #include <sys/time.h> #include <sys/resource.h> double getrusage_sec() struct rusage t; struct timeval tv; getrusage(rusage_self, &t); tv = t.ru_utime; // return tv.tv_sec + (double)tv.tv_usec*1e-6; return (tv.tv_sec + tv.tv_usec)/1000.0000; 2.2 #include <stdio.h> #include "common.h" //common.h void selectionsort(int,int ); int main(void) inputdata(); start=getrusage_sec();// start selectionsort(1,n); end=getrusage_sec();// end 2

printdata(); void selectionsort(int p,int q) int i,j,cmin; for(j=p;j<=q;j++) cmin=j; for(i=j+1;i<=q;i++) if(a[cmin]>a[i])cmin=i; swap(j,cmin); 2.3 #include <stdio.h> #include "common.h" void insertionsort(int,int); int main(void) inputdata(); start=getrusage_sec(); insertionsort(1,n); end=getrusage_sec(); printdata(); void insertionsort(int p,int q) int i,j,c; for(j=p+1;j<=q;j++) c=a[j]; i=j; while(i>p&&a[i-1]>c) A[i]=A[i-1];i=i-1; A[i]=c; 3

2.4 #include <stdio.h> #include "common.h" void heapsort(int,int); void makeheap(int,int); void heapify(int,int); void printdata_reverse(void); int main(void) inputdata(); start=getrusage_sec(); heapsort(1,n); end=getrusage_sec(); //printdata_reverse(); printdata(); void heapsort(int s,int t) int i; makeheap(s,t); for(i=t;i>=s+1;i--) swap(s,i); heapify(s,i-1); //A[s]...A[t] void makeheap(int s,int t) int i; 4

for(i=t;i>=s;i--) heapify(i,t); //A[p] Shift-down void heapify(int p,int q) int r; r=2*p; if(r <= q) if(r<q && A[r]>A[r+1]) r=r+1; if(a[p] > A[r]) swap(p,r); heapify(r,q); void printdata_reverse(void) int i; printf(" \n"); for(i=n;i>=1;i--) printf("%d ",A[i]); printf("\n"); 2.5 #include "common.h" void mergesort(int,int); void merge(int,int,int); int B[MAXN]; int main(void) inputdata(); start=getrusage_sec(); mergesort(1,n); end=getrusage_sec(); printdata(); 5

void mergesort(int p,int r) int q; if(p < r) q = (p+r)/2; mergesort(p,q);// mergesort mergesort(q+1,r); merge(p,q,r); A[p]...A[q] A[q+1]...A[r] void merge(int p,int q,int r) int i,j,k; i=p,j=q+1; // A A[i] A[j] B[k] for(k=p;k<=r;k++) if((j>r) ((i<=q)&&(a[i]<=a[j]))) B[k]=A[i]; i=i+1; else B[k]=A[j]; j=j+1; for(k=p;k<=r;k++) A[k]=B[k]; 2.6 #include <stdio.h> #include "common.h" int find_median3(int,int); int partition(int,int,int); void quicksort(int,int); 6

void insertionsort(int,int); int main(void) inputdata(); A[0]=-999999999;//A[0] start=getrusage_sec(); quicksort(1,n); end=getrusage_sec(); printdata(); int find_median3(int left,int right) int center,p; center=(left+right)/2; //A[left]<=A[center]<=A[right] if(a[left]>a[center])swap(left,center); if(a[left]>a[right])swap(left,right); if(a[center]>a[right])swap(center,right); p=a[center];//a[center] swap(center,right); return(p); int partition(int left,int right,int q) int i,j; i=left-1; j=right; do do i=i+1; while(a[i]<q); do j=j-1; while(a[j]>q); if(i<j)swap(i,j); while(i<j); swap(i,right); return(i); 7

void quicksort(int left,int right) int cutoff,pivot,i; cutoff=10; if((right-left)<cutoff) insertionsort(left,right); else pivot=find_median3(left,right); i=partition(left,right,pivot); quicksort(left,i-1);// (A[left]...A[i-1]) quicksort(i+1,right);// (A[i+1]...A[right]) void insertionsort(int p,int q) int i,j,c; for(j=p+1;j<=q;j++) c=a[j]; i=j; while(i>p&&a[i-1]>c) A[i]=A[i-1];i=i-1; A[i]=c; 3 c getrusage() getrusage() struct timeval 1000 int main(void) inputdata(); start=getrusage_sec(); <- insertionsort(1,n); end=getrusage_sec(); <- printdata(); end start 8

clock() time() gettimeofday() getrusage() 4 10 100 100,1000,2000,2500,5000,7500,10000,20000, 30000,40000,50000,60000,70000,80000,90000,100000,150000,200000,300000,400000,500000 500000 400000 4.1 random.c c rand rand 1 1000 random.c 10 500000 4.1.1 100 rand000100.txt prompt>./random > rand/rand000100.txt 100 prompt> cat rand/rand000100.txt 100 341 439 170 829 992 34 106 497 851 610 167 963 784 256 91 171 384 337 210 284 453 397 762 238 636 664 283 183 545 12 974 420 191 291 65 283 21 353 945 660 501 103 137 750 250 500 773 981 815 272 675 330 273 554 655 151 953 190 985 643 54 839 735 40 709 73 107 837 621 721 679 235 930 639 240 959 76 377 745 984 266 873 925 134 465 919 887 74 292 221 144 250 267 639 181 510 141 105 948 162 4.1.2 #include <stdlib.h> #include <stdio.h> #include <time.h> int main(void) int a,i,j; scanf("%d",&a); 9

printf("%d\n",a); srand((unsigned) time(null)); //1 1000 a for (i=0; i<a; i++) printf("%d\n",rand()%1000+1); 4.2 timing.sh prompt>./quick < randxxxxxx.txt randxxxxxx.txt rand ls./quick data xxx.dat 4.2.1 10 500000 quick.dat prompt>./timing.sh quick prompt> cat data/quick.dat 10 0.0000 100 0.0000 500 0.0000 1000 0.0000 2000 0.0000 2500 0.0000 5000 0.0000 7500 0.0000 10000 0.0000 20000 4.0010 30000 8.0010 40000 8.0000 50000 8.0010 60000 12.0010 70000 12.0010 80000 12.0000 90000 16.0010 10

100000 20.0010 150000 28.0020 200000 36.0020 300000 52.0040 400000 76.0050 500000 92.0060 4.2.2 #!/bin/sh # if [ $# -ne 1 ] ; then echo "prompt> $0 arg1" exit 1 fi rand ls list list= /bin/ls -1./rand data $1.dat touch./data/$1.dat //rand data/$1.dat for filename in $list do./$1 < rand/$filename >>./data/$1.dat done 11

5 6 n 2 nlogn n 2 nlogn O(n 2 ) O(nlogn) 2 1 [1] [2] Mover.C : http://kzk9.net/column/time.html [3] GnuplotTips http://t16web.lanl.gov/kawano/gnuplot/index.html 12