added stalin sort
This commit is contained in:
@@ -15,7 +15,7 @@ router.post('/', (req, res) => {
|
||||
return String(a).localeCompare(String(b));
|
||||
});
|
||||
|
||||
res.json({ sortedArray });
|
||||
res.json({ ret: sortedArray });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
29
src/routes/api/stalinSort.js
Normal file
29
src/routes/api/stalinSort.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
const { input } = req.body;
|
||||
|
||||
if (!Array.isArray(input)) {
|
||||
return res.status(400).json({ error: 'Request body muss ein Feld "array" mit einem Array enthalten' });
|
||||
}
|
||||
|
||||
const sortedArray = Array();
|
||||
sortedArray.push(input[0]);
|
||||
let executions = 0;
|
||||
|
||||
for (let i = 1; i < input.length; i++) {
|
||||
if (input[i] > input[i - 1]) {
|
||||
sortedArray.push(input[i]);
|
||||
} else {
|
||||
executions++;
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
ret: sortedArray,
|
||||
executions
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -39,7 +39,7 @@
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sortedArray": {
|
||||
"ret": {
|
||||
"type": "array",
|
||||
"items": {}
|
||||
}
|
||||
@@ -51,6 +51,48 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/stalinSort": {
|
||||
"post": {
|
||||
"summary": "Sorts an array using Stalin Sort",
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"input": {
|
||||
"type": "array",
|
||||
"items": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Sorted array and execution count",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"ret": {
|
||||
"type": "array",
|
||||
"items": {}
|
||||
},
|
||||
"executions": {
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/isEven": {
|
||||
"post": {
|
||||
"summary": "Checks if number is even",
|
||||
|
||||
@@ -2,6 +2,7 @@ const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const path = require('path');
|
||||
const sortRoute = require('./routes/api/sort');
|
||||
const stalinSortRoute = require('./routes/api/stalinSort');
|
||||
const isEvenRoute = require('./routes/api/isEven');
|
||||
const isOddRoute = require('./routes/api/isOdd');
|
||||
const toStringRoute = require('./routes/api/toString');
|
||||
@@ -17,6 +18,7 @@ app.use(express.static(path.join(__dirname, '../www')));
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.use('/api/sort', sortRoute);
|
||||
app.use('/api/stalinSort', stalinSortRoute);
|
||||
app.use('/api/isEven', isEvenRoute);
|
||||
app.use('/api/isOdd', isOddRoute);
|
||||
app.use('/api/toString', toStringRoute);
|
||||
|
||||
Reference in New Issue
Block a user