Skip to content

Instantly share code, notes, and snippets.

@sanemat
Created December 4, 2011 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanemat/1430305 to your computer and use it in GitHub Desktop.
Save sanemat/1430305 to your computer and use it in GitHub Desktop.
var selectionSort = {}
selectionSort.flag = false;
selectionSort.values = [1, 3, 2, 5, 4];
selectionSort.selectionSort = function(){
var count = this.values.length;
for (var i = 0; i < count - 1; i++){
var minpos = i;
for ( var j = i + 1; j < count; j++) {
if(this.values[j] < this.values[minpos]){
minpos = j;
}
}
this.swap(i, minpos);
}
};
selectionSort.swap = function(swapper, swappee){
var tmp = this.values[swapper];
this.values[swapper] = this.values[swappee];
this.values[swappee] = tmp;
};
describe('selectionSort', function(){
it('should return sorted value', function(){
expect(selectionSort.values).toEqual([1, 3, 2, 5, 4]);
selectionSort.selectionSort();
expect(selectionSort.values).toEqual([1, 2, 3, 4, 5]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment