From 3d5caa342301dcbfc6278638797e02ea809c4899 Mon Sep 17 00:00:00 2001 From: plane000 Date: Sun, 11 Nov 2018 20:03:57 +0000 Subject: [PATCH] bubble sort is so broken --- JavaScript/HitlerSort/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/JavaScript/HitlerSort/index.js b/JavaScript/HitlerSort/index.js index fbc5fde..02f5cf4 100644 --- a/JavaScript/HitlerSort/index.js +++ b/JavaScript/HitlerSort/index.js @@ -26,8 +26,16 @@ module.exports.HitlerSort = (arr) => { * @param {number[]|object[]} arr - Array of elements to sort */ module.exports.BubbleSort = (arr) => { - let len = arr.length; - + for (let i = arr.length - 1; i >= 0; i--) { + for (let j = i; j <= i; j++) { + if (arr[j - 1] > arr[j]) { + let temp = arr[j - 1]; + arr[j - 1] = arr[j]; + arr[j] = temp; + } + } + } + return arr; } // QuickSort for comparison @@ -42,16 +50,19 @@ module.exports.QuickSort = (arr) => { // Test const Sort = require('./index'); +console.log('-----HitlerSort-----'); console.log(Sort.HitlerSort([1, 2, 3, 4, 5, 1, 3, 5, 6])); console.log(Sort.HitlerSort([5, 6, 3, 1, 4, 7, 6, 5, 1])); console.log(Sort.HitlerSort([6, 3, 3, 4, 7, 1, 3, 2, 5])); console.log(Sort.HitlerSort([1, 9, 8, 4, 4, 2, 8, 2, 3])); +console.log('-----BubbleSort-----'); console.log(Sort.BubbleSort([1, 2, 3, 4, 5, 1, 3, 5, 6])); console.log(Sort.BubbleSort([5, 6, 3, 1, 4, 7, 6, 5, 1])); console.log(Sort.BubbleSort([6, 3, 3, 4, 7, 1, 3, 2, 5])); console.log(Sort.BubbleSort([1, 9, 8, 4, 4, 2, 8, 2, 3])); +console.log('-----QuickSort-----'); console.log(Sort.QuickSort([1, 2, 3, 4, 5, 1, 3, 5, 6])); console.log(Sort.QuickSort([5, 6, 3, 1, 4, 7, 6, 5, 1])); console.log(Sort.QuickSort([6, 3, 3, 4, 7, 1, 3, 2, 5]));