From b6ca15ed2e71ffdd35fdddcfc1df61802e76e322 Mon Sep 17 00:00:00 2001 From: chenyixuan0119 <100676477+chenyixuan0119@users.noreply.github.com> Date: Fri, 4 Mar 2022 23:55:07 +0800 Subject: [PATCH 1/9] Update 5_practice.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 直接計算版本 --- 5_practice.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/5_practice.c b/5_practice.c index 70d6651..b5eaf72 100644 --- a/5_practice.c +++ b/5_practice.c @@ -9,31 +9,39 @@ int main() { time_t t; // Input the number N - + printf("Input the number N :"); + scanf("%d",&N); // Locate the memory for x, yr, yi; - - // Initial setting for x, for example, x[k] = k - - + x = (double *) malloc(N*sizeof(double)); + yr = (double *) malloc(N*sizeof(double)); + yi = (double *) malloc(N*sizeof(double)); + // Initial setting for x, for example, x[k] = k + for(k=0;k Date: Sat, 5 Mar 2022 00:32:46 +0800 Subject: [PATCH 2/9] Revert "Update 5_practice.c" This reverts commit b6ca15ed2e71ffdd35fdddcfc1df61802e76e322. --- 5_practice.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/5_practice.c b/5_practice.c index b5eaf72..70d6651 100644 --- a/5_practice.c +++ b/5_practice.c @@ -9,39 +9,31 @@ int main() { time_t t; // Input the number N - printf("Input the number N :"); - scanf("%d",&N); - // Locate the memory for x, yr, yi; - x = (double *) malloc(N*sizeof(double)); - yr = (double *) malloc(N*sizeof(double)); - yi = (double *) malloc(N*sizeof(double)); + // Locate the memory for x, yr, yi; + // Initial setting for x, for example, x[k] = k - for(k=0;k Date: Sat, 5 Mar 2022 03:41:38 +0800 Subject: [PATCH 3/9] l16091019 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 利用尤拉公式把e^(-i*2*pi*k*n/N)轉換成cos跟sin直接計算 --- 5_practice.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/5_practice.c b/5_practice.c index 70d6651..4aa2d34 100644 --- a/5_practice.c +++ b/5_practice.c @@ -2,38 +2,48 @@ #include // for memory allocation #include // for time calculation #include // for sine and cosine functions + int main() { // Declare all the variables int k, n, N; double *x, *yr, *yi; - time_t t; + time_t t, t1; + int j; // Input the number N - + printf("Input the number N :"); + scanf("%d",&N); // Locate the memory for x, yr, yi; - - // Initial setting for x, for example, x[k] = k - - + x = (double *) malloc(N*sizeof(double)); + yr = (double *) malloc(N*sizeof(double)); + yi = (double *) malloc(N*sizeof(double)); + // Initial setting for x, for example, x[k] = k + for(k=0;k Date: Sat, 5 Mar 2022 03:45:18 +0800 Subject: [PATCH 4/9] Update 5_practice.c --- 5_practice.c | 1 - 1 file changed, 1 deletion(-) diff --git a/5_practice.c b/5_practice.c index 4aa2d34..299ae30 100644 --- a/5_practice.c +++ b/5_practice.c @@ -8,7 +8,6 @@ int main() { int k, n, N; double *x, *yr, *yi; time_t t, t1; - int j; // Input the number N printf("Input the number N :"); From 579dfbd2759a65619671ca65eccebecb67ee4e2b Mon Sep 17 00:00:00 2001 From: chenyixuan0119 <100676477+chenyixuan0119@users.noreply.github.com> Date: Sat, 5 Mar 2022 03:45:52 +0800 Subject: [PATCH 5/9] l16091019 --- 5_practice.c | 1 - 1 file changed, 1 deletion(-) diff --git a/5_practice.c b/5_practice.c index 299ae30..caa4256 100644 --- a/5_practice.c +++ b/5_practice.c @@ -2,7 +2,6 @@ #include // for memory allocation #include // for time calculation #include // for sine and cosine functions - int main() { // Declare all the variables int k, n, N; From ac64c7b70dab450c07a65d484227f662d5bb782f Mon Sep 17 00:00:00 2001 From: chenyixuan0119 <100676477+chenyixuan0119@users.noreply.github.com> Date: Tue, 8 Mar 2022 21:05:11 +0800 Subject: [PATCH 6/9] L16091019_update2_5_practice.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit method1跟上個版本一樣直接計算,method2為上課所說的利用遞迴方式計算 --- 5_practice.c | 57 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/5_practice.c b/5_practice.c index caa4256..1c4b351 100644 --- a/5_practice.c +++ b/5_practice.c @@ -2,20 +2,26 @@ #include // for memory allocation #include // for time calculation #include // for sine and cosine functions + int main() { // Declare all the variables - int k, n, N; + int k, n, N, i; double *x, *yr, *yi; - time_t t, t1; - + double *yr1,*yi1; + double an,bn,a0,b0,a,b; + double temp,dr,di; + time_t t,t1; + // Input the number N printf("Input the number N :"); scanf("%d",&N); - + // Locate the memory for x, yr, yi; x = (double *) malloc(N*sizeof(double)); yr = (double *) malloc(N*sizeof(double)); yi = (double *) malloc(N*sizeof(double)); + yr1 = (double *) malloc(N*sizeof(double)); + yi1 = (double *) malloc(N*sizeof(double)); // Initial setting for x, for example, x[k] = k for(k=0;k Date: Tue, 15 Mar 2022 22:59:38 +0800 Subject: [PATCH 7/9] Add files via upload --- 6_sorting.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 6_sorting.c diff --git a/6_sorting.c b/6_sorting.c new file mode 100644 index 0000000..d50b77d --- /dev/null +++ b/6_sorting.c @@ -0,0 +1,90 @@ +#include // for printf function +#include // for memory allocation +#include // for time calculation +#include // for sine and cosine functions +int quick_sort(double *x, int L, int R); +int main() { + // Declare all the variables + int k, m, n, N; + double *x, *y, z, p; + time_t t; + + // Input the number N + printf("Input N: "); + scanf("%d",&N); + + // Locate the memory for x and y; + x = (double *) malloc(N*sizeof(double)); + y = (double *) malloc(N*sizeof(double)); + + // Initial setting for x, for example, x[k] = 1.0*rand()/RAND_MAX + srand( time(NULL) ); + for(k=0;k x[k]) { + z = x[n]; + x[n] = x[k]; + x[k] = z; + } + } + } + t = clock() - t; + // print y, x, and time + printf("Sorting %d elements: %f s\n", N, 1.0*t/CLOCKS_PER_SEC); + if(N<10) { + printf("y \t\t x\n"); + for(k=0;k Date: Wed, 16 Mar 2022 02:30:35 +0800 Subject: [PATCH 8/9] update1_6_sorting.c --- 6_sorting.c | 103 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 20 deletions(-) diff --git a/6_sorting.c b/6_sorting.c index d50b77d..ec832e3 100644 --- a/6_sorting.c +++ b/6_sorting.c @@ -3,10 +3,11 @@ #include // for time calculation #include // for sine and cosine functions int quick_sort(double *x, int L, int R); +double quick_sort_median(double *x1, int L, int R, int M1,int bl); int main() { // Declare all the variables int k, m, n, N; - double *x, *y, z, p; + double *x, *y,*x1, z, p , med, med1; time_t t; // Input the number N @@ -16,11 +17,11 @@ int main() { // Locate the memory for x and y; x = (double *) malloc(N*sizeof(double)); y = (double *) malloc(N*sizeof(double)); - + x1 = (double *) malloc(N*sizeof(double)); // Initial setting for x, for example, x[k] = 1.0*rand()/RAND_MAX srand( time(NULL) ); for(k=0;k= R){ + return 0; + } + p = x[R-1]; + n = L; + + for(k=L;k= R){ + return 0; + } + + p = x1[R-1]; + n = L; + for(k=L;k M1){ + quick_sort_median(x1,L,n,M1,bl); + } + else if(n < M1){ + quick_sort_median(x1,n+1,R,M1,bl); + } + else{ + if(bl){ + return x1[n]; + } + else{ + quick_sort_median(x1,n+1,R,M1+1,bl); + return (x1[n]+x1[n+1])/2.0; + } + } } From 5153c81b572b1bbac30648967660329b3a8d4b10 Mon Sep 17 00:00:00 2001 From: chenyixuan0119 <100676477+chenyixuan0119@users.noreply.github.com> Date: Wed, 16 Mar 2022 04:44:53 +0800 Subject: [PATCH 9/9] update2_6_sorting.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit M1是利用quick_sort_median求中位數,而M2是利用quick_sort_median1求中位數,差別在求n為偶數時,M1是在 function 裡從大於第n/2大的數中找第n/2+1大的數字,而M2則是直接在main裡面全部從x[0]到x[N-1]在重新跑過一遍找第n/2+1大的數字,但後來發現兩個會一樣快,是因為M2在前n/2項不會做更動,所以其實就跟M1在找第n/2+1大的數做的事情是一樣的,四分位數就直接利用 M1 去搜尋 --- 6_sorting.c | 146 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 99 insertions(+), 47 deletions(-) diff --git a/6_sorting.c b/6_sorting.c index ec832e3..8c815d1 100644 --- a/6_sorting.c +++ b/6_sorting.c @@ -3,12 +3,13 @@ #include // for time calculation #include // for sine and cosine functions int quick_sort(double *x, int L, int R); -double quick_sort_median(double *x1, int L, int R, int M1,int bl); +double quick_sort_median(double *x, int L, int R, int M1,int bl); +double quick_sort_median1(double *x, int L, int R, int M1); int main() { // Declare all the variables int k, m, n, N; - double *x, *y,*x1, z, p , med, med1; - time_t t; + double *x, *y, z, p , med, med1, med2,q1,q3; + time_t t,t1,t2; // Input the number N printf("Input N: "); @@ -17,14 +18,12 @@ int main() { // Locate the memory for x and y; x = (double *) malloc(N*sizeof(double)); y = (double *) malloc(N*sizeof(double)); - x1 = (double *) malloc(N*sizeof(double)); // Initial setting for x, for example, x[k] = 1.0*rand()/RAND_MAX + srand( time(NULL) ); for(k=0;k= R){ return 0; } - p = x1[R-1]; + p = x[R-1]; n = L; for(k=L;k M1){ - quick_sort_median(x1,L,n,M1,bl); + quick_sort_median(x,L,n,M1,bl); } else if(n < M1){ - quick_sort_median(x1,n+1,R,M1,bl); + quick_sort_median(x,n+1,R,M1,bl); } else{ if(bl){ - return x1[n]; + return x[n]; } else{ - quick_sort_median(x1,n+1,R,M1+1,bl); - return (x1[n]+x1[n+1])/2.0; + quick_sort_median(x,n+1,R,M1+1,bl); + return (x[n]+x[n+1])/2.0; } } } + +double quick_sort_median1(double *x, int L, int R, int M1){ + int k, n,m; + double z, p; + if(L >= R){ + return 0; + } + + p = x[R-1]; + n = L; + for(k=L;k M1){ + quick_sort_median1(x,L,n,M1); + } + else if(n < M1){ + quick_sort_median1(x,n+1,R,M1); + } + else{ + return x[n]; + } + +}