mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 06:49:36 +03:00
Optimizing code and renaming variables
This commit is contained in:
parent
fb4ce20699
commit
b893041e47
@ -1,33 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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<n+1;i++)
|
||||
newArray[i]=LongestSub[i-1];
|
||||
*RESULT = newArray;
|
||||
*leRESULT_LENGTH = n+1;
|
||||
|
||||
}
|
||||
else{
|
||||
|
||||
*RESULT=LongestSub;
|
||||
*leRESULT_LENGTH=n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void longestSub(int* ARRAY,int ARRAY_LENGTH, int** RESULT,int* RESULT_LENGTH){ //RESULT and RESULT_LENGTH will be modified by their pointers
|
||||
|
||||
@ -37,10 +10,9 @@ void longestSub(int* ARRAY,int ARRAY_LENGTH, int** RESULT,int* RESULT_LENGTH){ /
|
||||
}
|
||||
else{
|
||||
int PIVOT=ARRAY[0];
|
||||
int *maxi=NULL;
|
||||
int maxi_length=0,i,j;
|
||||
int *LONGEST_SUB=NULL;
|
||||
int LONGEST_SUB_LENGTH=0,i,j;
|
||||
int TEMPORARY_ARRAY_LENGTH=0, *TEMPORARY_ARRAY=NULL;
|
||||
int *LS_RESULT=NULL, LS_RESULT_length;
|
||||
|
||||
for(i=1;i<ARRAY_LENGTH;i++){
|
||||
if (ARRAY[i]<PIVOT){
|
||||
@ -57,17 +29,15 @@ void longestSub(int* ARRAY,int ARRAY_LENGTH, int** RESULT,int* RESULT_LENGTH){ /
|
||||
}
|
||||
}
|
||||
|
||||
LS_RESULT=NULL;
|
||||
LS_RESULT_length=0;
|
||||
longestSub(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH,&LS_RESULT,&LS_RESULT_length);
|
||||
if(maxi_length<LS_RESULT_length+1){
|
||||
longestSub(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH,&TEMPORARY_ARRAY,&TEMPORARY_ARRAY_LENGTH);
|
||||
if(LONGEST_SUB_LENGTH<TEMPORARY_ARRAY_LENGTH+1){
|
||||
|
||||
maxi_length=LS_RESULT_length+1;
|
||||
maxi=(int*)realloc(maxi,(maxi_length)*sizeof(int));
|
||||
maxi[0]=ARRAY[i];
|
||||
LONGEST_SUB_LENGTH=TEMPORARY_ARRAY_LENGTH+1;
|
||||
LONGEST_SUB=(int*)realloc(LONGEST_SUB,(LONGEST_SUB_LENGTH)*sizeof(int));
|
||||
LONGEST_SUB[0]=ARRAY[i];
|
||||
|
||||
for(i=1;i<maxi_length;i++)
|
||||
maxi[i]=LS_RESULT[i-1];
|
||||
for(i=1;i<LONGEST_SUB_LENGTH;i++)
|
||||
LONGEST_SUB[i]=TEMPORARY_ARRAY[i-1];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -83,17 +53,17 @@ void longestSub(int* ARRAY,int ARRAY_LENGTH, int** RESULT,int* RESULT_LENGTH){ /
|
||||
}
|
||||
}
|
||||
|
||||
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<maxi_length;i++)
|
||||
maxi[i]=LS_RESULT[i-1];
|
||||
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));
|
||||
LONGEST_SUB[0]=PIVOT;
|
||||
for(i=1;i<LONGEST_SUB_LENGTH;i++)
|
||||
LONGEST_SUB[i]=TEMPORARY_ARRAY[i-1];
|
||||
}
|
||||
*RESULT=maxi;
|
||||
*RESULT_LENGTH=maxi_length;
|
||||
*RESULT=LONGEST_SUB;
|
||||
*RESULT_LENGTH=LONGEST_SUB_LENGTH;
|
||||
}
|
||||
|
||||
|
||||
@ -103,9 +73,10 @@ int main(){
|
||||
|
||||
int *example=NULL;
|
||||
int n,i;
|
||||
int t[9]={10, 22, 9, 33, 21, 50, 41, 60, 80};
|
||||
//Il y'a un problème au cas où on a un maximum au milieu
|
||||
longestSub(t,9,&example,&n);
|
||||
//int t[9]={10, 22, 9, 33, 21, 50, 41, 60, 80};
|
||||
int t[8]={18, 2, 15, 4, 30, 0, 11, 12};
|
||||
//Il y'a un problème au cas où on a un LONGEST_SUBmum au milieu
|
||||
longestSub(t,8,&example,&n);
|
||||
printf("longueur: %d\n",n);
|
||||
for(i=0;i<n;i++)
|
||||
printf("%d ",example[i]);
|
||||
|
Loading…
Reference in New Issue
Block a user