├── README.md ├── InvSqrt.c └── quick-sort.js /README.md: -------------------------------------------------------------------------------- 1 | # beacode 2 | Beautiful Code 3 | -------------------------------------------------------------------------------- /InvSqrt.c: -------------------------------------------------------------------------------- 1 | float InvSqrt (float x) 2 | { 3 | float xhalf = 0.5f*x; 4 | int i = *(int*)&x; 5 | i = 0x5f3759df - (i>>1); 6 | x = *(float*)&i; 7 | x = x*(1.5f - xhalf*x*x); 8 | return x; 9 | } 10 | -------------------------------------------------------------------------------- /quick-sort.js: -------------------------------------------------------------------------------- 1 | /** 2 | * An implementation for Quicksort. Doesn't 3 | * perform as well as the native Array.sort 4 | * and also runs the risk of a stack overflow 5 | * 6 | * Tests with: 7 | * 8 | * var array = []; 9 | * for(var i = 0; i < 20; i++) { 10 | * array.push(Math.round(Math.random() * 100)); 11 | * } 12 | * 13 | * Quicksort.sort(array); 14 | * 15 | * @author Paul Lewis 16 | */ 17 | var Quicksort = (function() { 18 | 19 | /** 20 | * Swaps two values in the heap 21 | * 22 | * @param {int} indexA Index of the first item to be swapped 23 | * @param {int} indexB Index of the second item to be swapped 24 | */ 25 | function swap(array, indexA, indexB) { 26 | var temp = array[indexA]; 27 | array[indexA] = array[indexB]; 28 | array[indexB] = temp; 29 | } 30 | 31 | /** 32 | * Partitions the (sub)array into values less than and greater 33 | * than the pivot value 34 | * 35 | * @param {Array} array The target array 36 | * @param {int} pivot The index of the pivot 37 | * @param {int} left The index of the leftmost element 38 | * @param {int} left The index of the rightmost element 39 | */ 40 | function partition(array, pivot, left, right) { 41 | 42 | var storeIndex = left, 43 | pivotValue = array[pivot]; 44 | 45 | // put the pivot on the right 46 | swap(array, pivot, right); 47 | 48 | // go through the rest 49 | for(var v = left; v < right; v++) { 50 | 51 | // if the value is less than the pivot's 52 | // value put it to the left of the pivot 53 | // point and move the pivot point along one 54 | if(array[v] < pivotValue) { 55 | swap(array, v, storeIndex); 56 | storeIndex++; 57 | } 58 | } 59 | 60 | // finally put the pivot in the correct place 61 | swap(array, right, storeIndex); 62 | 63 | return storeIndex; 64 | } 65 | 66 | /** 67 | * Sorts the (sub-)array 68 | * 69 | * @param {Array} array The target array 70 | * @param {int} left The index of the leftmost element, defaults 0 71 | * @param {int} left The index of the rightmost element, 72 | defaults array.length-1 73 | */ 74 | function sort(array, left, right) { 75 | 76 | var pivot = null; 77 | 78 | if(typeof left !== 'number') { 79 | left = 0; 80 | } 81 | 82 | if(typeof right !== 'number') { 83 | right = array.length - 1; 84 | } 85 | 86 | // effectively set our base 87 | // case here. When left == right 88 | // we'll stop 89 | if(left < right) { 90 | 91 | // pick a pivot between left and right 92 | // and update it once we've partitioned 93 | // the array to values < than or > than 94 | // the pivot value 95 | pivot = left + Math.ceil((right - left) * 0.5); 96 | newPivot = partition(array, pivot, left, right); 97 | 98 | // recursively sort to the left and right 99 | sort(array, left, newPivot - 1); 100 | sort(array, newPivot + 1, right); 101 | } 102 | 103 | } 104 | 105 | return { 106 | sort: sort 107 | }; 108 | 109 | })(); 110 | --------------------------------------------------------------------------------