gnome sort


See On Github

Data

Source Code

/*
 *  gnome sort
 *  ----------
 *  author: ghostsnstuff
 *  date: March 27, 2014
 *  src: http://en.wikipedia.org/wiki/Gnome_sort
 *  complexity:
 *   - worst case O(n^2)
 *   - best case O(n)
 *   - avg case O(n^2) 
 */
module.exports = function(list) {
  var i = 1
  while(i < list.length) {
    if(list[i] >= list[i-1]) {
      i++
    } else {
      var temp = list[i]
      list[i] = list[i-1]
      list[i-1] = temp
      if(i > 1) i--
    }
  }
  return list
}
var gnome = require('./gnome_sort')
  , assert = require('assert')
  , actual = [2,3,4,5]
  , expected = gnome([5,3,2,4])

assert.deepEqual(actual, expected, 'gnome([5,3,2,4]) failed')
process.stdout.write('gnome([5,3,2,4]) -> ['+expected+']\n')