Skip to content

Instantly share code, notes, and snippets.

@metallurgix
metallurgix / gcd.c
Last active August 29, 2015 14:02
GCD
int gcd(int a, int b)
{
while(b) b ^= a ^= b ^= a %= b;
return a;
}
@metallurgix
metallurgix / linear_search.c
Created July 7, 2014 07:08
Linear Search (C)
int linear_search(int *list,int size, int key)
{
int i;
for(i=0;i<=size;++i)
{
if(key==list[i])
return i;
}
return 0;
}
@metallurgix
metallurgix / fibonacci.rb
Created July 7, 2014 07:20
Fibonacci Series (Ruby)
def fib_n n
i=0
j=1
(1..n).map{i=j+j=i}
i
end
@metallurgix
metallurgix / prime.rb
Created July 7, 2014 18:24
Checking for Prime Number
def prime? n
return true if n == 2
return false if n < 2 or n % 2 == 0
for d in (3..Math.sqrt(n).to_i).step(2) do
return false if n % d == 0
end
return true
end
@metallurgix
metallurgix / matrix_mul.c
Created July 7, 2014 18:37
Standard Matrix Multiplication
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
void Multiply(int n, double** a, double** b, double** c)
{
int i=0;
int j=0;
int k=0;
for(i = 0; i < n; i++)
@metallurgix
metallurgix / quicksort.java
Last active August 29, 2015 14:03
QuickSort
public class QuickSort
{
private int[] a;
private int n;
public void sort(int[] b)
{
this.a=b;
n=a.length;
qs(0, n-1);
public class InsertionSort
{
private static int[] a;
private static int n;
public static void sort(int[] b)
{
a=b;
n=a.length;
isort();
@metallurgix
metallurgix / insertionsort.c
Last active August 29, 2015 14:03
Insertion Sort
void isort (int array[], int len)
{
register int i, j, t;
for (i = 1; i < len; i++)
{
j = i;
t = array[j];
while (j > 0 && array[j-1] > temp)
{
array[j] = array[j-1];
@metallurgix
metallurgix / bubblesort.c
Last active August 29, 2015 14:03
Bubble Sort
void bubblesort (int *array, int len)
{
register int j, t=1;
while(len-- && swap)
{
for (j=swap=0; j<len-1; j++)
{
if (array[j]>aray[j+1])
{
swap=1;
public class BubbleSort
{
private static int[] a;
private static int n;
public static void sort(int[] b)
{
a=b;
n=a.length;
bubblesort();