From 1633281630e060b062ed32cdbe2237054f90b18e Mon Sep 17 00:00:00 2001 From: Mehdi ALAOUI Date: Wed, 5 Apr 2017 01:23:33 +0100 Subject: [PATCH 1/4] Modified variance.c: renamed file (lowercase), better filling method (with realloc), optimized sorting and more readable code --- VARIANCE.C | 56 ------------------------------------------------------ variance.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 56 deletions(-) delete mode 100644 VARIANCE.C create mode 100644 variance.c diff --git a/VARIANCE.C b/VARIANCE.C deleted file mode 100644 index 627d71e1..00000000 --- a/VARIANCE.C +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include -void main() -{ - int a[10],n,i,j,temp,sum=0; - float mean,var,stand; - clrscr(); - printf("Enter no. for Random Numbers :"); - scanf("%d",&n); - for(i=0;i +#include +#include + +int main() { + + int *ARRAY=NULL,ARRAY_LENGTH,i,TEMPORARY_ELEMENT,isSorted=0; + float MEAN=0,VARIANCE=0,STAND; + + + printf("Enter no. for Random Numbers :"); + scanf("%d",&ARRAY_LENGTH); + ARRAY=(int *)realloc(ARRAY,ARRAY_LENGTH*(sizeof(int))); //We allocate the dedicated memory + for(i=0;iARRAY[i+1]){ // if the two elements aren't sorted + isSorted=0; //it means that the array is not sorted + TEMPORARY_ELEMENT=ARRAY[i]; //and we switch these elements using TEMPORARY_ELEMENT + ARRAY[i]=ARRAY[i+1]; + ARRAY[i+1]=TEMPORARY_ELEMENT; + } + } + } + for(i=0;i Date: Fri, 7 Apr 2017 04:29:56 +0100 Subject: [PATCH 2/4] Adding an algorithm that returns the longest increasing subsequence from an array --- LongestSubSequence.c | 115 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 LongestSubSequence.c diff --git a/LongestSubSequence.c b/LongestSubSequence.c new file mode 100644 index 00000000..bf2e7e54 --- /dev/null +++ b/LongestSubSequence.c @@ -0,0 +1,115 @@ +#include +#include + +int* longestSubsequence(int* ARRAY, int LENGTH, int**RESULT,int* leRESULT_LENGTH){ + if(LENGTH<=1){ + *RESULT=ARRAY; + *leRESULT_LENGTH=LENGTH; + + } + else{ + int PIVOT=ARRAY[0],n; + int* LongestSub=NULL; + longestSubsequence(ARRAY+1,LENGTH-1,&LongestSub,&n); + if (PIVOT<=LongestSub[0]){ + int *newArray=(int *)realloc(NULL,(n+1)*sizeof(int)); + int i; + newArray[0]=PIVOT; + for(i=1;i=ARRAY[i]){ + + TEMPORARY_ARRAY_LENGTH++; + TEMPORARY_ARRAY=(int *)realloc(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH*sizeof(int)); + TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1]=ARRAY[j]; + } + } + + LS_RESULT=NULL; + LS_RESULT_length=0; + longestSub(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH,&LS_RESULT,&LS_RESULT_length); + if(maxi_length=PIVOT){ + TEMPORARY_ARRAY_LENGTH++; + TEMPORARY_ARRAY=(int *)realloc(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH*sizeof(int)); + TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1]=ARRAY[i]; + } + } + + longestSub(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH,&LS_RESULT,&LS_RESULT_length); + if(LS_RESULT_length+1>maxi_length){ + + maxi_length=LS_RESULT_length+1; + maxi=(int*)realloc(maxi,(maxi_length)*sizeof(int)); + maxi[0]=PIVOT; + for(i=1;i Date: Fri, 7 Apr 2017 04:37:52 +0100 Subject: [PATCH 3/4] Optimizing code and renaming variables --- LongestSubSequence.c | 75 ++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 52 deletions(-) diff --git a/LongestSubSequence.c b/LongestSubSequence.c index bf2e7e54..6f5cf8e8 100644 --- a/LongestSubSequence.c +++ b/LongestSubSequence.c @@ -1,33 +1,6 @@ #include #include -int* longestSubsequence(int* ARRAY, int LENGTH, int**RESULT,int* leRESULT_LENGTH){ - if(LENGTH<=1){ - *RESULT=ARRAY; - *leRESULT_LENGTH=LENGTH; - - } - else{ - int PIVOT=ARRAY[0],n; - int* LongestSub=NULL; - longestSubsequence(ARRAY+1,LENGTH-1,&LongestSub,&n); - if (PIVOT<=LongestSub[0]){ - int *newArray=(int *)realloc(NULL,(n+1)*sizeof(int)); - int i; - newArray[0]=PIVOT; - for(i=1;imaxi_length){ - - maxi_length=LS_RESULT_length+1; - maxi=(int*)realloc(maxi,(maxi_length)*sizeof(int)); - maxi[0]=PIVOT; - for(i=1;iLONGEST_SUB_LENGTH){ + + LONGEST_SUB_LENGTH=TEMPORARY_ARRAY_LENGTH+1; + LONGEST_SUB=(int*)realloc(LONGEST_SUB,(LONGEST_SUB_LENGTH)*sizeof(int)); + LONGEST_SUB[0]=PIVOT; + for(i=1;i Date: Fri, 7 Apr 2017 04:50:50 +0100 Subject: [PATCH 4/4] Main reviewed, space added between operators (+,..) --- LongestSubSequence.c | 92 ++++++++++++++++++++++--------------------- a.out | Bin 8649 -> 8816 bytes 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/LongestSubSequence.c b/LongestSubSequence.c index 6f5cf8e8..b1d74a41 100644 --- a/LongestSubSequence.c +++ b/LongestSubSequence.c @@ -4,66 +4,66 @@ void longestSub(int* ARRAY,int ARRAY_LENGTH, int** RESULT,int* RESULT_LENGTH){ //RESULT and RESULT_LENGTH will be modified by their pointers - if(ARRAY_LENGTH<=1){ + if(ARRAY_LENGTH <= 1){ *RESULT=ARRAY; - *RESULT_LENGTH=ARRAY_LENGTH; + *RESULT_LENGTH = ARRAY_LENGTH; } else{ - int PIVOT=ARRAY[0]; - int *LONGEST_SUB=NULL; - int LONGEST_SUB_LENGTH=0,i,j; - int TEMPORARY_ARRAY_LENGTH=0, *TEMPORARY_ARRAY=NULL; + int PIVOT = ARRAY[0]; + int *LONGEST_SUB = NULL; + int i, j, LONGEST_SUB_LENGTH = 0; + int TEMPORARY_ARRAY_LENGTH = 0, *TEMPORARY_ARRAY = NULL; - for(i=1;i=ARRAY[i]){ + for(j = i+1;j < ARRAY_LENGTH; j++){ + if(ARRAY[j] >= ARRAY[i]){ TEMPORARY_ARRAY_LENGTH++; - TEMPORARY_ARRAY=(int *)realloc(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH*sizeof(int)); - TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1]=ARRAY[j]; + TEMPORARY_ARRAY = (int *)realloc(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH*sizeof(int)); + TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1] = ARRAY[j]; } } - longestSub(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH,&TEMPORARY_ARRAY,&TEMPORARY_ARRAY_LENGTH); - if(LONGEST_SUB_LENGTH=PIVOT){ + if(ARRAY[i] >= PIVOT){ TEMPORARY_ARRAY_LENGTH++; - TEMPORARY_ARRAY=(int *)realloc(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH*sizeof(int)); - TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1]=ARRAY[i]; + TEMPORARY_ARRAY = (int *)realloc(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH*sizeof(int)); + TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1] = ARRAY[i]; } } - longestSub(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH,&TEMPORARY_ARRAY,&TEMPORARY_ARRAY_LENGTH); - if(TEMPORARY_ARRAY_LENGTH+1>LONGEST_SUB_LENGTH){ + longestSub(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH, &TEMPORARY_ARRAY, &TEMPORARY_ARRAY_LENGTH); + if(TEMPORARY_ARRAY_LENGTH + 1 > LONGEST_SUB_LENGTH){ - LONGEST_SUB_LENGTH=TEMPORARY_ARRAY_LENGTH+1; - LONGEST_SUB=(int*)realloc(LONGEST_SUB,(LONGEST_SUB_LENGTH)*sizeof(int)); - LONG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