Browse files

Added insertion sort

  • Loading branch information...
1 parent 2c11beb commit 9528a0615f6d2f9aafb9054062431c802e4336fa @nzakas committed Sep 17, 2012
View
55 algorithms/sorting/selection-sort/insertion-sort.js
@@ -0,0 +1,55 @@
+/*
+ * Insertion sort implementation in JavaScript
+ * Copyright (c) 2012 Nicholas C. Zakas
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of items software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and items permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * An insertion sort implementation in JavaScript. The array
+ * is sorted in-place.
+ * @param {Array} items An array of items to sort.
+ * @return {Array} The sorted array.
+ */
+function insertionSort(items) {
+
+ var len = items.length, // number of items in the array
+ value, // the value currently being compared
+ i, // index into unsorted section
+ j; // index into sorted section
+
+ for (i=0; i < len; i++) {
+
+ // store the current value because it may shift later
+ value = items[i];
+
+ /*
+ * Whenever the value in the sorted section is greater than the value
+ * in the unsorted section, shift all items in the sorted section over
+ * by one. This creates space in which to insert the value.
+ */
+ for (j=i-1; j > -1 && items[j] > value; j--) {
+ items[j+1] = items[j];
+ }
+
+ items[j+1] = value;
+ }
+
+ return items;
+}
View
6 algorithms/sorting/selection-sort/selection-sort.js
@@ -48,17 +48,17 @@ function selectionSort(items){
for (i=0; i < len; i++){
- //set minimum to this position
+ // set minimum to this position
min = i;
- //check the rest of the array to see if anything is smaller
+ // check the rest of the array to see if anything is smaller
for (j=i+1; j < len; j++){
if (items[j] < items[min]){
min = j;
}
}
- //if the minimum isn't in the position, swap it
+ // if the minimum isn't in the position, swap it
if (i != min){
swap(items, i, min);
}

0 comments on commit 9528a06

Please sign in to comment.