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