Ver Mensaje Individual
  #12 (permalink)  
Antiguo 23/10/2011, 01:14
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 19 años, 10 meses
Puntos: 834
Respuesta: Desafios Javascript, ronda 2

Esta es mi implementación del ejercicio de ordenamiento (ajusté el quicksort clásico a los requerimientos del ejecicio):
Código:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script type="text/javascript">
function quicksort(arr){
 	while(arr.length){
		var opts=[[],[]],
		piv = arr[0],
		tmp=[].concat(arr.slice(1));
		while(tmp.length){
			var o=tmp.shift();
			opts[+Boolean((o-piv).toString().indexOf('-'))].push(o);
		}
		return quicksort(opts[0]).concat(piv, quicksort(opts[1]));
	}
	return [];
}
onload=function(){
	var a=[],i=0;
	for(;i<10;i++)
		a.push(Math.round(Math.random()*100));
	document.getElementById('log').innerHTML=a+'-->'+quicksort(a);
}
</script>
</head>

<body>
<div id="log"></div>
</body>
</html>
http://jsfiddle.net/panino/msBhE/