Skip to content

Instantly share code, notes, and snippets.

@Rivares
Last active August 12, 2018 10:10
Show Gist options
  • Save Rivares/63562b5668b703a59a6564063fe511f9 to your computer and use it in GitHub Desktop.
Save Rivares/63562b5668b703a59a6564063fe511f9 to your computer and use it in GitHub Desktop.
Fundomental algorithms
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include "MyFundMethods.h"
int main()
{
srand(time(NULL));
// Multiply matrix, Sum diagonal elements
// Matrix A(3,3), Matrix B(3,3), Matrix C(N,M) - realloc, calloc, memset, memcpy
int *arr = (int *) malloc(sizeof(int)*SIZE_ARR);
mixer(arr);
//===============================================================================
bubble_Sort(arr);
printf("Bubble sort:\n");
for(size_t i = 0; i < SIZE_ARR; ++i)
{
printf("%d\n", arr[i]);
}
mixer(arr);
insertion_Sort(arr);
printf("\nInsert sort:\n");
for(size_t i = 0; i < SIZE_ARR; ++i)
{
printf("%d\n", arr[i]);
}
mixer(arr);
selection_Sort(arr);
printf("\nSelect sort:\n");
for(size_t i = 0; i < SIZE_ARR; ++i)
{
printf("%d\n", arr[i]);
}
free(arr);
char buf[500];
const char * file_name = "swsignals.lua";
file_Input(file_name, buf, 500);
file_Output("cioy_swsignals.lua", buf, 500);
// Parallel sort's methods
pthread_t *threads[NUM_PROCESSES];
pthread_create(&threads[0], 0, bubble_Sort, (void *)arr);
pthread_create(&threads[1], 0, selection_Sort, (void *)arr);
pthread_create(&threads[2], 0, insertion_Sort, (void *)arr);
getchar();
return 0;
}
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#define SIZE_ARR 15
#define NUM_THREADS 4
// Function with arguments by copy value, by reference(addresses), by point
/* Mixer */
void mixer(int arr[]);
/* Bubble sort */
void bubble_Sort(int arr[], int length);
/* Insertion sort */
void insertion_Sort(int arr[], int length);
/* Select sort */
void selection_Sort(int arr[], int length);
/* Quick sort */
void quick_Sort();
/* Fibonachi */
int numbers_Fibonachi(int num);
/* Factorial */
int factorial(int num);
/* Greatest common divisor */
int greatest_Common_divisor(int a, int b);
/* Converting int <--> char */
void converting_int_char();
/* File input */
void file_Input(const char * file_name, char buf[], int size_buf);
/* File output */
void file_Output(const char * file_name, char buf[], int size_buf);
int main()
{
srand(time(NULL));
// Multiply matrix, Sum diagonal elements
// Matrix A(3,3), Matrix B(3,3), Matrix C(N,M) - malloc, realloc, calloc, memset, memcpy
int *arr = (int *) malloc(sizeof(int)*SIZE_ARR);
mixer(arr);
//===============================================================================
bubble_Sort(arr, SIZE_ARR);
printf("Bubble sort:\n");
for(size_t i = 0; i < SIZE_ARR; ++i)
{
printf("%d\n", arr[i]);
}
mixer(arr);
insertion_Sort(arr, SIZE_ARR);
printf("\nInsert sort:\n");
for(size_t i = 0; i < SIZE_ARR; ++i)
{
printf("%d\n", arr[i]);
}
mixer(arr);
selection_Sort(arr, SIZE_ARR);
printf("\nSelect sort:\n");
for(size_t i = 0; i < SIZE_ARR; ++i)
{
printf("%d\n", arr[i]);
}
pthread_t threads[NUM_THREADS];
pthread_create(&threads[0], NULL, bubble_Sort, arr, SIZE_ARR);
pthread_create(&threads[1], NULL, selection_Sort, arr, SIZE_ARR);
pthread_create(&threads[2], NULL, insertion_Sort, arr, SIZE_ARR);
pthread_exit(NULL);
free(arr);
char buf[500];
const char * file_name = "swsignals.lua";
file_Input(file_name, buf, 500);
file_Output("cioy_swsignals.lua", buf, 500);
// Parallel sort's methods
return 0;
}
void mixer(int arr[])
{
for(size_t i = 0; i < SIZE_ARR; ++i)
{
arr[i] = rand() % 3566;
}
}
/* Bubble sort */
void bubble_Sort(int arr[], int length)
{
int buf = 0;
for(size_t i = 0; i < length; ++i)
{
for(size_t j = (length-1); j >= (i+1); --j)
{
if(arr[j] < arr[j-1])
{
buf = arr[j];
arr[j] = arr[j-1];
arr[j-1] = buf;
}
}
}
}
/* Insertion sort */
void insertion_Sort(int arr[], int length)
{
int key = 0, j = 0;
for(size_t i = 1; i < length; ++i)
{
key = arr[i];
j = i - 1;
while( (j >= 0) && (arr[j] > key) ) // insertion in sub array
{
arr[j+1] = arr[j];
--j;
arr[j+1] = key; // to next in sorted sub array
}
}
}
/* Select sort */
void selection_Sort(int arr[], int length)
{
int buf = 0, j = 0;
for(size_t i = 0; i < length; ++i)
{
j = i;
for(size_t k = i + 1; k < length; ++k)
{
if(arr[j] > arr[k])
{
j = k;
}
}
buf = arr[i];
arr[i] = arr[j];
arr[j] = buf;
}
}
/* Quick sort */
void quick_Sort(int* data, const int len)
{
const int lenD = len;
int pivot = 0;
int ind = lenD/2;
int i,j = 0,k = 0;
if(lenD>1)
{
int* L = (int*)malloc(sizeof(int)*lenD);
int* R = (int*)malloc(sizeof(int)*lenD);
pivot = data[ind];
for(i=0;i<lenD;i++)
{
if(i!=ind)
{
if(data[i]<pivot)
{
L[j] = data[i];
j++;
}
else
{
R[k] = data[i];
k++;
}
}
}
quick_Sort(L, j);
quick_Sort(R, k);
for(int cnt=0;cnt<lenD;cnt++)
{
if(cnt<j)
{
data[cnt] = L[cnt];
}
else
if(cnt==j)
{
data[cnt] = pivot;
}
else
{
data[cnt] = R[cnt-(j+1)];
}
}
}
}
/* Fibonachi */
int numbers_Fibonachi(int num)
{
if(num < 1) return 0;
if(num == 1)return 1;
return (numbers_Fibonachi(num - 1) + numbers_Fibonachi(num - 2));
}
/* Factorial */
int factorial(int num)
{
int result = 1;
for(size_t i = 1; i < num; ++i)
result *= i;
return result;
}
/* Greatest common divisor */
int greatest_Common_divisor(int a, int b)
{
int c = 0;
while(b)
{
c = a % b;
a = b;
b = c;
}
return abs(a);
}
/* Converting int <--> char */
void converting_int_char()
{
char ch = '4';
printf("%d\n", ch - '0');
int num = 5;
printf("%c\n", num + '0');
}
/* File input */
void file_Input(const char * file_name, char buf[], int size_buf)
{
FILE *fd = fopen(file_name, "r");
memset(buf, 0, sizeof(buf));
while(fgets(buf, 500, fd) != 0)
printf("%s", buf);
fclose(fd);
}
/* File output */
void file_Output(const char * file_name, char buf[], int size_buf)
{
FILE *fd = fopen(file_name, "w+");
while(fputs(buf, fd) != 0);
fclose(fd);
}
#ifndef MYFUNDMETHODS_H
#define MYFUNDMETHODS_H
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#define SIZE_ARR 15
#define NUM_PROCESSES 3
// Function with arguments by copy value, by reference(addresses), by point
/* Mixer */
void mixer(int arr[]);
/* Bubble sort */
void *bubble_Sort(int arr[]);
/* Insertion sort */
void insertion_Sort(int arr[]);
/* Select sort */
void selection_Sort(int arr[]);
/* Quick sort */
void quick_Sort();
/* Fibonachi */
int numbers_Fibonachi(int num);
/* Factorial */
int factorial(int num);
/* Greatest common divisor */
int greatest_Common_divisor(int a, int b);
/* Converting int <--> char */
void converting_int_char();
/* File input */
void file_Input(const char * file_name, char buf[], int size_buf);
/* File output */
void file_Output(const char * file_name, char buf[], int size_buf);
#endif
#include "MyStructs.h"
/* A Singly-Linked List */
void insertNodeInSinglyLinkedList()
{
}
void deleteNodeInSinglyLinkedList()
{
}
void displayNodeInSinglyLinkedList()
{
}
/* Stack */
void pushNode()
{
}
void popNode()
{
}
/* Queue */
void insertNodeInQueue()
{
}
void deleteNodeInQueue()
{
}
void displayNodeInQueue()
{
}
#ifndef MYSTRUCTS_H
#define MYSTRUCTS_H
/* A Singly-Linked List */
void InSinglyLinkedList();
void deleteNodeInSinglyLinkedList();
void displayNodeInSinglyLinkedList();
/* Stack */
void pushNode();
void popNode();
/* Queue */
void insertNodeInQueue();
void deleteNodeInQueue();
void displayNodeInQueue();
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment