mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge pull request #3 from kvedala/project_euler/master2
Project euler/master2
This commit is contained in:
commit
40531e65c0
@ -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)
|
||||
|
22
DIRECTORY.md
22
DIRECTORY.md
@ -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)
|
||||
|
@ -1,66 +1,71 @@
|
||||
|
||||
// Write CPP code here
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
// Write CPP code here
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#define MAX 80
|
||||
#define PORT 8080
|
||||
#define SA struct sockaddr
|
||||
void func(int sockfd)
|
||||
{
|
||||
char buff[MAX];
|
||||
int n;
|
||||
for (;;) {
|
||||
bzero(buff, sizeof(buff));
|
||||
printf("Enter the string : ");
|
||||
n = 0;
|
||||
while ((buff[n++] = getchar()) != '\n')
|
||||
;
|
||||
write(sockfd, buff, sizeof(buff));
|
||||
bzero(buff, sizeof(buff));
|
||||
read(sockfd, buff, sizeof(buff));
|
||||
printf("From Server : %s", buff);
|
||||
if ((strncmp(buff, "exit", 4)) == 0) {
|
||||
printf("Client Exit...\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int sockfd, connfd;
|
||||
struct sockaddr_in servaddr, cli;
|
||||
|
||||
// socket create and varification
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd == -1) {
|
||||
printf("socket creation failed...\n");
|
||||
exit(0);
|
||||
}
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#define MAX 80
|
||||
#define PORT 8080
|
||||
#define SA struct sockaddr
|
||||
void func(int sockfd)
|
||||
{
|
||||
char buff[MAX];
|
||||
int n;
|
||||
for (;;)
|
||||
{
|
||||
bzero(buff, sizeof(buff));
|
||||
printf("Enter the string : ");
|
||||
n = 0;
|
||||
while ((buff[n++] = getchar()) != '\n')
|
||||
;
|
||||
write(sockfd, buff, sizeof(buff));
|
||||
bzero(buff, sizeof(buff));
|
||||
read(sockfd, buff, sizeof(buff));
|
||||
printf("From Server : %s", buff);
|
||||
if ((strncmp(buff, "exit", 4)) == 0)
|
||||
{
|
||||
printf("Client Exit...\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int sockfd, connfd;
|
||||
struct sockaddr_in servaddr, cli;
|
||||
|
||||
// socket create and varification
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd == -1)
|
||||
{
|
||||
printf("socket creation failed...\n");
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
printf("Socket successfully created..\n");
|
||||
bzero(&servaddr, sizeof(servaddr));
|
||||
|
||||
// assign IP, PORT
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
servaddr.sin_port = htons(PORT);
|
||||
|
||||
// connect the client socket to server socket
|
||||
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
|
||||
printf("connection with the server failed...\n");
|
||||
exit(0);
|
||||
}
|
||||
printf("Socket successfully created..\n");
|
||||
bzero(&servaddr, sizeof(servaddr));
|
||||
|
||||
// assign IP, PORT
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
servaddr.sin_port = htons(PORT);
|
||||
|
||||
// connect the client socket to server socket
|
||||
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
|
||||
{
|
||||
printf("connection with the server failed...\n");
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
printf("connected to the server..\n");
|
||||
|
||||
// function for chat
|
||||
func(sockfd);
|
||||
|
||||
// close the socket
|
||||
close(sockfd);
|
||||
}
|
||||
printf("connected to the server..\n");
|
||||
|
||||
// function for chat
|
||||
func(sockfd);
|
||||
|
||||
// close the socket
|
||||
close(sockfd);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
@ -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();
|
||||
}
|
@ -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();
|
||||
|
||||
}}
|
@ -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;
|
||||
}
|
@ -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} )
|
||||
|
@ -5,24 +5,25 @@
|
||||
#include <stdio.h>
|
||||
int decimal_to_octal(int decimal)
|
||||
{
|
||||
if( (decimal<8) && (decimal>0) )
|
||||
if ((decimal < 8) && (decimal > 0))
|
||||
{
|
||||
return decimal;
|
||||
}
|
||||
else if(decimal==0)
|
||||
else if (decimal == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ( (decimal_to_octal(decimal/8)*10) + decimal%8 );
|
||||
return ((decimal_to_octal(decimal / 8) * 10) + decimal % 8);
|
||||
}
|
||||
}
|
||||
void main()
|
||||
int main()
|
||||
{
|
||||
int octalNumber,decimalNumber;
|
||||
int octalNumber, decimalNumber;
|
||||
printf("\nEnter your decimal number : ");
|
||||
scanf("%d",&decimalNumber);
|
||||
scanf("%d", &decimalNumber);
|
||||
octalNumber = decimal_to_octal(decimalNumber);
|
||||
printf("\nThe octal of %d is : %d" ,decimalNumber,octalNumber);
|
||||
printf("\nThe octal of %d is : %d", decimalNumber, octalNumber);
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,81 +4,82 @@
|
||||
|
||||
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;
|
||||
place = 0ll;
|
||||
|
||||
/* Hexadecimal to binary conversion */
|
||||
for(i=0; hex[i]!='\0'; i++)
|
||||
for (i = 0; hex[i] != '\0'; i++)
|
||||
{
|
||||
bin = bin * place;
|
||||
|
||||
switch(hex[i])
|
||||
switch (hex[i])
|
||||
{
|
||||
case '0':
|
||||
bin += 0;
|
||||
break;
|
||||
case '1':
|
||||
bin += 1;
|
||||
break;
|
||||
case '2':
|
||||
bin += 10;
|
||||
break;
|
||||
case '3':
|
||||
bin += 11;
|
||||
break;
|
||||
case '4':
|
||||
bin += 100;
|
||||
break;
|
||||
case '5':
|
||||
bin += 101;
|
||||
break;
|
||||
case '6':
|
||||
bin += 110;
|
||||
break;
|
||||
case '7':
|
||||
bin += 111;
|
||||
break;
|
||||
case '8':
|
||||
bin += 1000;
|
||||
break;
|
||||
case '9':
|
||||
bin += 1001;
|
||||
break;
|
||||
case 'a':
|
||||
case 'A':
|
||||
bin += 1010;
|
||||
break;
|
||||
case 'b':
|
||||
case 'B':
|
||||
bin += 1011;
|
||||
break;
|
||||
case 'c':
|
||||
case 'C':
|
||||
bin += 1100;
|
||||
break;
|
||||
case 'd':
|
||||
case 'D':
|
||||
bin += 1101;
|
||||
break;
|
||||
case 'e':
|
||||
case 'E':
|
||||
bin += 1110;
|
||||
break;
|
||||
case 'f':
|
||||
case 'F':
|
||||
bin += 1111;
|
||||
break;
|
||||
default:
|
||||
printf("Invalid hexadecimal input.");
|
||||
case '0':
|
||||
bin += 0;
|
||||
break;
|
||||
case '1':
|
||||
bin += 1;
|
||||
break;
|
||||
case '2':
|
||||
bin += 10;
|
||||
break;
|
||||
case '3':
|
||||
bin += 11;
|
||||
break;
|
||||
case '4':
|
||||
bin += 100;
|
||||
break;
|
||||
case '5':
|
||||
bin += 101;
|
||||
break;
|
||||
case '6':
|
||||
bin += 110;
|
||||
break;
|
||||
case '7':
|
||||
bin += 111;
|
||||
break;
|
||||
case '8':
|
||||
bin += 1000;
|
||||
break;
|
||||
case '9':
|
||||
bin += 1001;
|
||||
break;
|
||||
case 'a':
|
||||
case 'A':
|
||||
bin += 1010;
|
||||
break;
|
||||
case 'b':
|
||||
case 'B':
|
||||
bin += 1011;
|
||||
break;
|
||||
case 'c':
|
||||
case 'C':
|
||||
bin += 1100;
|
||||
break;
|
||||
case 'd':
|
||||
case 'D':
|
||||
bin += 1101;
|
||||
break;
|
||||
case 'e':
|
||||
case 'E':
|
||||
bin += 1110;
|
||||
break;
|
||||
case 'f':
|
||||
case 'F':
|
||||
bin += 1111;
|
||||
break;
|
||||
default:
|
||||
printf("Invalid hexadecimal input.");
|
||||
}
|
||||
|
||||
place = 10000;
|
||||
@ -87,36 +88,36 @@ int main()
|
||||
place = 1;
|
||||
|
||||
/* Binary to octal conversion */
|
||||
while(bin > 0)
|
||||
while (bin > 0)
|
||||
{
|
||||
rem = bin % 1000;
|
||||
|
||||
switch(rem)
|
||||
switch (rem)
|
||||
{
|
||||
case 0:
|
||||
val = 0;
|
||||
break;
|
||||
case 1:
|
||||
val = 1;
|
||||
break;
|
||||
case 10:
|
||||
val = 2;
|
||||
break;
|
||||
case 11:
|
||||
val = 3;
|
||||
break;
|
||||
case 100:
|
||||
val = 4;
|
||||
break;
|
||||
case 101:
|
||||
val = 5;
|
||||
break;
|
||||
case 110:
|
||||
val = 6;
|
||||
break;
|
||||
case 111:
|
||||
val = 7;
|
||||
break;
|
||||
case 0:
|
||||
val = 0;
|
||||
break;
|
||||
case 1:
|
||||
val = 1;
|
||||
break;
|
||||
case 10:
|
||||
val = 2;
|
||||
break;
|
||||
case 11:
|
||||
val = 3;
|
||||
break;
|
||||
case 100:
|
||||
val = 4;
|
||||
break;
|
||||
case 101:
|
||||
val = 5;
|
||||
break;
|
||||
case 110:
|
||||
val = 6;
|
||||
break;
|
||||
case 111:
|
||||
val = 7;
|
||||
break;
|
||||
}
|
||||
|
||||
octal = (val * place) + octal;
|
||||
@ -129,4 +130,4 @@ int main()
|
||||
printf("Octal number = %lld", octal);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 327ddab3e895c26026eeb39ed7e0b44d82597137
|
||||
Subproject commit e0dc782b7b0f162299d10d94b0132f111d89c22f
|
@ -1,20 +1,20 @@
|
||||
/*
|
||||
author: Christian Bender
|
||||
This file contains a simple test program for each hash-function.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "hash.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char s[] = "hello";
|
||||
|
||||
/* actual tests */
|
||||
printf("sdbm: %s --> %llX\n", s, sdbm(s));
|
||||
printf("djb2: %s --> %llX\n", s, djb2(s));
|
||||
printf("xor8: %s --> %X\n", s, xor8(s)); /* 8 bit */
|
||||
printf("adler_32: %s --> %X\n", s, adler_32(s)); /* 32 bit */
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
author: Christian Bender
|
||||
This file contains a simple test program for each hash-function.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "hash.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char s[] = "hello";
|
||||
|
||||
/* actual tests */
|
||||
printf("sdbm: %s --> %llX\n", s, sdbm(s));
|
||||
printf("djb2: %s --> %llX\n", s, djb2(s));
|
||||
printf("xor8: %s --> %X\n", s, xor8(s)); /* 8 bit */
|
||||
printf("adler_32: %s --> %X\n", s, adler_32(s)); /* 32 bit */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -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} )
|
||||
|
101
misc/rselect.c
101
misc/rselect.c
@ -1,54 +1,77 @@
|
||||
#include <stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<time.h>
|
||||
void swap(int *a ,int *b)
|
||||
{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;
|
||||
while(p2>p1)
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
void swap(int *a, int *b)
|
||||
{
|
||||
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;
|
||||
while (p2 > p1)
|
||||
{
|
||||
if (a[p1] > pivot && a[p2]<pivot)
|
||||
{swap(&a[p1],&a[p2]);}
|
||||
if (a[p1] > pivot && a[p2] < pivot)
|
||||
{
|
||||
swap(&a[p1], &a[p2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (a[p1] <=pivot)
|
||||
{p1++;}
|
||||
if (a[p2]>=pivot)
|
||||
{p2--;}
|
||||
if (a[p1] <= pivot)
|
||||
{
|
||||
p1++;
|
||||
}
|
||||
if (a[p2] >= pivot)
|
||||
{
|
||||
p2--;
|
||||
}
|
||||
}
|
||||
}
|
||||
swap(&a[pindex],&a[p2]);
|
||||
swap(&a[pindex], &a[p2]);
|
||||
return p2;
|
||||
}
|
||||
int rselect(int a[],int l,int r,int n,int o)
|
||||
int rselect(int a[], int l, int r, int n, int o)
|
||||
{
|
||||
int pivot,pindex,pactual;
|
||||
if (r>l)
|
||||
int pivot, pindex, pactual;
|
||||
if (r > l)
|
||||
{
|
||||
pindex = rand()%(r-l+1);
|
||||
pivot = a[pindex];
|
||||
pactual = part(a,l,r,n,pivot,pindex);
|
||||
|
||||
if (pactual == o)
|
||||
{return a[pactual];}
|
||||
|
||||
if (o < pactual)
|
||||
{rselect(a,l,pactual-1,n,o);}
|
||||
|
||||
if (o>pactual)
|
||||
{rselect(a,pactual+1,r,n,o-pactual);}
|
||||
pindex = rand() % (r - l + 1);
|
||||
pivot = a[pindex];
|
||||
pactual = part(a, l, r, n, pivot, pindex);
|
||||
|
||||
if (pactual == o)
|
||||
{
|
||||
return a[pactual];
|
||||
}
|
||||
|
||||
if (o < pactual)
|
||||
{
|
||||
rselect(a, l, pactual - 1, n, o);
|
||||
}
|
||||
|
||||
if (o > pactual)
|
||||
{
|
||||
rselect(a, pactual + 1, r, n, o - pactual);
|
||||
}
|
||||
}
|
||||
if (r==l)
|
||||
{return a[l];}
|
||||
if (r == l)
|
||||
{
|
||||
return a[l];
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{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);}
|
||||
printf("\n\n%d",rselect(a,0,n-1,n,o));
|
||||
{
|
||||
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);
|
||||
}
|
||||
printf("\n\n%d", rselect(a, 0, n - 1, n, o));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,38 +1,38 @@
|
||||
/**
|
||||
* Modified on 07/12/2017, Kyler Smith
|
||||
*
|
||||
* A number is called strong number if sum of the
|
||||
* A number is called strong number if sum of the
|
||||
* factorial of its digit is equal to number itself.
|
||||
*/
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void strng(int a)
|
||||
{
|
||||
int j=a;
|
||||
int sum=0;
|
||||
int b,i,fact=1;
|
||||
while(a>0)
|
||||
int j = a;
|
||||
int sum = 0;
|
||||
int b, i, fact = 1;
|
||||
while (a > 0)
|
||||
{
|
||||
fact=1;
|
||||
b=a%10;
|
||||
for(i=1;i<=b;i++)
|
||||
fact = 1;
|
||||
b = a % 10;
|
||||
for (i = 1; i <= b; i++)
|
||||
{
|
||||
fact=fact*i;
|
||||
fact = fact * i;
|
||||
}
|
||||
a=a/10;
|
||||
sum=sum+fact;
|
||||
a = a / 10;
|
||||
sum = sum + fact;
|
||||
}
|
||||
if(sum==j)
|
||||
printf("%d is a strong number",j);
|
||||
if (sum == j)
|
||||
printf("%d is a strong number", j);
|
||||
else
|
||||
printf("%d is not a strong number",j);
|
||||
printf("%d is not a strong number", j);
|
||||
}
|
||||
void main()
|
||||
int main()
|
||||
{
|
||||
int a;
|
||||
printf("Enter the number to check");
|
||||
scanf("%d",&a);
|
||||
scanf("%d", &a);
|
||||
strng(a);
|
||||
return 0;
|
||||
}
|
||||
|
26
numerical_methods/CMakeLists.txt
Normal file
26
numerical_methods/CMakeLists.txt
Normal 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} )
|
104
numerical_methods/Gauss_Elimination.c
Normal file
104
numerical_methods/Gauss_Elimination.c
Normal 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
39
numerical_methods/MEAN.C
Normal 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;
|
||||
}
|
51
numerical_methods/MEDIAN.C
Normal file
51
numerical_methods/MEDIAN.C
Normal 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;
|
||||
}
|
27
numerical_methods/Seidal.C
Normal file
27
numerical_methods/Seidal.C
Normal 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;
|
||||
}
|
46
numerical_methods/lagrange_theorem.C
Normal file
46
numerical_methods/lagrange_theorem.C
Normal 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;
|
||||
}
|
||||
}
|
68
numerical_methods/newton-raphson-root.c
Normal file
68
numerical_methods/newton-raphson-root.c
Normal 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;
|
||||
}
|
41
numerical_methods/simpsons_1-3rd rule.c
Normal file
41
numerical_methods/simpsons_1-3rd rule.c
Normal 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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -1,13 +1,12 @@
|
||||
#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 */
|
||||
int count = 0; /* we use this variable to count number of bytes of file read */
|
||||
char ch = ' '; /* temporary variable to store character read from file */
|
||||
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 */
|
||||
for (int i = 0; i < num_digits; i++, count++)
|
||||
@ -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
|
||||
|
@ -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");
|
||||
|
@ -2,27 +2,24 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#pragma message ("Using OpenMP parallelization")
|
||||
#else
|
||||
#pragma message ("Not using OpenMP parallelization")
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Computes the length of collatz sequence for a given
|
||||
* starting number
|
||||
* Computes the length of collatz sequence for a given
|
||||
* starting number
|
||||
**/
|
||||
long long collatz(long long start_num)
|
||||
{
|
||||
long long length = 1;
|
||||
|
||||
while (start_num != 1) /* loop till we reach 1 */
|
||||
|
||||
while (start_num != 1) /* loop till we reach 1 */
|
||||
{
|
||||
if(start_num & 0x01) /* check for odd */
|
||||
if (start_num & 0x01) /* check for odd */
|
||||
start_num = 3 * start_num + 1;
|
||||
else
|
||||
start_num >>= 1; /* simpler divide by 2 */
|
||||
length ++;
|
||||
start_num >>= 1; /* simpler divide by 2 */
|
||||
length++;
|
||||
}
|
||||
|
||||
return length;
|
||||
@ -33,39 +30,39 @@ int main(int argc, char **argv)
|
||||
long long max_len = 0, max_len_num = 0;
|
||||
long long MAX_NUM = 1000000;
|
||||
|
||||
if (argc == 2) /* set commandline argumnet as the maximum iteration number */
|
||||
if (argc == 2) /* set commandline argumnet as the maximum iteration number */
|
||||
{
|
||||
MAX_NUM = atoll(argv[1]);
|
||||
printf("Maximum number: %lld\n", MAX_NUM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Since the computational values for each iteration step are independent,
|
||||
* we can compute them in parallel. However, the maximum values should be
|
||||
/**
|
||||
* Since the computational values for each iteration step are independent,
|
||||
* we can compute them in parallel. However, the maximum values should be
|
||||
* updated in synchrony so that we do not get into a "race condition".
|
||||
*
|
||||
*
|
||||
* To compile with supporintg gcc or clang, the flag "-fopenmp" should be passes
|
||||
* while with Microsoft C compiler, the flag "/fopenmp" should be used.
|
||||
*
|
||||
*
|
||||
* Automatically detects for OPENMP using the _OPENMP macro.
|
||||
**/
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
|
||||
#endif
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
|
||||
#endif
|
||||
for (long long i = 1; i < MAX_NUM; i++)
|
||||
{
|
||||
long long L = collatz(i);
|
||||
if (L > max_len)
|
||||
{
|
||||
max_len = L; /* length of sequence */
|
||||
max_len_num = i; /* starting number */
|
||||
max_len = L; /* length of sequence */
|
||||
max_len_num = i; /* starting number */
|
||||
}
|
||||
|
||||
#if defined(_OPENMP) && defined(DEBUG)
|
||||
#if defined(_OPENMP) && defined(DEBUG)
|
||||
printf("Thread: %2d\t %3lld: \t%5lld\n", omp_get_thread_num(), i, L);
|
||||
#elif defined(DEBUG)
|
||||
#elif defined(DEBUG)
|
||||
printf("%3lld: \t%5lld\n", i, L);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
printf("Start: %3lld: \tLength: %5lld\n", max_len_num, max_len);
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
|
74
project_euler/Problem 26/sol1.c
Normal file
74
project_euler/Problem 26/sol1.c
Normal 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
23
searching/CMakeLists.txt
Normal 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} )
|
@ -1,27 +1,32 @@
|
||||
#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 n,i,v;
|
||||
int main()
|
||||
{
|
||||
int n, i, v;
|
||||
printf("Enter the size of the array:\n");
|
||||
scanf("%d",&n); //Taking input for the size of Array
|
||||
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
|
||||
if (linearsearch(a,n,v))
|
||||
if (linearsearch(a, n, v))
|
||||
printf("Value %d is in the array.\n", v);
|
||||
else
|
||||
printf("Value %d is not in the array.\n", v);
|
||||
return 0;
|
||||
}
|
||||
|
@ -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;
|
||||
@ -10,14 +9,14 @@ int binarySearch(int array[], int leng, int searchX)
|
||||
left = 0;
|
||||
right = leng - 1;
|
||||
|
||||
while(left <= right)
|
||||
while (left <= right)
|
||||
{
|
||||
pos = left + (right - left) / 2;
|
||||
if(array[pos] == searchX)
|
||||
if (array[pos] == searchX)
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
else if(array[pos] > searchX)
|
||||
else if (array[pos] > searchX)
|
||||
{
|
||||
right = pos - 1;
|
||||
}
|
||||
@ -29,10 +28,9 @@ 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 };
|
||||
int array[len] = {5, 8, 10, 14, 16};
|
||||
|
||||
int position;
|
||||
position = binarySearch(array, len, 5);
|
||||
@ -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;
|
||||
}
|
||||
|
23
searching/pattern_search/CMakeLists.txt
Normal file
23
searching/pattern_search/CMakeLists.txt
Normal 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} )
|
@ -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
|
@ -1,56 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define MAX 20
|
||||
#define MAX 20
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int i , arraySort[MAX] ={0} , isSort = FALSE, changePlace;
|
||||
|
||||
|
||||
/* For example
|
||||
int i, arraySort[MAX] = {0}, isSort = FALSE, changePlace;
|
||||
|
||||
/* For example
|
||||
Insertion random values in array to test
|
||||
*/
|
||||
|
||||
|
||||
for(i = 0 ; i < MAX; i++)
|
||||
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
arraySort[i] = rand()%101 ;
|
||||
arraySort[i] = rand() % 101;
|
||||
}
|
||||
|
||||
|
||||
/* Algorithm of bubble methods */
|
||||
|
||||
while(isSort)
|
||||
/* Algorithm of bubble methods */
|
||||
|
||||
while (isSort)
|
||||
{
|
||||
isSort = FALSE;
|
||||
isSort = FALSE;
|
||||
|
||||
for( i = 0 ; i < MAX - 1 ; i++)
|
||||
{
|
||||
if(arraySort[i] > arraySort[i+1])
|
||||
{
|
||||
changePlace = arratSort[i];
|
||||
arraySort[i] = arraySort[i+1];
|
||||
arraySort[i+1] = changePlace ;
|
||||
isSort = TRUE;
|
||||
}
|
||||
|
||||
}
|
||||
for (i = 0; i < MAX - 1; i++)
|
||||
{
|
||||
if (arraySort[i] > arraySort[i + 1])
|
||||
{
|
||||
changePlace = arraySort[i];
|
||||
arraySort[i] = arraySort[i + 1];
|
||||
arraySort[i + 1] = changePlace;
|
||||
isSort = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* See if it works */
|
||||
|
||||
for(i = 0 ; i < MAX; i++)
|
||||
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
printf("%d\n", arraySort[i]);
|
||||
printf("%d\n", arraySort[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
||||
|
23
sorting/CMakeLists.txt
Normal file
23
sorting/CMakeLists.txt
Normal 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} )
|
@ -24,13 +24,13 @@ int findMax(int arr[], int n)
|
||||
|
||||
for (maxElementIdx = 0, i = 0; i < n; ++i)
|
||||
if (arr[i] > arr[maxElementIdx])
|
||||
maxElementIdx = i;
|
||||
maxElementIdx = i;
|
||||
|
||||
return maxElementIdx;
|
||||
}
|
||||
|
||||
// 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)
|
||||
@ -39,13 +39,13 @@ int pancakeSort(int *arr, int n)
|
||||
int maxElementIdx = findMax(arr, curr_size);
|
||||
|
||||
// Move the maximum element to end of current array if it's not already at the end
|
||||
if (maxElementIdx != curr_size-1)
|
||||
{
|
||||
if (maxElementIdx != curr_size - 1)
|
||||
{
|
||||
// To move at the end, first move maximum number to beginning
|
||||
flip(arr, maxElementIdx);
|
||||
flip(arr, maxElementIdx);
|
||||
|
||||
// Now move the maximum number to end by reversing current array
|
||||
flip(arr, curr_size-1);
|
||||
flip(arr, curr_size - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -53,26 +53,29 @@ int pancakeSort(int *arr, int n)
|
||||
// Displays the array, passed to this method
|
||||
void display(int arr[], int n)
|
||||
{
|
||||
for(int i = 0; i < n; i++)
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,24 +1,24 @@
|
||||
#include <stdio.h>
|
||||
void stoogesort(int [], int, int);
|
||||
|
||||
void main()
|
||||
void stoogesort(int[], int, int);
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[100], i, n;
|
||||
|
||||
|
||||
printf("How many elements do you want to sort: ");
|
||||
scanf("%d", &n);
|
||||
for (i = 0;i < n; i++)
|
||||
for (i = 0; i < n; i++)
|
||||
scanf(" %d", &arr[i]);
|
||||
stoogesort(arr, 0, n - 1);
|
||||
printf("Sorted array : \n");
|
||||
for (i = 0;i < n;i++)
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void stoogesort(int arr[], int i, int j)
|
||||
{
|
||||
int temp, k;
|
||||
|
@ -1,91 +1,92 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void swap (int *a,int *b)//To swap the variables//
|
||||
void swap(int *a, int *b) //To swap the variables//
|
||||
{
|
||||
int t;
|
||||
t= *a;
|
||||
*a=*b;
|
||||
*b=t;
|
||||
|
||||
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 c=l;
|
||||
int p1,p2;
|
||||
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];
|
||||
p1++;
|
||||
}
|
||||
else
|
||||
{b[c++] = a[p2];p2++;}
|
||||
|
||||
};
|
||||
|
||||
if (p2 == r+1)
|
||||
void merge(int a[], int l, int r, int n) //To merge //
|
||||
{
|
||||
int *b = (int *)malloc(n * sizeof(int));
|
||||
int c = l;
|
||||
int p1, p2;
|
||||
p1 = l;
|
||||
p2 = ((l + r) / 2) + 1;
|
||||
while ((p1 < ((l + r) / 2) + 1) && (p2 < r + 1))
|
||||
{
|
||||
while ((p1<((l+r)/2)+1))
|
||||
{
|
||||
b[c++] = a[p1];
|
||||
if (a[p1] <= a[p2])
|
||||
{
|
||||
b[c++] = a[p1];
|
||||
p1++;
|
||||
};
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
b[c++] = a[p2];
|
||||
p2++;
|
||||
}
|
||||
};
|
||||
|
||||
if (p2 == r + 1)
|
||||
{
|
||||
while ((p1 < ((l + r) / 2) + 1))
|
||||
{
|
||||
b[c++] = a[p1];
|
||||
p1++;
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((p2<r+1))
|
||||
{
|
||||
b[c++] = a[p2];
|
||||
p2++;
|
||||
};
|
||||
}
|
||||
|
||||
for (c=l;c<r-l+1;c++)
|
||||
while ((p2 < r + 1))
|
||||
{
|
||||
b[c++] = a[p2];
|
||||
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 (r - l == 1)
|
||||
{
|
||||
if (a[l] > a[r])
|
||||
swap(&a[l], &a[r]);
|
||||
}
|
||||
else if (l == r)
|
||||
{
|
||||
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(a,l,r,n);
|
||||
|
||||
{
|
||||
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 *a,n,i;
|
||||
scanf("%d",&n);
|
||||
a = (int*)malloc(n*sizeof(int));
|
||||
for (i=0;i<n;i++)
|
||||
{
|
||||
|
||||
scanf("%d",&a[i]);
|
||||
|
||||
int main(void)
|
||||
{ //main function//
|
||||
int *a, n, i;
|
||||
scanf("%d", &n);
|
||||
a = (int *)malloc(n * sizeof(int));
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
merge_sort(a, n, 0, n - 1);
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
|
||||
printf(" %d", a[i]);
|
||||
}
|
||||
|
||||
free(a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
mergesort(a,n,0,n-1);
|
||||
for (i=0;i<n;i++)
|
||||
{
|
||||
|
||||
printf(" %d",&a[i]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -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[])
|
||||
|
@ -1,42 +1,44 @@
|
||||
//sorting of array list using Radix sort
|
||||
#include <stdio.h>
|
||||
|
||||
#define range 10 // Range for integers is 10 as digits range from 0-9
|
||||
#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 i, max = ar[0];
|
||||
for(i = 0; i<size; i++){
|
||||
if(ar[i]>max)
|
||||
max = ar[i];
|
||||
}
|
||||
return max;
|
||||
int MAX(int ar[], int size)
|
||||
{
|
||||
int i, max = ar[0];
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (ar[i] > max)
|
||||
max = ar[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
// Counting sort according to the digit represented by place
|
||||
void countSort(int arr[],int n,int place)
|
||||
void countSort(int arr[], int n, int place)
|
||||
{
|
||||
int i,freq[range]={0};
|
||||
int i, freq[range] = {0};
|
||||
int output[n];
|
||||
|
||||
|
||||
// Store count of occurences in freq[]
|
||||
for(i=0;i<n;i++)
|
||||
freq[(arr[i]/place)%range]++;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
freq[(arr[i] / place) % range]++;
|
||||
|
||||
// Change freq[i] so that it contains the actual position of the digit in output[]
|
||||
for(i=1;i<range;i++)
|
||||
freq[i]+=freq[i-1];
|
||||
|
||||
for (i = 1; i < range; i++)
|
||||
freq[i] += freq[i - 1];
|
||||
|
||||
// Build the output array
|
||||
for(i=n-1;i>=0;i--)
|
||||
for (i = n - 1; i >= 0; i--)
|
||||
{
|
||||
output[freq[(arr[i]/place)%range]-1]=arr[i];
|
||||
freq[(arr[i]/place)%range]--;
|
||||
output[freq[(arr[i] / place) % range] - 1] = arr[i];
|
||||
freq[(arr[i] / place) % range]--;
|
||||
}
|
||||
|
||||
|
||||
// Copy the output array to arr[], so it contains numbers according to the current digit
|
||||
for(i=0;i<n;i++)
|
||||
arr[i]=output[i];
|
||||
for (i = 0; i < n; i++)
|
||||
arr[i] = output[i];
|
||||
}
|
||||
|
||||
/*This is where the sorting of the array takes place
|
||||
@ -44,41 +46,48 @@ void countSort(int arr[],int n,int place)
|
||||
n --- Array Size
|
||||
max --- Maximum element in Array
|
||||
*/
|
||||
void radixsort(int arr[],int n,int max) //max is the maximum element in the array
|
||||
void radixsort(int arr[], int n, int max) //max is the maximum element in the array
|
||||
{
|
||||
int mul=1;
|
||||
while(max)
|
||||
int mul = 1;
|
||||
while (max)
|
||||
{
|
||||
countsort(arr,n,mul);
|
||||
mul*=10;
|
||||
max/=10;
|
||||
countSort(arr, n, mul);
|
||||
mul *= 10;
|
||||
max /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]){
|
||||
int n;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for(i = 0; i < n; i++){
|
||||
scanf("%d", &arr[i] );
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||
|
||||
int max;
|
||||
max = MAX(arr,n);
|
||||
|
||||
radixsort(arr, n, max);
|
||||
|
||||
printf("Sorted array: ");
|
||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
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
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||
|
||||
int max;
|
||||
max = MAX(arr, n);
|
||||
|
||||
radixsort(arr, n, max);
|
||||
|
||||
printf("Sorted array: ");
|
||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,111 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define ELEMENT_NR 20000
|
||||
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
||||
const char *notation = "Shell Sort Big O Notation:\
|
||||
\n--> Best Case: O(n log(n)) \
|
||||
\n--> Average Case: depends on gap sequence \
|
||||
\n--> Worst Case: O(n)";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void shellSort(int array[], int len)
|
||||
{
|
||||
int i, j, gap;
|
||||
|
||||
for (gap = len / 2; gap > 0; gap = gap / 2)
|
||||
for (i = gap; i < len; i++)
|
||||
for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j = j - gap)
|
||||
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;
|
||||
double time_spent;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < ELEMENT_NR; i++)
|
||||
{
|
||||
array[i] = rand() % range + 1;
|
||||
array2[i] = array[i];
|
||||
}
|
||||
|
||||
size = ARRAY_LEN(array);
|
||||
|
||||
show_data(array, size);
|
||||
start = clock();
|
||||
shellSort(array, size);
|
||||
end = clock();
|
||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("Data Sorted\n");
|
||||
show_data(array, size);
|
||||
|
||||
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;
|
||||
}
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define ELEMENT_NR 20000
|
||||
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
||||
const char *notation = "Shell Sort Big O Notation:\
|
||||
\n--> Best Case: O(n log(n)) \
|
||||
\n--> Average Case: depends on gap sequence \
|
||||
\n--> Worst Case: O(n)";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void shellSort(int array[], int len)
|
||||
{
|
||||
int i, j, gap;
|
||||
|
||||
for (gap = len / 2; gap > 0; gap = gap / 2)
|
||||
for (i = gap; i < len; i++)
|
||||
for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j = j - gap)
|
||||
swap(&array[j], &array[j + gap]);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int array[ELEMENT_NR];
|
||||
int range = 500;
|
||||
int size;
|
||||
clock_t start, end;
|
||||
double time_spent;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < ELEMENT_NR; i++)
|
||||
array[i] = rand() % range + 1;
|
||||
|
||||
size = ARRAY_LEN(array);
|
||||
|
||||
show_data(array, size);
|
||||
start = clock();
|
||||
shellSort(array, size);
|
||||
end = clock();
|
||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("Data Sorted\n");
|
||||
show_data(array, size);
|
||||
|
||||
printf("%s\n", notation);
|
||||
printf("Time spent sorting: %.4g ms\n", time_spent * 1e3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
81
sorting/shell_Sort2.c
Normal file
81
sorting/shell_Sort2.c
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user