Category Archives: Code samples

Insertion sort php

<?php /** * An unsorted list of integers * Worst case O(n^2) * Best case O(n) * @param array $unsorted */ function insertionSort(array &$unsorted){ for($i = 1; $i < count($unsorted); $i++){ $temp = $unsorted[$i]; $j = $i – 1; while … Continue reading

Posted in algorithms, php | Leave a comment

Double Ended Queue implementation in Java, and Operations run in O(1) time

public class Main {     /**      * @param args      */     public static void main(String[] args) {     Deque<Integer> Q = new Deque<Integer>(6);     try {         Q.addFirst(3);         System.out.println(Q);         Q.addFirst(5);         System.out.println(Q); … Continue reading

Posted in Code samples | Tagged | Leave a comment

Resursion to add n by n elements in an array

Algorithm   Algorithm sum(A,n)     Input: A, an n by n array     Output: The sum of elements in A       if n=1 then return A[0][0]       s  ← 0     while i < n -1 do         s ← s + A[i][n-1] … Continue reading

Posted in Code samples | Leave a comment

power function in O(n) time, algorithm plus implementation in python

Algorithm number 1.   Algorithm powerOf(x,n)         Input: integer n, non negative integer x         Output : x to the power of n         if n = 0              return 1         m ← 0         while n > 0 … Continue reading

Posted in Code samples | Tagged | Leave a comment

Find the factorial : Python

def factorial(n): """ Returns the factorial of an integer Author: http://www.victorbyte.net """ if n == 0: return 1; elif n > 0: return n * factorial(n – 1);

Posted in Code samples | Tagged | Leave a comment

Check for duplicate characters : Python

def hasDuplicate(str_var): “”” Checks for repeated characters Author: http://www.victorbyte.net returns Boolean value “”” for i in range (0, len(str_var)): for j in range( i + 1, len(str_var)): if str_var[i].lower() == str_var[j].lower(): return True; return False;

Posted in algorithms, python | 3 Comments

Check for repeated characters: csharp

/// Checks for repeated characters /// Author: http://www.victorbyte.net /// returns Boolean value static bool hasDuplicate(string str) { for (int i = 0; i < str.Length; i++) { for (int j = i + 1; j < str.Length; j++) { if … Continue reading

Posted in Code samples | Tagged , | Leave a comment

Greatest Common Divisor : Java

/** * Gets GCD between two positive integers. * source: http://www.victorbyte.net * Returns int value */ public static int getGCD(int numA, int numB) { if(numA < numB) { int temp = numA; numA = numB; numB = temp; } if( … Continue reading

Posted in Code samples | Tagged | Leave a comment

Check for truth in a predicate : Java

/** * Checks for truth in a predicate. * http://www.victorbyte.net/ * Returns boolean value */ public static boolean forEachPredicate(double [] set_S) { for(double x:set_S) if(!(Math.pow(x,2.0) >= x)) //predicate return false; return true; } public static boolean negateForEachPredicate(double [] set_S) { … Continue reading

Posted in Code samples | Tagged | Leave a comment

Sort int array elements from High to Low : Java

/** * Sort int array elements from high to low. * http://www.victorbyte.net/ * void method */ public static void arraySortHighToLow(int[] array) { int temp = 0; for (int loop = array.length – 2, index = array.length – 1; loop >= … Continue reading

Posted in algorithms, java | Leave a comment