bubble sort is so broken
This commit is contained in:
@@ -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]));
|
||||
|
||||
Reference in New Issue
Block a user