Skip to content

Commit 00834d7

Browse files
author
a.shatalov
committed
bubbling sort with tests
1 parent 6587802 commit 00834d7

File tree

4 files changed

+4099
-0
lines changed

4 files changed

+4099
-0
lines changed

bubblingSort/bubblingSort.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function bubblingSort(data) {
2+
const lastIndex = data.length - 1;
3+
for (let i = 0; i < lastIndex; i++) {
4+
for (let z = 0; z < lastIndex - i; z++) {
5+
const leftValue = data[z];
6+
const rightValue = data[z + 1];
7+
if (leftValue > rightValue) {
8+
data[z] = rightValue;
9+
data[z + 1] = leftValue;
10+
}
11+
}
12+
}
13+
return data;
14+
}

bubblingSort/bubblingSort.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { bubblingSort } from './bubblingSort';
2+
3+
const testData1 = [1, 9, 5, 3, 7, 8];
4+
const testData2 = [7, 8, 9, 3, 1, 5];
5+
const testData3 = [9, 7, 5, 1, 3, 8];
6+
const testData4 = [5, 7, 8, 3, 9, 1];
7+
const testData5 = [8, 3, 1, 7, 5, 9];
8+
9+
const sortedData = [1, 3, 5, 7, 8, 9];
10+
11+
test('bubbling sort', () => {
12+
expect(bubblingSort(testData1)).toEqual(sortedData);
13+
expect(bubblingSort(testData2)).toEqual(sortedData);
14+
expect(bubblingSort(testData3)).toEqual(sortedData);
15+
expect(bubblingSort(testData4)).toEqual(sortedData);
16+
expect(bubblingSort(testData5)).toEqual(sortedData);
17+
});

package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "jsSortExample",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"repository": "https://github.com/alex-shatalov/jsSortExample.git",
6+
"author": "a.shatalov",
7+
"license": "MIT",
8+
"scripts": {
9+
"test": "jest --watch"
10+
},
11+
"devDependencies": {
12+
"babel-preset-env": "^1.7.0",
13+
"jest": "^23.6.0"
14+
},
15+
"babel": {
16+
"presets": [
17+
"env"
18+
]
19+
}
20+
}

0 commit comments

Comments
 (0)