Permalink
Browse files

Use the module pattern for the sort algorithms

  • Loading branch information...
1 parent 0abb7f0 commit 8739ed73c86a88b45c3bf7431420e4ae5c1ecb6f @fhd committed Apr 17, 2012
Showing with 129 additions and 120 deletions.
  1. +35 −33 public/js/sorting/heapsort.js
  2. +27 −24 public/js/sorting/mergeSort.js
  3. +31 −29 public/js/sorting/quicksort.js
  4. +36 −34 public/js/sorting/randomizedQuicksort.js
@@ -1,40 +1,42 @@
-function exchange(array, i, j) {
- var temp = array[i];
- array[i] = array[j];
- array[j] = temp;
-}
+var heapsort = (function() {
+ function exchange(array, i, j) {
+ var temp = array[i];
+ array[i] = array[j];
+ array[j] = temp;
+ }
-function maxHeapify(array, root) {
- var left = (root == 0) ? 1 : 2 * root,
- right = left + 1,
- largest = (left < array.heapSize && array[left] > array[root])
- ? left : root;
+ function maxHeapify(array, root) {
+ var left = (root == 0) ? 1 : 2 * root,
+ right = left + 1,
+ largest = (left < array.heapSize && array[left] > array[root])
+ ? left : root;
- if (right < array.heapSize && array[right] > array[largest])
- largest = right;
+ if (right < array.heapSize && array[right] > array[largest])
+ largest = right;
- if (largest != root) {
- exchange(array, root, largest);
- maxHeapify(array, largest);
+ if (largest != root) {
+ exchange(array, root, largest);
+ maxHeapify(array, largest);
+ }
}
-}
-function buildMaxHeap(array) {
- var i;
- array.heapSize = array.length;
- for (i = Math.round(array.length / 2); i >= 0; i--) {
- maxHeapify(array, i);
- update(array);
+ function buildMaxHeap(array) {
+ var i;
+ array.heapSize = array.length;
+ for (i = Math.round(array.length / 2); i >= 0; i--) {
+ maxHeapify(array, i);
+ update(array);
+ }
}
-}
-function heapsort(array) {
- var i;
- buildMaxHeap(array);
- for (i = array.length - 1; i >= 1; i--) {
- exchange(array, 0, i);
- array.heapSize--;
- maxHeapify(array, 0);
- update(array);
- }
-}
+ return function(array) {
+ var i;
+ buildMaxHeap(array);
+ for (i = array.length - 1; i >= 1; i--) {
+ exchange(array, 0, i);
+ array.heapSize--;
+ maxHeapify(array, 0);
+ update(array);
+ }
+ };
+})();
@@ -1,30 +1,33 @@
-function merge(array, start, middle, end) {
- var array1 = array.slice(start, middle),
- array2 = array.slice(middle, end),
- j = 0,
- k = 0,
- i;
+var mergeSort = (function() {
+ function merge(array, start, middle, end) {
+ var array1 = array.slice(start, middle),
+ array2 = array.slice(middle, end),
+ j = 0,
+ k = 0,
+ i;
- for (i = start; i < end; i++)
- if (j < array1.length && (k == array2.length || array1[j] <= array2[k]))
- array[i] = array1[j++];
+ for (i = start; i < end; i++)
+ if (j < array1.length
+ && (k == array2.length || array1[j] <= array2[k]))
+ array[i] = array1[j++];
else
array[i] = array2[k++];
-}
+ }
-function mergeSort(array, start, end) {
- var middle;
+ return function(array, start, end) {
+ var middle;
- if (start == undefined)
- start = 0;
- if (end == undefined)
- end = array.length;
+ if (start == undefined)
+ start = 0;
+ if (end == undefined)
+ end = array.length;
- if (start < end - 1) {
- middle = Math.floor((start + end + 1) / 2);
- mergeSort(array, start, middle);
- mergeSort(array, middle, end);
- merge(array, start, middle, end);
- update(array);
- }
-}
+ if (start < end - 1) {
+ middle = Math.floor((start + end + 1) / 2);
+ mergeSort(array, start, middle);
+ mergeSort(array, middle, end);
+ merge(array, start, middle, end);
+ update(array);
+ }
+ };
+})();
@@ -1,35 +1,37 @@
-function exchange(array, i, j) {
- var temp = array[i];
- array[i] = array[j];
- array[j] = temp;
-}
+var quicksort = (function() {
+ function exchange(array, i, j) {
+ var temp = array[i];
+ array[i] = array[j];
+ array[j] = temp;
+ }
-function partition(array, start, end) {
- var last = end - 1,
- pivot = array[last],
- i = start - 1,
- j;
+ function partition(array, start, end) {
+ var last = end - 1,
+ pivot = array[last],
+ i = start - 1,
+ j;
- for (j = start; j < last; j++)
- if (array[j] <= pivot)
- exchange(array, ++i, j);
+ for (j = start; j < last; j++)
+ if (array[j] <= pivot)
+ exchange(array, ++i, j);
- exchange(array, ++i, last);
- return i;
-}
+ exchange(array, ++i, last);
+ return i;
+ }
-function quicksort(array, start, end) {
- var split;
+ return function(array, start, end) {
+ var split;
- if (start == undefined)
- start = 0;
- if (end == undefined)
- end = array.length;
+ if (start == undefined)
+ start = 0;
+ if (end == undefined)
+ end = array.length;
- if (start < end - 1) {
- split = partition(array, start, end);
- update(array);
- quicksort(array, start, split);
- quicksort(array, split + 1, end);
- }
-}
+ if (start < end - 1) {
+ split = partition(array, start, end);
+ update(array);
+ quicksort(array, start, split);
+ quicksort(array, split + 1, end);
+ }
+ };
+})();
@@ -1,41 +1,43 @@
-function exchange(array, i, j) {
- var temp = array[i];
- array[i] = array[j];
- array[j] = temp;
-}
+var randomizedQuicksort = (function() {
+ function exchange(array, i, j) {
+ var temp = array[i];
+ array[i] = array[j];
+ array[j] = temp;
+ }
-function partition(array, start, end) {
- var last = end - 1,
- pivot = array[last],
- i = start - 1,
- j;
+ function partition(array, start, end) {
+ var last = end - 1,
+ pivot = array[last],
+ i = start - 1,
+ j;
- for (j = start; j < last; j++)
- if (array[j] <= pivot)
- exchange(array, ++i, j);
+ for (j = start; j < last; j++)
+ if (array[j] <= pivot)
+ exchange(array, ++i, j);
- exchange(array, ++i, last);
- return i;
-}
+ exchange(array, ++i, last);
+ return i;
+ }
-function randomizedPartition(array, start, end) {
- var randomPivot = Math.floor(Math.random() * (end - start) + start);
- exchange(array, end - 1, randomPivot);
- return partition(array, start, end);
-}
+ function randomizedPartition(array, start, end) {
+ var randomPivot = Math.floor(Math.random() * (end - start) + start);
+ exchange(array, end - 1, randomPivot);
+ return partition(array, start, end);
+ }
-function randomizedQuicksort(array, start, end) {
- var split;
+ return function(array, start, end) {
+ var split;
- if (start == undefined)
- start = 0;
- if (end == undefined)
- end = array.length;
+ if (start == undefined)
+ start = 0;
+ if (end == undefined)
+ end = array.length;
- if (start < end - 1) {
- split = randomizedPartition(array, start, end);
- update(array);
- randomizedQuicksort(array, start, split);
- randomizedQuicksort(array, split + 1, end);
- }
-}
+ if (start < end - 1) {
+ split = randomizedPartition(array, start, end);
+ update(array);
+ randomizedQuicksort(array, start, split);
+ randomizedQuicksort(array, split + 1, end);
+ }
+ };
+})();

0 comments on commit 8739ed7

Please sign in to comment.