mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 13:31:21 +03:00
Added Longest increasing subsequence algorithm, and optimized variance.c
Added Longest increasing subsequence algorithm, and optimized variance.c
This commit is contained in:
commit
4913c8e604
88
LongestSubSequence.c
Normal file
88
LongestSubSequence.c
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
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){
|
||||||
|
*RESULT=ARRAY;
|
||||||
|
*RESULT_LENGTH = ARRAY_LENGTH;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
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_LENGTH; i++){
|
||||||
|
if (ARRAY[i] < PIVOT){
|
||||||
|
|
||||||
|
TEMPORARY_ARRAY_LENGTH = 0;
|
||||||
|
TEMPORARY_ARRAY = NULL;
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
longestSub(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH, &TEMPORARY_ARRAY, &TEMPORARY_ARRAY_LENGTH);
|
||||||
|
if(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[0] = ARRAY[i];
|
||||||
|
|
||||||
|
for(i = 1;i < LONGEST_SUB_LENGTH; i++)
|
||||||
|
LONGEST_SUB[i] = TEMPORARY_ARRAY[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEMPORARY_ARRAY = NULL;
|
||||||
|
TEMPORARY_ARRAY_LENGTH = 0;
|
||||||
|
for(i = 1;i < ARRAY_LENGTH; i++){
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = LONGEST_SUB;
|
||||||
|
*RESULT_LENGTH = LONGEST_SUB_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
int EXAMPLE_LENGTH = 8;
|
||||||
|
int EXAMPLE[] = {18, 2, 15, 4, 30, 0, 11, 12};
|
||||||
|
|
||||||
|
int *RESULT = NULL;
|
||||||
|
int RESULT_LENGTH, i;
|
||||||
|
|
||||||
|
longestSub(EXAMPLE, EXAMPLE_LENGTH, &RESULT, &RESULT_LENGTH);
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
56
VARIANCE.C
56
VARIANCE.C
@ -1,56 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<conio.h>
|
|
||||||
#include<math.h>
|
|
||||||
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<n;i++)
|
|
||||||
{
|
|
||||||
a[i]=rand()%100;
|
|
||||||
}
|
|
||||||
printf("Random Numbers Generated are :\n");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
printf("\n%d",a[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
printf("\nSorted Data:");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
for(j=0;j<n;j++)
|
|
||||||
{
|
|
||||||
if(a[i]<a[j])
|
|
||||||
{
|
|
||||||
temp=a[i];
|
|
||||||
a[i]=a[j];
|
|
||||||
a[j]=temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
printf("\n%d",a[i]);
|
|
||||||
sum=sum+a[i];
|
|
||||||
}
|
|
||||||
mean=sum/(float)n;
|
|
||||||
printf("\nMean :");
|
|
||||||
printf("%f",mean);
|
|
||||||
printf("\n");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
sum=sum+(pow((a[i]-mean),2));
|
|
||||||
}
|
|
||||||
var=sum/(float)n;
|
|
||||||
printf("\nVariance is : %f",var);
|
|
||||||
printf("\n");
|
|
||||||
stand=sqrt(var);
|
|
||||||
printf("\nStandard Deviation is : %f",stand);
|
|
||||||
getch();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
54
variance.c
Normal file
54
variance.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include<math.h>
|
||||||
|
|
||||||
|
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;i<ARRAY_LENGTH;i++) //We generate the random numbers
|
||||||
|
ARRAY[i]=rand()%100;
|
||||||
|
|
||||||
|
printf("Random Numbers Generated are :\n"); //We display them
|
||||||
|
for(i=0;i<ARRAY_LENGTH;i++)
|
||||||
|
printf("%d ",ARRAY[i]);
|
||||||
|
|
||||||
|
printf("\nSorted Data: ");//Then we sort it using Bubble Sort..
|
||||||
|
|
||||||
|
while(!isSorted){ //While our array's not sorted
|
||||||
|
isSorted=1; //we suppose that it's sorted
|
||||||
|
for(i=0;i<ARRAY_LENGTH-1;i++){ //then for each element of the array
|
||||||
|
if(ARRAY[i]>ARRAY[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<ARRAY_LENGTH;i++) {
|
||||||
|
printf("%d ",ARRAY[i]);
|
||||||
|
MEAN=MEAN+ARRAY[i];
|
||||||
|
}
|
||||||
|
MEAN=MEAN/(float)ARRAY_LENGTH;
|
||||||
|
|
||||||
|
for(i=0;i<ARRAY_LENGTH;i++)
|
||||||
|
VARIANCE=VARIANCE+(pow((ARRAY[i]-MEAN),2));
|
||||||
|
|
||||||
|
VARIANCE=VARIANCE/(float)ARRAY_LENGTH;
|
||||||
|
STAND=sqrt(VARIANCE);
|
||||||
|
|
||||||
|
|
||||||
|
printf("\n\n- Mean is: %f\n",MEAN);
|
||||||
|
printf("- Variance is: %f\n",VARIANCE);
|
||||||
|
printf("- Standard Deviation is: %f\n",STAND);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user