Skip to content

Instantly share code, notes, and snippets.

@metallurgix
metallurgix / mergesort.java
Last active August 29, 2015 14:03
Mege Sort
public class MergeSorter
{
private static int[] a, b;
public static void sort(int[] c)
{
a=c;
int n=a.length;
b=new int[(n+1)/2];
mergesort(0, n-1);
@metallurgix
metallurgix / shellsort.c
Last active August 29, 2015 14:03
Shell Sort
void shellsort(int *a, int n)
{
int h, i, j, k;
for (h = n; h /= 2;)
{
for (i = h; i < n; i++)
{
k = a[i];
for (j = i; j >= h && k < a[j - h]; j -= h) {
a[j] = a[j - h];
@metallurgix
metallurgix / block_matrix_mul.c
Last active May 29, 2022 02:31
Blocked Matrix Multiplication
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
void Multiply(int n, double** a, double** b, double** c)
{
int bi=0;
int bj=0;
int bk=0;
int i=0;
@metallurgix
metallurgix / pthread_dot.c
Last active August 29, 2015 14:04
Dot-Product using pThread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAXTHRDS 124
typedef struct
{
double *my_x;
double *my_y;
@metallurgix
metallurgix / omp_mul.c
Last active August 4, 2024 12:23
Matrix Multiplication using OpenMP
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <sys/time.h>
#define N 1000
int A[N][N];
@metallurgix
metallurgix / mpi_mul.c
Created July 15, 2014 20:03
Matrix Multiplication using MPI
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <mpi.h>
int n, p;
int main(int argc, char **argv)
{
int myn, myrank;
double *a, *b, *c, *allB, start, sum, *allC, sumdiag;
@metallurgix
metallurgix / bin_search.c
Last active August 29, 2015 14:04
Binary Search
int BinarySearch(int *array, int n, int key)
{
register int low = 0, high = n-1, mid;
while(low <= high)
{
mid = low + ((high-low)/2);
if(array[mid] < key)
low = mid + 1;
else if(array[mid] > key)
high = mid-1;
@metallurgix
metallurgix / bin_search.java
Created July 19, 2014 19:13
Binary Search
public static int binarySearch(int[] nums, int key)
{
int hi = nums.length - 1;
int lo = 0;
int mid
while(hi >= lo)
{
mid = lo + ((hi - lo) / 2);
if(nums[mid] > key)
hi = mid - 1;
@metallurgix
metallurgix / fib_search.c
Created July 19, 2014 19:33
Fibonacci Search
int fibSearch(int a[], int n, int x)
{
int inf=0, pos, k;
static int kk= -1, nn=-1, fib[]={0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141};
if(nn!=n)
{
k=0;
while(fib[k]<n)
k++;
@metallurgix
metallurgix / strings.rb
Last active August 29, 2015 14:04
Useful String Methods
class String
#Capitalize the beginning of ever word in a string.
def word_capitalize
self.gsub(/\b\w/){$&.upcase}
end
#Check for balanced parantheses
def para_check?
para=Array.new