R
The question is:Do you really know if your algorithm works for every possible case?By making a purely comparative sorting algorithm the first thing that should consideration is the amount of combinations that can be generated by the amount of elements in the input.To discover this number of combinations just calculate the factorial of the number of element that will be ordered, as are three element to be ordered then will be 3! = 6 different combinations that the input may be willing:smaller, medium, largersmaller, larger, mediummeans, smaller, largermeans, larger, smallerbigger, smaller, halfbigger, medium, smallerThe second thing you should consider is if your algorithm works in all cases.For testing purposes I built an array with 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 the entrance by the console it will receive at once one array of three elements with input, simulating the input by the three-number console.Then I created a test function that iterates about the different possible non-repeated combinations formed by 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 did 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
How can your algorithm be seen 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 entry is already ordered 1,2,3 just assign the variables menor, meio and maior the input values as set out://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 in which the algorithmic failure is 2,1,3. Failure because the test condition for this entry is not present in your algorithm that would be (n1 > n2) && (n1 < n3). Making the changes 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 did 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 searched the most difficult path.The simplest way would be to declare an array to receive the entries and apply the method https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/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 do not want to use method sort() can use https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Operators/Atribuicao_via_desestruturacao to permute the 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 algorithmic: https://repl.it/repls/CompetentAdmiredEditors If you are curious about more elaborate ordination methods https://pt.wikipedia.org/wiki/Algoritmo_de_ordena%C3%A7%C3%A3o and search here on the site the names of the algorithms contained in this list.