-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Felipe Natan
committed
Mar 5, 2020
1 parent
5773877
commit c0eecc5
Showing
5 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const reverse = require('./reverseArray'); | ||
|
||
function bubbleSort(arr){ | ||
let arr2 = [...arr]; | ||
for(let i = 0; i < arr2.length; i++){ | ||
for(let j = i+1; j < arr2.length; j++){ | ||
if(arr2[i] > arr2[j]){ | ||
let temp = arr2[i]; | ||
arr2[i] = arr2[j]; | ||
arr2[j] = temp; | ||
} | ||
} | ||
} | ||
|
||
return arr2; | ||
} | ||
|
||
module.exports = {sort:bubbleSort , reverse}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function reverse(arr){ | ||
let reversedArr = [] | ||
for(let i = arr.length-1; i >= 0; i--) | ||
reversedArr.push(arr[i]); | ||
return reversedArr; | ||
} | ||
|
||
module.exports = reverse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const assert = require('assert'); | ||
const BubbleSort = require('../../src/algorithms/sorting/bubbleSort'); | ||
|
||
describe('BubbleSort', function() { | ||
const arr = [6,2,7,15,3,4,10,-8,56]; | ||
const sortedArr = [-8,2,3,4,6,7,10,15,56]; | ||
const reversedArr = [56,15,10,7,6,4,3,2,-8]; | ||
|
||
it('should sort an array', () => { | ||
assert.deepEqual(BubbleSort.sort(arr), sortedArr); | ||
}); | ||
|
||
|
||
it('should return a reversed array', () => { | ||
let tempArr = BubbleSort.sort(arr); | ||
assert.deepEqual(BubbleSort.reverse(tempArr), reversedArr); | ||
}); | ||
}); |