The question is:
You really know if your algorithm works for all possible cases?
When making a purely comparative ordering algorithm the first thing you should consider is the number of combinations that can be generated by the amount of elements in the input.
To find out this number of combinations just calculate the factorial of the element number that will be ordered, as are three elements to be ordered then will be 3! = 6
different combinations that the input may be willing:
- smaller, medium , larger
- minor, major, medium, medium
- medium , smaller, larger
- medium , larger, smaller
- minor, minor, minor, minor
- larger, smaller, smaller, smaller
The second thing you should consider is whether your algorithm works in all cases.
For the purpose of testing I built an array with the numbers 1, 2 and 3 that will be used to test all possibilities of the algorithm:
teste = [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]];
Then I encapsulated your algorithm in a function to facilitate testing:
function ordenar(entrada){
let maior, menor, meio;
let n1 = entrada[0];
let n2 = entrada[1];
let n3 = entrada[2];
if ((n1 > n2) && (n1 > n3)) {
maior = n1;
if (n2 < n3) {
meio = n3
menor = n2
} else {
meio = n2
menor = n3
}
} else if ((n2 > n1) && (n2 > n3)) {
maior = n2;
if (n1 < n3) {
meio = n3
menor = n1
} else {
meio = n1
menor = n3
}
}
return [menor,meio,maior]
}
It is the same algorithm of the question only that instead of receiving input from the console it will receive at once one array
three-element input, simulating input by the three-number console.
So I created a test function that iterates over the different possible non-repeating combinations formed by the numbers 1, 2 and 3:
//Teste de sucesso do algorítimo de ordenação.
teste.forEach((caso) =>{
//Obtem o resultado do algorítimo de ordenação para o caso a ser analisado.
let resultado = ordenar(caso);
console.log(`------------------------------`);
console.log(`entrada: ${caso}`);
console.log(`resultado: ${resultado}`);
//Faz uma comparação superficial entre o resultado esperado e o resultado
console.log(`teste ${JSON.stringify([1,2,3])==JSON.stringify(resultado)? 'APROVADO': 'REPROVADO'}`);
});
So I took the test here https://repl.it/repls/LoneSoulfulMonad
Resulting:
------------------------------
entrada: 1,2,3
resultado: ,,
teste REPROVADO
------------------------------
entrada: 1,3,2
resultado: 1,2,3
teste APROVADO
------------------------------
entrada: 2,1,3
resultado: ,,
teste REPROVADO
------------------------------
entrada: 2,3,1
resultado: 1,2,3
teste APROVADO
------------------------------
entrada: 3,1,2
resultado: 1,2,3
teste APROVADO
------------------------------
entrada: 3,2,1
resultado: 1,2,3
teste APROVADO
As can be seen your algorithm failed in two cases:
------------------------------
entrada: 1,2,3
resultado: ,,
teste REPROVADO
------------------------------
entrada: 2,1,3
resultado: ,,
teste REPROVADO
The first case is the simplest to solve, is when the input is already sorted 1,2,3
just assign the variables menor
, meio
and maior
the input values as provided:
//Solução para o caso da entrada já vier ordenada.
let menor = n1 = entrada[0];
let meio = n2 = entrada[1];
let maior = n3 = entrada[2];
The second case where the failed algorithm is 2,1,3
. Fails because the test condition for that input is not present in your algorithm which would be (n1 > n2) && (n1 < n3)
. Making the changes in your algorithm:
function ordenar(entrada){
//Caso da entrada já vier ordenada.
let menor = n1 = entrada[0];
let meio = n2 = entrada[1];
let maior = n3 = entrada[2];
if (n1 > n2){
if (n1 > n3) {
maior = n1;
if (n2 < n3) {
meio = n3
menor = n2
} else {
//meio = n2; <-- Se tornou redundante
menor = n3
}
} else { //caso (n1 < n3)
menor = n2
meio = n1
}
} else if ((n1 < n2) && (n2 > n3)) {
maior = n2;
if (n1 < n3) {
meio = n3
//menor = n1 <-- Se tornou redundante
} else {
meio = n1
menor = n3
}
}
return [menor,meio,maior]
}
So I took this test: https://repl.it/repls/VisibleLemonchiffonForms
Which resulted:
------------------------------
entrada: 1,2,3
resultado: 1,2,3
teste APROVADO
------------------------------
entrada: 1,3,2
resultado: 1,2,3
teste APROVADO
------------------------------
entrada: 2,1,3
resultado: 1,2,3
teste APROVADO
------------------------------
entrada: 2,3,1
resultado: 1,2,3
teste APROVADO
------------------------------
entrada: 3,1,2
resultado: 1,2,3
teste APROVADO
------------------------------
entrada: 3,2,1
resultado: 1,2,3
teste APROVADO
Honestly I think that according to the given parameters you sought the most difficult path.
The simplest way would be to declare an array to receive the inputs and apply the method sort()
.
var n= new Array(3);
rs = require("readline-sync")
n[0] = rs.questionInt("Digite o primeiro valor ")
n[1] = rs.questionInt("Digite o segundo valor ")
n[2] = rs.questionInt("Digite o terceiro valor ")
n.sort();
console.log(n);
If you don’t want to use method sort()
can use the structuring syntax to exchange array elements while testing them:
var n= new Array(3);
rs = require("readline-sync")
n[0] = rs.questionInt("Digite o primeiro valor ")
n[1] = rs.questionInt("Digite o segundo valor ")
n[2] = rs.questionInt("Digite o terceiro valor ")
if (n[0] > n[1]) [n[0],n[1]] = [n[1],n[0]];
if (n[1] > n[2]) [n[1],n[2]] = [n[2],n[1]];
if (n[0] > n[1]) [n[0],n[1]] = [n[1],n[0]];
console.log(n);
The tests of these last two algorithms: https://repl.it/repls/CompetentAdmiredEditors
If you are curious about more elaborate ordering methods see this list and search here on the site the names of the algorithms contained in this list.
Important you [Dit] your post and explain in detail the problem, describing what you tried and where is the current difficulty, preferably with a [mcve]. Studying the post available on this link can make a very positive difference in your use of the site: Stack Overflow Survival Guide in English
– Bacco
Luiz, the problem is this phrase "if you type 8-4-1 it shows 1-4-8 already contrary not" should be something like "if I type a disorderly input, 8-4-1, it works. But if I type an already ordered input, 1-4-8, the algorithm doesn’t work.". We are a question and answer website(Q&A), so the clarity of the question is very important to us.
– Augusto Vasques