mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Main reviewed, space added between operators (+,..)
This commit is contained in:
parent
b893041e47
commit
a70400eead
@ -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
|
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=ARRAY;
|
||||||
*RESULT_LENGTH=ARRAY_LENGTH;
|
*RESULT_LENGTH = ARRAY_LENGTH;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
int PIVOT=ARRAY[0];
|
int PIVOT = ARRAY[0];
|
||||||
int *LONGEST_SUB=NULL;
|
int *LONGEST_SUB = NULL;
|
||||||
int LONGEST_SUB_LENGTH=0,i,j;
|
int i, j, LONGEST_SUB_LENGTH = 0;
|
||||||
int TEMPORARY_ARRAY_LENGTH=0, *TEMPORARY_ARRAY=NULL;
|
int TEMPORARY_ARRAY_LENGTH = 0, *TEMPORARY_ARRAY = NULL;
|
||||||
|
|
||||||
for(i=1;i<ARRAY_LENGTH;i++){
|
for(i = 1; i < ARRAY_LENGTH; i++){
|
||||||
if (ARRAY[i]<PIVOT){
|
if (ARRAY[i] < PIVOT){
|
||||||
|
|
||||||
TEMPORARY_ARRAY_LENGTH=0;
|
TEMPORARY_ARRAY_LENGTH = 0;
|
||||||
TEMPORARY_ARRAY=NULL;
|
TEMPORARY_ARRAY = NULL;
|
||||||
|
|
||||||
for(j=i+1;j<ARRAY_LENGTH;j++){
|
for(j = i+1;j < ARRAY_LENGTH; j++){
|
||||||
if(ARRAY[j]>=ARRAY[i]){
|
if(ARRAY[j] >= ARRAY[i]){
|
||||||
|
|
||||||
TEMPORARY_ARRAY_LENGTH++;
|
TEMPORARY_ARRAY_LENGTH++;
|
||||||
TEMPORARY_ARRAY=(int *)realloc(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH*sizeof(int));
|
TEMPORARY_ARRAY = (int *)realloc(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH*sizeof(int));
|
||||||
TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1]=ARRAY[j];
|
TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1] = ARRAY[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
longestSub(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH,&TEMPORARY_ARRAY,&TEMPORARY_ARRAY_LENGTH);
|
longestSub(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH, &TEMPORARY_ARRAY, &TEMPORARY_ARRAY_LENGTH);
|
||||||
if(LONGEST_SUB_LENGTH<TEMPORARY_ARRAY_LENGTH+1){
|
if(LONGEST_SUB_LENGTH < TEMPORARY_ARRAY_LENGTH + 1){
|
||||||
|
|
||||||
LONGEST_SUB_LENGTH=TEMPORARY_ARRAY_LENGTH+1;
|
LONGEST_SUB_LENGTH = TEMPORARY_ARRAY_LENGTH + 1;
|
||||||
LONGEST_SUB=(int*)realloc(LONGEST_SUB,(LONGEST_SUB_LENGTH)*sizeof(int));
|
LONGEST_SUB = (int *)realloc(LONGEST_SUB, LONGEST_SUB_LENGTH*sizeof(int));
|
||||||
LONGEST_SUB[0]=ARRAY[i];
|
LONGEST_SUB[0] = ARRAY[i];
|
||||||
|
|
||||||
for(i=1;i<LONGEST_SUB_LENGTH;i++)
|
for(i = 1;i < LONGEST_SUB_LENGTH; i++)
|
||||||
LONGEST_SUB[i]=TEMPORARY_ARRAY[i-1];
|
LONGEST_SUB[i] = TEMPORARY_ARRAY[i-1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEMPORARY_ARRAY=NULL;
|
TEMPORARY_ARRAY = NULL;
|
||||||
TEMPORARY_ARRAY_LENGTH=0;
|
TEMPORARY_ARRAY_LENGTH = 0;
|
||||||
for(i=1;i<ARRAY_LENGTH;i++){
|
for(i = 1;i < ARRAY_LENGTH; i++){
|
||||||
|
|
||||||
if(ARRAY[i]>=PIVOT){
|
if(ARRAY[i] >= PIVOT){
|
||||||
TEMPORARY_ARRAY_LENGTH++;
|
TEMPORARY_ARRAY_LENGTH++;
|
||||||
TEMPORARY_ARRAY=(int *)realloc(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH*sizeof(int));
|
TEMPORARY_ARRAY = (int *)realloc(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH*sizeof(int));
|
||||||
TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1]=ARRAY[i];
|
TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1] = ARRAY[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
longestSub(TEMPORARY_ARRAY,TEMPORARY_ARRAY_LENGTH,&TEMPORARY_ARRAY,&TEMPORARY_ARRAY_LENGTH);
|
longestSub(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH, &TEMPORARY_ARRAY, &TEMPORARY_ARRAY_LENGTH);
|
||||||
if(TEMPORARY_ARRAY_LENGTH+1>LONGEST_SUB_LENGTH){
|
if(TEMPORARY_ARRAY_LENGTH + 1 > LONGEST_SUB_LENGTH){
|
||||||
|
|
||||||
LONGEST_SUB_LENGTH=TEMPORARY_ARRAY_LENGTH+1;
|
LONGEST_SUB_LENGTH = TEMPORARY_ARRAY_LENGTH + 1;
|
||||||
LONGEST_SUB=(int*)realloc(LONGEST_SUB,(LONGEST_SUB_LENGTH)*sizeof(int));
|
LONGEST_SUB = (int *)realloc(LONGEST_SUB, LONGEST_SUB_LENGTH*sizeof(int));
|
||||||
LONGEST_SUB[0]=PIVOT;
|
LONGEST_SUB[0] = PIVOT;
|
||||||
for(i=1;i<LONGEST_SUB_LENGTH;i++)
|
for(i = 1;i < LONGEST_SUB_LENGTH; i++)
|
||||||
LONGEST_SUB[i]=TEMPORARY_ARRAY[i-1];
|
LONGEST_SUB[i] = TEMPORARY_ARRAY[i-1];
|
||||||
}
|
}
|
||||||
*RESULT=LONGEST_SUB;
|
*RESULT = LONGEST_SUB;
|
||||||
*RESULT_LENGTH=LONGEST_SUB_LENGTH;
|
*RESULT_LENGTH = LONGEST_SUB_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -71,15 +71,17 @@ void longestSub(int* ARRAY,int ARRAY_LENGTH, int** RESULT,int* RESULT_LENGTH){ /
|
|||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
int *example=NULL;
|
int EXAMPLE_LENGTH = 8;
|
||||||
int n,i;
|
int EXAMPLE[] = {18, 2, 15, 4, 30, 0, 11, 12};
|
||||||
//int t[9]={10, 22, 9, 33, 21, 50, 41, 60, 80};
|
|
||||||
int t[8]={18, 2, 15, 4, 30, 0, 11, 12};
|
int *RESULT = NULL;
|
||||||
//Il y'a un problème au cas où on a un LONGEST_SUBmum au milieu
|
int RESULT_LENGTH, i;
|
||||||
longestSub(t,8,&example,&n);
|
|
||||||
printf("longueur: %d\n",n);
|
longestSub(EXAMPLE, EXAMPLE_LENGTH, &RESULT, &RESULT_LENGTH);
|
||||||
for(i=0;i<n;i++)
|
|
||||||
printf("%d ",example[i]);
|
printf("Longest Sub Sequence length: %d and it's:\n", RESULT_LENGTH);
|
||||||
|
for(i = 0;i < RESULT_LENGTH; i++)
|
||||||
|
printf("%d ",RESULT[i]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user