Category Archives: algorithms

Posts that implement Computer Science algorithms.

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

Calculate the diagonal difference of an N x N matrix

<?php /** * Calculate the diagonal difference of an N x N matrix * * runs in O(n) * * @param int $n size of N x N array * @param array $a N x N array * * @return … Continue reading

Posted in algorithms | Tagged , , | Leave a comment

Generating n prime numbers in JavaScript

/** * Prime number * parameter n is an integer * Generate n prime numbers */ var primeList=[2]; //seed function prime(n) { n += 1; for (var k = 0; k < primeList.length; k++) { if((n%primeList[k])==0) return prime(n); } primeList.push(n); … Continue reading

Posted in algorithms, javascript prime number function, prime number, prime number function | 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

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