Skip to content

Instantly share code, notes, and snippets.

function encrypt(text){
var cipher = crypto.createCipher('aes-256-cbc','d6F3Efeq')
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher('aes-256-cbc','d6F3Efeq')
var dec = decipher.update(text,'hex','utf8')
@rteja91
rteja91 / Merge Sort
Created August 3, 2014 15:48
Merge Sort
function mergesort($arr){
if(count($arr) == 1 ) return $arr;
$mid = count($arr) / 2;
$left = array_slice($arr, 0, $mid);
$right = array_slice($arr, $mid);
$left = mergesort($left);
$right = mergesort($right);
return merge($left, $right);
}
@rteja91
rteja91 / Shell Sort
Created August 3, 2014 14:09
Shell Sort
function shellsort(array $arr)
{
$n = sizeof($arr);
$t = ceil(log($n, 2));
$d[1] = 1;
for ($i = 2; $i <= $t; $i++) {
$d[$i] = 2 * $d[$i - 1] + 1;
}
$d = array_reverse($d);
@rteja91
rteja91 / quick sort
Created August 3, 2014 14:09
quick sort
function quicksort(array $arr, $left, $right)
{
$i = $left;
$j = $right;
$separator = $arr[floor(($left + $right) / 2)];
while ($i <= $j) {
while ($arr[$i] < $separator) {
$i++;
}
@rteja91
rteja91 / Counting Sort
Created August 3, 2014 14:08
Counting Sort
function countingSort(array $arr)
{
$n = sizeof($arr);
$p = array();
$sorted = array();
for ($i = 0; $i < $n; $i++) {
$p[$i] = 0;
}
@rteja91
rteja91 / Selection sort
Created August 3, 2014 14:07
Selection sort
function selectionSort(array $arr)
{
$n = sizeof($arr);
for ($i = 0; $i < $n; $i++) {
$lowestValueIndex = $i;
$lowestValue = $arr[$i];
for ($j = $i + 1; $j < $n; $j++) {
if ($arr[$j] < $lowestValue) {
$lowestValueIndex = $j;
$lowestValue = $arr[$j];
@rteja91
rteja91 / Bubble Sort
Created August 3, 2014 14:05
Bubble Sort in Various languages
#PHP
function bubbleSort(array $arr)
{
$n = sizeof($arr);
for ($i = 1; $i < $n; $i++) {
for ($j = $n - 1; $j >= $i; $j--) {
if($arr[$j-1] > $arr[$j]) {
$tmp = $arr[$j - 1];
$arr[$j - 1] = $arr[$j];
$arr[$j] = $tmp;
@rteja91
rteja91 / Sorting algorithms in PHP
Last active November 27, 2022 20:08
Sorting algorithms in php
// Bubble sort
function bubbleSort(array $arr)
{
$n = sizeof($arr);
for ($i = 1; $i < $n; $i++) {
for ($j = $n - 1; $j >= $i; $j--) {
if($arr[$j-1] > $arr[$j]) {
$tmp = $arr[$j - 1];
$arr[$j - 1] = $arr[$j];