Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolucion tarea algoritmos #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
font-weight: normal;
background-color: cadetblue;
}
.innerBox{
/* width: 100%; */
display:inline-flex;
flex-shrink: 1;
background: whitesmoke;
padding: 40px;
margin: 10px;
box-sizing: border-box;
justify-content: center;
align-items: center;
text-align: center;
}
.text {
font-size: 30px;
display: inline-flex;
flex-shrink: 1;
font-weight: 600;
padding: 10px;
}

</style>
</head>
<body>
<div class="container">
<div class="innerBox">
<p class="text" id="listaOrdenada"></p>
</div>
</div>
<script src="tarea.js"></script>
</body>
</html>
67 changes: 67 additions & 0 deletions tarea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 1.- planteamineto del problema: se tiene una lista desordenada de numeros ,
// con diferentes valores que van del 0 al 13, para lo cual se busca ordenarlos con un algoritmo Quicksort
// , para lo cual tenemos en la lista un numero ya conocido el cual es 0.
// 2.- por tanto los datos de entrada, seran una cantidad N de numeros, qiue tienen valor ente 0 - 13, lo cual indica que pueden ser: [0,2,12,11,5,1,6,9,3,10,4,7,8,12,3,2,1,8].
// 3.- se tienen que hacer un comprativo entre numeros determinando cual es mayor a cual y ordenando de mayor a menor, ya que lo que se pide resolver en el problema es que la primera posicion sea a0,
// a0 por tanto sera siempre el primer numero del array que hagamos
// entonces se hara el ordenamiento, no importando que haya numeros repetidos.


const ordenarDeMenorAmayor = (a, b) => {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
};

const quickSort = (
arrayDesordenado,
ordenamientoAlgoritmo = ordenarDeMenorAmayor
) => {

const ordenarArray = [...arrayDesordenado];

const cambiarDeLugar = (arrayAmover, i, j) => {
const a = arrayAmover[i];
arrayAmover[i] = arrayAmover[j];
arrayAmover[j] = a;
};

const particion = (arrayDivision, start, end) => {
const pivote = arrayDivision[end];
let separarIndice = start;
for (let j = start; j <= end - 1; j++) {
const sortValue = ordenamientoAlgoritmo(arrayDivision[j], pivote);
if (sortValue === -1) {
cambiarDeLugar(arrayDivision, separarIndice, j);
separarIndice++;
}
}
cambiarDeLugar(arrayDivision, separarIndice, end);
return separarIndice;
};


const recursiveSort = (arraytoSort, start, end) => {

if (start < end) {
const pivotePosition = particion(arraytoSort, start, end);
recursiveSort(arraytoSort, start, pivotePosition - 1);
recursiveSort(arraytoSort, pivotePosition + 1, end);
}
};

//ordenar el array
recursiveSort(ordenarArray, 0, arrayDesordenado.length - 1);
return ordenarArray;
};

const listaN = [45,2,12,11,5,1,6,9,3,100,4,7,8,12,3,2,1,8];
const ListNordenada = quickSort(listaN);
// console.log(ListNordenada);
document.getElementById('listaOrdenada').innerHTML =
`Esta es la lista ordenada: <br> ${ListNordenada.join(', ')}<br> <br>
el resultado de a0 es =${ListNordenada[0]}`;