Merge pull request #3 from kvedala/project_euler/master2

Project euler/master2
This commit is contained in:
Krishna Vedala 2020-04-08 21:26:01 -04:00 committed by GitHub
commit 40531e65c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
47 changed files with 1270 additions and 933 deletions

View File

@ -13,12 +13,15 @@ include_directories(function_timer/include)
# include_directories(${CMAKE_BINARY_DIR}/include)
# link_directories(${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE})
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
add_subdirectory(conversions)
add_subdirectory(misc)
add_subdirectory(project_euler)
add_subdirectory(sorting)
add_subdirectory(searching)
add_subdirectory(numerical_methods)
if(USE_OPENMP)
find_package(OpenMP)

View File

@ -5,15 +5,6 @@
* [Udp Client](https://github.com/TheAlgorithms/C/blob/master/client_server/UDP_Client.c)
* [Udp Server](https://github.com/TheAlgorithms/C/blob/master/client_server/UDP_Server.c)
## Computer Oriented Statistical Methods
* [Gauss Elimination](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/Gauss_Elimination.c)
* [Lagrange Theorem](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/lagrange_theorem.C)
* [Mean](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/MEAN.C)
* [Median](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/MEDIAN.C)
* [Seidal](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/Seidal.C)
* [Simpson'S 1-3Rd Rule](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/simpson's%201-3rd%20rule.c)
* [Variance](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/variance.c)
## Conversions
* [Binary To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/binary_to_decimal.c)
* [Binary To Hexadecimal](https://github.com/TheAlgorithms/C/blob/master/conversions/binary_to_hexadecimal.c)
@ -206,6 +197,16 @@
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C/blob/master/misc/Tower_Of_Hanoi.c)
* [Union Find](https://github.com/TheAlgorithms/C/blob/master/misc/union_Find.c)
## Numerical Methods
* [Gauss Elimination](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/Gauss_Elimination.c)
* [Lagrange Theorem](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/lagrange_theorem.C)
* [Mean](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/MEAN.C)
* [Median](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/MEDIAN.C)
* [Newton-Raphson-Root](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/newton-raphson-root.c)
* [Seidal](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/Seidal.C)
* [Simpsons 1-3Rd Rule](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/simpsons_1-3rd%20rule.c)
* [Variance](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/variance.c)
## Project Euler
* Problem 01
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2001/sol1.c)
@ -257,6 +258,8 @@
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2023/sol2.c)
* Problem 25
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2025/sol1.c)
* Problem 26
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2026/sol1.c)
## Searching
* [Binary Search](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c)
@ -296,4 +299,5 @@
* [Selection Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Selection_Sort.c)
* [Shaker Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/shaker_sort.c)
* [Shell Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/shell_Sort.c)
* [Shell Sort2](https://github.com/TheAlgorithms/C/blob/master/sorting/shell_Sort2.c)
* [Stooge Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Stooge_Sort.c)

View File

@ -6,6 +6,7 @@
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
@ -13,7 +14,8 @@ void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
for (;;)
{
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
@ -23,7 +25,8 @@ void func(int sockfd)
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
if ((strncmp(buff, "exit", 4)) == 0)
{
printf("Client Exit...\n");
break;
}
@ -37,7 +40,8 @@ int main()
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
@ -51,7 +55,8 @@ int main()
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
{
printf("connection with the server failed...\n");
exit(0);
}

View File

@ -1,100 +0,0 @@
#include<stdio.h>
#include<conio.h>
void display(float a[20][20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
{
printf("%.2f \t",a[i][j]);
}
printf("\n");
}
}
float interchange(float m[20][20],int i,int n)
{
float tmp[20][20];
float max=fabs(m[i][i]);
int j,k=i;
for(j=i;j<n;j++)
{
if(max<fabs(m[j][i]))
{
max=fabs(m[j][i]);
k=j;
}
}
for(j=0;j<=n;j++)
{
tmp[i][j]=m[i][j];
m[i][j]=m[k][j];
m[k][j]=tmp[i][j];
}
return m[20][20];
}
float eliminate(float m[20][20],int i,int n)
{
float tmp;
int k=1,l,j;
for(j=i;j<n-1;j++)
{
tmp=-((m[i+k][i])/(m[i][i]));
for(l=0;l<=n;l++)
{
m[i+k][l]=(m[i+k][l])+(m[i][l]*tmp);
}
k++;
}
return m[20][20];
}
void main()
{
int i,j,n,k=0,l;
float m[20][20],mul,tmp[20][20],val,ans[20];
clrscr();
printf("Total No.of Equations : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter Co-efficient Of Equations %d & Total --->>>\n",i+1);
for(j=0;j<=n;j++)
{
printf("r%d%d : ",i,j);
scanf("%f",&m[i][j]);
}
printf("\n");
}
printf(":::::::::::: Current Matrix ::::::::::::\n\n");
display(m,n);
for(i=0;i<n-1;i++)
{
printf("\n------->>>>>>>>>>>>>>>>>>>>>>>>-------- %d\n",i+1);
m[20][20]=interchange(m,i,n);
display(m,n);
printf("\n_______________________________________\n");
m[20][20]=eliminate(m,i,n);
display(m,n);
}
printf("\n\n Values are : \n");
for(i=n-1;i>=0;i--)
{
l=n-1;
mul=0;
for(j=0;j<k;j++)
{
mul=mul+m[i][l]*ans[l];
l--;
}
k++;
ans[i]=(m[i][n]-mul)/m[i][i];
printf("X%d = %.2f\n",i+1,ans[i]);
}
getch();
}

View File

@ -1,46 +0,0 @@
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10],n,i,j,temp,sum=0;
float mean;
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);
getch();
}

View File

@ -1,50 +0,0 @@
#include<stdio.h>
//#include<conio.h>
#include<math.h>
void main()
{
int a[10],n,i,j,temp;
float mean,median;
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]);
}
if(n%2==0)
{
median=(a[n/2]+a[(n/2)-1])/2;
}
else
{
median=a[n/2];
}
printf("\nMedian is : %f",median);
getch();
}

View File

@ -1,26 +0,0 @@
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3,x1,x2,x3;
clrscr();
printf("Enter values of eq1:");
scanf("%f%f%f%f",&a1,&a2,&a3,&d1);
printf("Enter values of eq2:");
scanf("%f%f%f%f",&b1,&b2,&b3,&d2);
printf("Enter values of eq3:");
scanf("%f%f%f%f",&c1,&c2,&c3,&d3);
x1=x2=x3=0.0;
do
{
a=x1;
b=x2;
c=x3;
x1=(1/a1)*(d1-(a2*x2)-(a3*x3));
x2=(1/b2)*(d2-(b1*x1)-(b3*x3));
x3=(1/c3)*(d3-(c1*x1)-(c2*x2));
}while(fabs(x1-a)>0.0001&&fabs(x2-b)>0.0001&&fabs(x3-c)>0.0001);
printf("x1=%f\nx2=%f\nx3=%f",x1,x2,x3);
getch();
}

View File

@ -1,45 +0,0 @@
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float x[20],y[20],a,sum,p;
int n,i,j;
clrscr();
printf("Enter the no of entry to insert->");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of x%d->",i);
scanf("%f",&x[i]);
printf("enter the value of y%d->",i);
scanf("%f",&y[i]);
}
printf("\n X \t\t Y \n");
printf("----------------------------\n");
for(i=0;i<n;i++)
{
printf("%f\t",x[i]);
printf("%f\n",y[i]);
}
printf("\nenter the value of x for interpolation:");
scanf("%f",&a)
sum=0;
for(i=0;i<n;i++)
{
p=1.0;
for(j=0;j<n;j++)
{
if(i !=j)
{
p=p*(a-x[j])/(x[i]-x[j]);
}
sum=sum+y[i]*p;
}
printf("ans is->%f",sum);
getch();
}}

View File

@ -1,41 +0,0 @@
#include<stdio.h>
#include<math.h>
float f(float x)
{
return 1.0+x*x*x; //This is the expresion of the function to integrate?
}
void main()
{
int i,n;
float a,b,h,x,s2,s3,sum,integral;
printf("enter the lower limit of the integration:");
scanf("%f",&a);
printf("enter the upper limit of the integration:");
scanf("%f",&b);
printf("enter the number of intervals:");
scanf("%d",&n);
h=(b-a)/n;
sum=f(a)+f(b);
s2=s3=0.0;
for(i=1;i<n;i+=3)
{
x=a+i*h;
s3=s3+f(x)+f(x+h);
}
for(i=3;i<n;i+=3)
{
x=a+i*h;
s2=s2+f(x);
}
integral=(h/3.0)*(sum+2*s2+4*s3);
printf("\nValue of the integral = %9.4f\n",integral);
return 0;
}

View File

@ -1,3 +1,6 @@
if(USE_OPENMP)
find_package(OpenMP)
endif(USE_OPENMP)
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
@ -7,10 +10,16 @@ file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".c" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} function_timer )
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
if(OpenMP_C_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_C)
endif()
install(TARGETS ${testname} DESTINATION "bin/conversions")
endforeach( testsourcefile ${APP_SOURCES} )

View File

@ -18,11 +18,12 @@ int decimal_to_octal(int decimal)
return ((decimal_to_octal(decimal / 8) * 10) + decimal % 8);
}
}
void main()
int main()
{
int octalNumber, decimalNumber;
printf("\nEnter your decimal number : ");
scanf("%d", &decimalNumber);
octalNumber = decimal_to_octal(decimalNumber);
printf("\nThe octal of %d is : %d", decimalNumber, octalNumber);
return 0;
}

View File

@ -4,13 +4,14 @@
int main()
{
char hex[17];
#define MAX_STR_LEN 17
char hex[MAX_STR_LEN];
long long octal, bin, place;
int i = 0, rem, val;
/* Input hexadecimal number from user */
printf("Enter any hexadecimal number: ");
gets(hex);
fgets(hex, MAX_STR_LEN, stdin);
octal = 0ll;
bin = 0ll;

@ -1 +1 @@
Subproject commit 327ddab3e895c26026eeb39ed7e0b44d82597137
Subproject commit e0dc782b7b0f162299d10d94b0132f111d89c22f

View File

@ -1,3 +1,8 @@
if(USE_OPENMP)
find_package(OpenMP)
endif()
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
@ -11,6 +16,9 @@ foreach( testsourcefile ${APP_SOURCES} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} function_timer )
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
if(OpenMP_C_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_C)
endif()
install(TARGETS ${testname} DESTINATION "bin/misc")
endforeach( testsourcefile ${APP_SOURCES} )

View File

@ -2,19 +2,31 @@
#include <stdlib.h>
#include <time.h>
void swap(int *a, int *b)
{int t;t =*a;*a=*b;*b=t;}
{
int t;
t = *a;
*a = *b;
*b = t;
}
int part(int a[], int l, int r, int n, int pivot, int pindex)
{int p1=l,p2=r;
{
int p1 = l, p2 = r;
while (p2 > p1)
{
if (a[p1] > pivot && a[p2] < pivot)
{swap(&a[p1],&a[p2]);}
{
swap(&a[p1], &a[p2]);
}
else
{
if (a[p1] <= pivot)
{p1++;}
{
p1++;
}
if (a[p2] >= pivot)
{p2--;}
{
p2--;
}
}
}
swap(&a[pindex], &a[p2]);
@ -30,25 +42,36 @@ int rselect(int a[],int l,int r,int n,int o)
pactual = part(a, l, r, n, pivot, pindex);
if (pactual == o)
{return a[pactual];}
{
return a[pactual];
}
if (o < pactual)
{rselect(a,l,pactual-1,n,o);}
{
rselect(a, l, pactual - 1, n, o);
}
if (o > pactual)
{rselect(a,pactual+1,r,n,o-pactual);}
{
rselect(a, pactual + 1, r, n, o - pactual);
}
}
if (r == l)
{return a[l];}
{
return a[l];
}
return -1;
}
int main()
{srand(time(NULL));
{
srand(time(NULL));
int n, o, i, *a;
scanf("%d %d", &n, &o);
a = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{scanf("%d",a+i);}
{
scanf("%d", a + i);
}
printf("\n\n%d", rselect(a, 0, n - 1, n, o));
return 0;
}

View File

@ -7,7 +7,6 @@
#include <stdio.h>
void strng(int a)
{
int j = a;
@ -29,10 +28,11 @@ void strng(int a)
else
printf("%d is not a strong number", j);
}
void main()
int main()
{
int a;
printf("Enter the number to check");
scanf("%d", &a);
strng(a);
return 0;
}

View File

@ -0,0 +1,26 @@
if(USE_OPENMP)
find_package(OpenMP)
endif(USE_OPENMP)
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".c" "" testname ${testsourcefile} )
string( REPLACE " " "_" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} function_timer )
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
if(OpenMP_C_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_C)
endif()
install(TARGETS ${testname} DESTINATION "bin/stats")
endforeach( testsourcefile ${APP_SOURCES} )

View File

@ -0,0 +1,104 @@
#include <stdio.h>
#include <math.h>
#define ARRAY_SIZE 20
void display(float a[ARRAY_SIZE][ARRAY_SIZE], int n)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j <= n; j++)
{
printf("%.2f \t", a[i][j]);
}
printf("\n");
}
}
float interchange(float m[ARRAY_SIZE][ARRAY_SIZE], int i, int n)
{
float tmp[ARRAY_SIZE][ARRAY_SIZE];
float max = fabs(m[i][i]);
int j, k = i;
for (j = i; j < n; j++)
{
if (max < fabs(m[j][i]))
{
max = fabs(m[j][i]);
k = j;
}
}
for (j = 0; j <= n; j++)
{
tmp[i][j] = m[i][j];
m[i][j] = m[k][j];
m[k][j] = tmp[i][j];
}
return m[ARRAY_SIZE - 1][ARRAY_SIZE - 1];
}
float eliminate(float m[ARRAY_SIZE][ARRAY_SIZE], int i, int n)
{
float tmp;
int k = 1, l, j;
for (j = i; j < n - 1; j++)
{
tmp = -((m[i + k][i]) / (m[i][i]));
for (l = 0; l <= n; l++)
{
m[i + k][l] = (m[i + k][l]) + (m[i][l] * tmp);
}
k++;
}
return m[ARRAY_SIZE - 1][ARRAY_SIZE - 1];
}
int main(void)
{
int i, j, n, k = 0, l;
float m[ARRAY_SIZE][ARRAY_SIZE], mul, tmp[ARRAY_SIZE][ARRAY_SIZE], val, ans[ARRAY_SIZE];
printf("Total No.of Equations : ");
scanf("%d", &n);
printf("\n");
for (i = 0; i < n; i++)
{
printf("Enter Co-efficient Of Equations %d & Total --->>>\n", i + 1);
for (j = 0; j <= n; j++)
{
printf("r%d%d : ", i, j);
scanf("%f", &m[i][j]);
}
printf("\n");
}
printf(":::::::::::: Current Matrix ::::::::::::\n\n");
display(m, n);
for (i = 0; i < n - 1; i++)
{
printf("\n------->>>>>>>>>>>>>>>>>>>>>>>>-------- %d\n", i + 1);
m[ARRAY_SIZE - 1][ARRAY_SIZE - 1] = interchange(m, i, n);
display(m, n);
printf("\n_______________________________________\n");
m[ARRAY_SIZE - 1][ARRAY_SIZE - 1] = eliminate(m, i, n);
display(m, n);
}
printf("\n\n Values are : \n");
for (i = n - 1; i >= 0; i--)
{
l = n - 1;
mul = 0;
for (j = 0; j < k; j++)
{
mul = mul + m[i][l] * ans[l];
l--;
}
k++;
ans[i] = (m[i][n] - mul) / m[i][i];
printf("X%d = %.2f\n", i + 1, ans[i]);
}
return 0;
}

39
numerical_methods/MEAN.C Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_LEN INT_MAX
int main(int argc, char **argv)
{
int a[MAX_LEN], n = 10, i, j, temp, sum = 0;
float mean;
if (argc == 2)
{
n = atoi(argv[1]);
if (n >= MAX_LEN)
{
fprintf(stderr, "Maximum %d!\n", MAX_LEN);
return 1;
}
}
printf("Random Numbers Generated are : ");
for (i = 0; i < n; i++)
{
a[i] = rand() % 100;
printf("%2d, ", a[i]);
}
putchar('\n');
for (i = 0; i < n; i++)
sum = sum + a[i];
mean = sum / (float)n;
printf("\nMean :");
printf("%f", mean);
return 0;
}

View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a[10], n, i, j, temp;
float mean, median;
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]);
}
if (n % 2 == 0)
{
median = (a[n / 2] + a[(n / 2) - 1]) / 2;
}
else
{
median = a[n / 2];
}
printf("\nMedian is : %f", median);
return 0;
}

View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3, x1, x2, x3;
printf("Enter values of eq1:");
scanf("%f%f%f%f", &a1, &a2, &a3, &d1);
printf("Enter values of eq2:");
scanf("%f%f%f%f", &b1, &b2, &b3, &d2);
printf("Enter values of eq3:");
scanf("%f%f%f%f", &c1, &c2, &c3, &d3);
x1 = x2 = x3 = 0.0;
do
{
a = x1;
b = x2;
c = x3;
x1 = (1 / a1) * (d1 - (a2 * x2) - (a3 * x3));
x2 = (1 / b2) * (d2 - (b1 * x1) - (b3 * x3));
x3 = (1 / c3) * (d3 - (c1 * x1) - (c2 * x2));
} while (fabs(x1 - a) > 0.0001 && fabs(x2 - b) > 0.0001 && fabs(x3 - c) > 0.0001);
printf("x1=%f\nx2=%f\nx3=%f", x1, x2, x3);
return 0;
}

View File

@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float x[20], y[20], a, sum, p;
int n, i, j;
printf("Enter the no of entry to insert->");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("enter the value of x%d->", i);
scanf("%f", &x[i]);
printf("enter the value of y%d->", i);
scanf("%f", &y[i]);
}
printf("\n X \t\t Y \n");
printf("----------------------------\n");
for (i = 0; i < n; i++)
{
printf("%f\t", x[i]);
printf("%f\n", y[i]);
}
printf("\nenter the value of x for interpolation:");
scanf("%f", &a);
sum = 0;
for (i = 0; i < n; i++)
{
p = 1.0;
for (j = 0; j < n; j++)
{
if (i != j)
{
p = p * (a - x[j]) / (x[i] - x[j]);
}
sum = sum + y[i] * p;
}
printf("ans is->%f", sum);
return 0;
}
}

View File

@ -0,0 +1,68 @@
/***
* approximate solution for f(x) = 0
* given f(x) and f'(x)
**/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <complex.h> /* requires minimum of C99 */
/**
* f(x)
*/
double complex function(double complex x)
{
return x * x - 3.; /* x^2 = 3 - solution is sqrt(3) */
// return x * x - 2.; /* x^2 = 2 - solution is sqrt(2) */
}
/**
* f'(x)
*/
double complex d_function(double complex x)
{
return 2. * x;
}
int main(int argc, char **argv)
{
const double accuracy = 1e-10;
double delta = 1;
double complex cdelta = 1;
/* initialize random seed: */
srand(time(NULL));
double complex root = (rand() % 100 - 50) + (random() % 100 - 50) * I;
unsigned long counter = 0;
while (delta > accuracy && counter < ULONG_MAX)
{
cdelta = function(root) / d_function(root);
root += -cdelta;
counter++;
delta = fabs(cabs(cdelta));
#if defined(DEBUG) || !defined(NDEBUG)
if (counter % 50 == 0)
{
double r = creal(root);
double c = cimag(root);
printf("Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter, r,
c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta);
}
#endif
}
double r = creal(root);
double c = fabs(cimag(root)) < accuracy ? 0 : cimag(root);
printf("Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter, r,
c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta);
return 0;
}

View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <math.h>
float f(float x)
{
return 1.0 + x * x * x; //This is the expresion of the function to integrate?
}
int main()
{
int i, n;
float a, b, h, x, s2, s3, sum, integral;
printf("enter the lower limit of the integration:");
scanf("%f", &a);
printf("enter the upper limit of the integration:");
scanf("%f", &b);
printf("enter the number of intervals:");
scanf("%d", &n);
h = (b - a) / n;
sum = f(a) + f(b);
s2 = s3 = 0.0;
for (i = 1; i < n; i += 3)
{
x = a + i * h;
s3 = s3 + f(x) + f(x + h);
}
for (i = 3; i < n; i += 3)
{
x = a + i * h;
s2 = s2 + f(x);
}
integral = (h / 3.0) * (sum + 2 * s2 + 4 * s3);
printf("\nValue of the integral = %9.4f\n", integral);
return 0;
}

View File

@ -6,47 +6,61 @@ e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
#include <stdio.h>
#include <math.h>
int isprime(int no) {
int isprime(int no)
{
int sq;
if (no == 2) {
if (no == 2)
{
return 1;
}
else if (no%2 == 0) {
else if (no % 2 == 0)
{
return 0;
}
sq = ((int)(sqrt(no))) + 1;
for (int i = 3; i < sq; i + 2) {
if (no%i == 0) {
for (int i = 3; i < sq; i += 2)
{
if (no % i == 0)
{
return 0;
}
}
return 1;
}
int main() {
int main()
{
int maxNumber = 0;
int n = 0;
int n1;
scanf("%d", &n);
if (isprime(n) == 1)
printf("%d", n);
else {
while (n % 2 == 0) {
else
{
while (n % 2 == 0)
{
n = n / 2;
}
if (isprime(n) == 1) {
if (isprime(n) == 1)
{
printf("%d\n", n);
}
else {
else
{
n1 = ((int)(sqrt(n))) + 1;
for (int i = 3; i < n1; i + 2) {
if (n%i == 0) {
if (isprime((int)(n / i)) == 1) {
for (int i = 3; i < n1; i += 2)
{
if (n % i == 0)
{
if (isprime((int)(n / i)) == 1)
{
maxNumber = n / i;
break;
}
else if (isprime(i) == 1) {
else if (isprime(i) == 1)
{
maxNumber = i;
}
}

View File

@ -1,28 +1,34 @@
#include <stdio.h>
unsigned long gcd(unsigned long a, unsigned long b) {
unsigned long gcd(unsigned long a, unsigned long b)
{
unsigned long r;
if (a > b) {
if (a > b)
{
unsigned long t = a;
a = b;
b = t;
}
while (r = a % b) {
while ((r = (a % b)))
{
a = b;
b = r;
}
return b;
}
unsigned long lcm(unsigned long a, unsigned long b) {
unsigned long lcm(unsigned long a, unsigned long b)
{
unsigned long long p = (unsigned long long)a * b;
return p / gcd(a, b);
}
int main(void) {
int main(void)
{
unsigned long ans = 1;
unsigned long i;
for (i = 1; i <= 20; i++) {
for (i = 1; i <= 20; i++)
{
ans = lcm(ans, i);
}
printf("%lu\n", ans);

View File

@ -1,12 +1,11 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int64_t get_product(FILE *fp, long start_pos, int num_digits)
long long int get_product(FILE *fp, long start_pos, int num_digits)
{
char ch = ' '; /* temporary variable to store character read from file */
uint8_t num = 0; /* temporary variable to store digit read */
int64_t prod = 1; /* product accumulator */
unsigned char num = 0; /* temporary variable to store digit read */
long long int prod = 1; /* product accumulator */
int count = 0; /* we use this variable to count number of bytes of file read */
/* accumulate product for num_digits */
@ -49,7 +48,7 @@ int main(int argc, char *argv[])
{
int position = 0;
int num_digits = 4;
int64_t prod, max_prod = 0;
long long int prod, max_prod = 0;
/* if second command-line argument is ge=iven,
* use it as the number of digits to compute

View File

@ -1,6 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h> /* for memmove */
int main(int argc, char *argv[])
@ -8,9 +7,9 @@ int main(int argc, char *argv[])
int position = 0, num_bad_chars = 0;
int num_digits = 4;
char ch;
uint8_t num, num_prev;
uint8_t *buffer = NULL;
int64_t prod = 1, max_prod = 0;
unsigned char num, num_prev;
unsigned char *buffer = NULL;
long long int prod = 1, max_prod = 0;
/* if second command-line argument is given,
* use it as the number of digits to compute
@ -20,7 +19,7 @@ int main(int argc, char *argv[])
num_digits = atoi(argv[1]);
/* allocate memory to store past values */
buffer = calloc(num_digits, sizeof(uint8_t));
buffer = calloc(num_digits, sizeof(unsigned char));
if (!buffer)
{
perror("Unable to allocate memory for buffer");

View File

@ -3,9 +3,6 @@
#ifdef _OPENMP
#include <omp.h>
#pragma message ("Using OpenMP parallelization")
#else
#pragma message ("Not using OpenMP parallelization")
#endif
/**

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>

View File

@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#define MAX_DENO 2000
#define MAX_LEN (MAX_DENO + 10)
int compare(const void *a, const void *b)
{
return (*(unsigned short *)a - *(unsigned short *)b);
}
int main(int argc, char *argv[])
{
unsigned short max_digits = 0, max_idx_number = 0;
clock_t start_time = clock();
#ifdef _OPENMP
#pragma omp for
#endif
for (unsigned short deno = 2; deno < MAX_DENO; deno++)
{
unsigned short remainders[MAX_LEN];
unsigned short rem = 1, *rem_ptr = remainders;
memset(remainders, (unsigned short)-1, MAX_LEN * sizeof(unsigned short));
// remainders[0] = 1;
// printf("1/%-4u\t ", deno);
unsigned short index = 0, num_digits;
while (rem != 0)
{
rem = (rem * 10) % deno;
if (rem == 0)
{
index = 0;
break;
}
rem_ptr = (unsigned short *)bsearch(&rem, remainders, MAX_LEN, sizeof(unsigned short), compare);
// printf("%2d, ", rem);
// printf("(%14p), ", rem_ptr);
if (rem_ptr != NULL)
break;
remainders[index] = rem;
rem_ptr = remainders;
index++;
}
num_digits = index - (rem_ptr - remainders);
// printf("\n\t(%14p, %14p, %4u, %4u)\n", rem_ptr, remainders, index, num_digits);
#ifdef _OPENMP
#pragma omp critical
{
#endif
if (num_digits > max_digits)
{
max_digits = num_digits;
max_idx_number = deno;
// printf("\t (%u, %u)\n ", max_digits, max_idx_number);
}
#ifdef _OPENMP
}
#endif
}
clock_t end_time = clock();
printf("Time taken: %.4g ms\n", 1e3 * (double)(end_time - start_time) / CLOCKS_PER_SEC);
printf("Maximum digits: %hu\t Denominator: %hu\n", max_digits, max_idx_number);
return 0;
}

23
searching/CMakeLists.txt Normal file
View File

@ -0,0 +1,23 @@
if(USE_OPENMP)
find_package(OpenMP)
endif()
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".c" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} function_timer )
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
if(OpenMP_C_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_C)
endif()
install(TARGETS ${testname} DESTINATION "bin/searching")
endforeach( testsourcefile ${APP_SOURCES} )

View File

@ -1,22 +1,26 @@
#include <stdio.h>
int linearsearch(int *arr, int size, int val){
int linearsearch(int *arr, int size, int val)
{
int i;
for (i = 0; i < size; i++){
for (i = 0; i < size; i++)
{
if (arr[i] == val)
return 1;
}
return 0;
}
void main(){
int main()
{
int n, i, v;
printf("Enter the size of the array:\n");
scanf("%d", &n); //Taking input for the size of Array
int a[n];
printf("Enter the contents for an array of size %d:\n", n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);// accepts the values of array elements until the loop terminates//
for (i = 0; i < n; i++)
scanf("%d", &a[i]); // accepts the values of array elements until the loop terminates//
printf("Enter the value to be searched:\n");
scanf("%d", &v); //Taking input the value to be searched
@ -24,4 +28,5 @@ void main(){
printf("Value %d is in the array.\n", v);
else
printf("Value %d is not in the array.\n", v);
return 0;
}

View File

@ -2,7 +2,6 @@
#include <stdlib.h>
#define len 5
int binarySearch(int array[], int leng, int searchX)
{
int pos = -1, right, left, i = 0;
@ -29,8 +28,7 @@ int binarySearch(int array[], int leng, int searchX)
return -1; /* not found */
}
void main(int argc, char *argv[])
int main(int argc, char *argv[])
{
int array[len] = {5, 8, 10, 14, 16};
@ -44,5 +42,5 @@ void main(int argc, char *argv[])
printf("The number %d exist in array at position : %d \n", 5, position);
}
return 0;
}

View File

@ -0,0 +1,23 @@
if(USE_OPENMP)
find_package(OpenMP)
endif()
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".c" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} function_timer )
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
if(OpenMP_C_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_C)
endif()
install(TARGETS ${testname} DESTINATION "bin/searching/pattern")
endforeach( testsourcefile ${APP_SOURCES} )

View File

@ -1,14 +0,0 @@
CC = gcc
FLAG = -o
all: naive_search rabin_karp_search boyer_moore_search
naive_search : naive_search.c
$(CC) $(FLAG) naive_search naive_search.c
rabin_karp_search : rabin_karp_search
$(CC) $(FLAG) rabin_karp_search rabin_karp_search.c
boyer_moore_search: boyer_moore_search boyer_moore_search.c
$(CC) $(FLAG) boyer_moore_search boyer_moore_search.c
clean:
rm naive_search rabin_karp_search boyer_moore_search

View File

@ -4,24 +4,20 @@
#define TRUE 1
#define FALSE 0
int main()
{
int i, arraySort[MAX] = {0}, isSort = FALSE, changePlace;
/* For example
Insertion random values in array to test
*/
for (i = 0; i < MAX; i++)
{
arraySort[i] = rand() % 101;
}
/* Algorithm of bubble methods */
while (isSort)
@ -32,12 +28,11 @@ int main()
{
if (arraySort[i] > arraySort[i + 1])
{
changePlace = arratSort[i];
changePlace = arraySort[i];
arraySort[i] = arraySort[i + 1];
arraySort[i + 1] = changePlace;
isSort = TRUE;
}
}
}
@ -48,9 +43,5 @@ int main()
printf("%d\n", arraySort[i]);
}
return EXIT_SUCCESS;
}

23
sorting/CMakeLists.txt Normal file
View File

@ -0,0 +1,23 @@
if(USE_OPENMP)
find_package(OpenMP)
endif()
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".c" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} function_timer )
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
if(OpenMP_C_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_C)
endif()
install(TARGETS ${testname} DESTINATION "bin/sorting")
endforeach( testsourcefile ${APP_SOURCES} )

View File

@ -30,7 +30,7 @@ int findMax(int arr[], int n)
}
// Sorts the array using flip operations
int pancakeSort(int *arr, int n)
void pancakeSort(int *arr, int n)
{
// Start from the complete array and one by one reduce current size by one
for (int curr_size = n; curr_size > 1; --curr_size)
@ -61,18 +61,21 @@ void display(int arr[], int n)
printf("\n");
}
#define N 50
// Driver program to test above function
int main()
{
int arr[] = {23, 10, 20, 11, 12, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);
int arr[N];
for (int i = 0; i < N; i++)
arr[i] = rand() % (N << 1); /* random numbers from 0 to 2N */
printf("Original array: ");
display(arr, n);
display(arr, N);
pancakeSort(arr, n);
pancakeSort(arr, N);
printf("Sorted array: ");
display(arr, n);
display(arr, N);
return 0;
}

View File

@ -1,7 +1,7 @@
#include <stdio.h>
void stoogesort(int[], int, int);
void main()
int main()
{
int arr[100], i, n;
@ -16,9 +16,9 @@ void main()
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
void stoogesort(int arr[], int i, int j)
{
int temp, k;

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b) //To swap the variables//
{
@ -6,22 +7,27 @@ void swap (int *a,int *b)//To swap the variables//
t = *a;
*a = *b;
*b = t;
}
void merge(int a[], int l, int r, int n) //To merge //
{ int *b = (int*)malloc(n*sizeof(int));
{
int *b = (int *)malloc(n * sizeof(int));
int c = l;
int p1, p2;
p1 = l;p2=((l+r)/2)+1;
p1 = l;
p2 = ((l + r) / 2) + 1;
while ((p1 < ((l + r) / 2) + 1) && (p2 < r + 1))
{ if(a[p1] <= a[p2])
{ b[c++] = a[p1];
{
if (a[p1] <= a[p2])
{
b[c++] = a[p1];
p1++;
}
else
{b[c++] = a[p2];p2++;}
{
b[c++] = a[p2];
p2++;
}
};
if (p2 == r + 1)
@ -31,7 +37,6 @@ int c=l;
b[c++] = a[p1];
p1++;
};
}
else
{
@ -44,27 +49,27 @@ p2++;
for (c = l; c < r - l + 1; c++)
a[c] = b[c];
}
void mergesort(int a[],int n,int l,int r)
void merge_sort(int *a, int n, int l, int r)
{
if (r - l == 1)
{
if (a[l] > a[r])
swap(&a[l], &a[r]);
}
else if (l == r)
{}
{
}
else
{mergesort(a,n,l,(l+r)/2);
mergesort(a,n,((l+r)/2)+1,r);
{
merge_sort(a, n, l, (l + r) / 2);
merge_sort(a, n, ((l + r) / 2) + 1, r);
merge(a, l, r, n);
}
}
int main(void) { //main function//
int main(void)
{ //main function//
int *a, n, i;
scanf("%d", &n);
a = (int *)malloc(n * sizeof(int));
@ -72,20 +77,16 @@ for (i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
mergesort(a,n,0,n-1);
merge_sort(a, n, 0, n - 1);
for (i = 0; i < n; i++)
{
printf(" %d",&a[i]);
printf(" %d", a[i]);
}
free(a);
return 0;
}

View File

@ -259,7 +259,7 @@ void insert2(char *s)
Tptr pp, *p;
p = &root;
while (pp = *p)
while (pp == *p)
{
if ((d = *s - pp->splitchar) == 0)
{
@ -390,7 +390,6 @@ void nearsearch(Tptr p, char *s, int d)
nearsearch(p->hikid, s, d);
}
#define NUMBER_OF_STRING 3
int main(int argc, char *argv[])

View File

@ -4,9 +4,11 @@
#define range 10 // Range for integers is 10 as digits range from 0-9
// Utility function to get the maximum value in ar[]
int MAX(int ar[], int size){
int MAX(int ar[], int size)
{
int i, max = ar[0];
for(i = 0; i<size; i++){
for (i = 0; i < size; i++)
{
if (ar[i] > max)
max = ar[i];
}
@ -49,13 +51,21 @@ void radixsort(int arr[],int n,int max) //max is the maximum element
int mul = 1;
while (max)
{
countsort(arr,n,mul);
countSort(arr, n, mul);
mul *= 10;
max /= 10;
}
}
int main(int argc, const char * argv[]){
void display(int *arr, int N)
{
for (int i = 0; i < N; i++)
printf("%d, ", arr[i]);
putchar('\n');
}
int main(int argc, const char *argv[])
{
int n;
printf("Enter size of array:\n");
scanf("%d", &n); // E.g. 8
@ -63,7 +73,8 @@ int main(int argc, const char * argv[]){
printf("Enter the elements of the array\n");
int i;
int arr[n];
for(i = 0; i < n; i++){
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
@ -79,6 +90,4 @@ int main(int argc, const char * argv[]){
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
return 0;
}

View File

@ -37,38 +37,10 @@ void shellSort(int array[], int len)
swap(&array[j], &array[j + gap]);
}
/**
* Optimized algorithm - takes half the time as other
**/
void shell_sort2(int array[], int LEN)
{
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
const int gap_len = 8;
int i, j, g;
for (g = 0; g < gap_len; g++)
{
int gap = gaps[g];
for (i = gap; i < LEN; i++)
{
int tmp = array[i];
for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
array[j] = array[j - gap];
array[j] = tmp;
}
}
#ifdef DEBUG
for (i = 0; i < LEN; i++)
printf("%s\t", data[i]);
#endif
}
int main(int argc, char *argv[])
{
int i;
int array[ELEMENT_NR];
int array2[ELEMENT_NR];
int range = 500;
int size;
clock_t start, end;
@ -76,10 +48,7 @@ int main(int argc, char *argv[])
srand(time(NULL));
for (i = 0; i < ELEMENT_NR; i++)
{
array[i] = rand() % range + 1;
array2[i] = array[i];
}
size = ARRAY_LEN(array);
@ -95,17 +64,5 @@ int main(int argc, char *argv[])
printf("%s\n", notation);
printf("Time spent sorting: %.4g ms\n", time_spent * 1e3);
printf("--------------------------\n");
start = clock();
shell_sort2(array2, size);
end = clock();
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("Data Sorted\n");
show_data(array2, size);
printf("%s\n", notation);
printf("Time spent sorting: %.4g ms\n", time_spent * 1e3);
return 0;
}

81
sorting/shell_Sort2.c Normal file
View File

@ -0,0 +1,81 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "function_timer.h"
#define ELEMENT_NR 20000
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
void show_data(int arr[], int len)
{
int i;
for (i = 0; i < len; i++)
printf("%3d ", arr[i]);
printf("\n");
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* Optimized algorithm - takes half the time as other
**/
void shell_sort(int array[], int LEN)
{
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
const int gap_len = 8;
int i, j, g;
for (g = 0; g < gap_len; g++)
{
int gap = gaps[g];
for (i = gap; i < LEN; i++)
{
int tmp = array[i];
for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
array[j] = array[j - gap];
array[j] = tmp;
}
}
#ifdef DEBUG
for (i = 0; i < LEN; i++)
printf("%s\t", data[i]);
#endif
}
int main(int argc, char *argv[])
{
int i;
int array[ELEMENT_NR];
int range = 500;
int size;
double time_spent;
srand(time(NULL));
for (i = 0; i < ELEMENT_NR; i++)
array[i] = rand() % range + 1;
size = ARRAY_LEN(array);
function_timer *timer = new_timer();
show_data(array, size);
start_timer(timer);
shell_sort(array, size);
time_spent = end_timer(timer);
printf("Data Sorted\n");
show_data(array, size);
printf("Time spent sorting: %.4g s\n", time_spent);
return 0;
}