mirror of
https://github.com/Yet-Another-DreamTeam/js_algorithms.git
synced 2026-03-13 08:09:40 +00:00
add dataGenerator.js
This commit is contained in:
parent
d4617312ea
commit
ab3a8ebaeb
22
dataGenerator.js
Normal file
22
dataGenerator.js
Normal file
@ -0,0 +1,22 @@
|
||||
function generateSortedIntArray(length) {
|
||||
let array = [];
|
||||
for (let index = 0; index < length; index++) {
|
||||
array.push(index + 1);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
function generateUnsortedIntArray(length) {
|
||||
let array = generateSortedIntArray(length);
|
||||
|
||||
array.sort(function (a, b) {
|
||||
return 0.5 - Math.random();
|
||||
});
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateSortedIntArray,
|
||||
generateUnsortedIntArray,
|
||||
};
|
||||
6
index.js
6
index.js
@ -1,6 +1,8 @@
|
||||
let bubblesort = require("./bubblesort");
|
||||
let dataGenerator = require("./dataGenerator");
|
||||
|
||||
console.log(bubblesort([1, 3, 2], true));
|
||||
let data = dataGenerator.generateUnsortedIntArray(1000);
|
||||
|
||||
console.log(dataGenerator.generateUnsortedIntArray(10));
|
||||
console.time("bubblesort");
|
||||
console.log(bubblesort(data, true));
|
||||
console.timeEnd("bubblesort");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user