From 7a3b54c916fc45e1b56f1c0449d19f4a02925575 Mon Sep 17 00:00:00 2001 From: Panzki Date: Fri, 29 Sep 2017 21:56:14 +0200 Subject: [PATCH 1/2] Added the bogosort algorithm. --- Sorts/bogoSort.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Sorts/bogoSort.js diff --git a/Sorts/bogoSort.js b/Sorts/bogoSort.js new file mode 100644 index 000000000..a7eca029e --- /dev/null +++ b/Sorts/bogoSort.js @@ -0,0 +1,60 @@ +/* +* A simple helper function that checks, if the array is +* sorted in ascending order. +*/ +Array.prototype.isSorted = function() { + + var length = this.length; + + if (length < 2) { + + return true; + } + + for (var i = 0; i < length - 1; i++) { + + if (this[i] > this[i + 1]) { + + return false; + } + } + return true; +}; + +/* +* A simple helper function to shuffle the array randomly in place. +*/ +Array.prototype.shuffle = function() { + + for (var i = this.length; i; i--) { + + var m = Math.floor(Math.random() * i); + var n = this[i - 1]; + this[i - 1] = this[m]; + this[m] = n; + } + +}; + +/* +* Implementation of the bogosort algorithm. This sorting algorithm randomly +* rearranges the array until it is sorted. +* For more information see: https://en.wikipedia.org/wiki/Bogosort +*/ +function bogoSort(items) { + + while(!items.isSorted()){ + + items.shuffle() + } + return items; +} + +//Implementation of bogoSort + +var ar = [5, 6, 7, 8, 1, 2, 12, 14]; +//Array before Sort +console.log(ar); +bogoSort(ar); +//Array after sort +console.log(ar); \ No newline at end of file From 94602f59af351863f7852504c11742e96fb12c16 Mon Sep 17 00:00:00 2001 From: Panzki Date: Fri, 29 Sep 2017 21:56:14 +0200 Subject: [PATCH 2/2] Added the bogosort algorithm. --- Sorts/bogoSort.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Sorts/bogoSort.js diff --git a/Sorts/bogoSort.js b/Sorts/bogoSort.js new file mode 100644 index 000000000..c9fe08f77 --- /dev/null +++ b/Sorts/bogoSort.js @@ -0,0 +1,55 @@ +/* +* A simple helper function that checks, if the array is +* sorted in ascending order. +*/ +Array.prototype.isSorted = function() { + + var length = this.length; + + if (length < 2) { + return true; + } + + for (var i = 0; i < length - 1; i++) { + if (this[i] > this[i + 1]) { + return false; + } + } + return true; +}; + +/* +* A simple helper function to shuffle the array randomly in place. +*/ +Array.prototype.shuffle = function() { + + for (var i = this.length; i; i--) { + var m = Math.floor(Math.random() * i); + var n = this[i - 1]; + this[i - 1] = this[m]; + this[m] = n; + } + +}; + +/* +* Implementation of the bogosort algorithm. This sorting algorithm randomly +* rearranges the array until it is sorted. +* For more information see: https://en.wikipedia.org/wiki/Bogosort +*/ +function bogoSort(items) { + + while(!items.isSorted()){ + items.shuffle() + } + return items; +} + +//Implementation of bogoSort + +var ar = [5, 6, 7, 8, 1, 2, 12, 14]; +//Array before Sort +console.log(ar); +bogoSort(ar); +//Array after sort +console.log(ar); \ No newline at end of file