Ver Mensaje Individual
  #4 (permalink)  
Antiguo 27/10/2004, 12:55
Avatar de AlZuwaga
AlZuwaga
Colaborador
 
Fecha de Ingreso: febrero-2001
Ubicación: 34.517 S, 58.500 O
Mensajes: 14.550
Antigüedad: 24 años, 2 meses
Puntos: 535


Ouch, acabo de terminarlo yo también.
Bueno, te paso otra opción que debe ser muy similar a la de u_g

Hace uso de una función que encontré en 4GuysFromRolla.com y sirve para ordenar el array:

Código:
Sub QuickSort(vec,loBound,hiBound)
  Dim pivot,loSwap,hiSwap,temp

  '== This procedure is adapted from the algorithm given in:
  '==    Data Abstractions & Structures using C++ by
  '==    Mark Headington and David Riley, pg. 586
  '== Quicksort is the fastest array sorting routine for
  '== unordered arrays.  Its big O is  n log n


  '== Two items to sort
  if hiBound - loBound = 1 then
    if vec(loBound) > vec(hiBound) then
      temp=vec(loBound)
      vec(loBound) = vec(hiBound)
      vec(hiBound) = temp
    End If
  End If

  '== Three or more items to sort
  pivot = vec(int((loBound + hiBound) / 2))
  vec(int((loBound + hiBound) / 2)) = vec(loBound)
  vec(loBound) = pivot
  loSwap = loBound + 1
  hiSwap = hiBound
  
  do
    '== Find the right loSwap
    while loSwap < hiSwap and vec(loSwap) <= pivot
      loSwap = loSwap + 1
    wend
    '== Find the right hiSwap
    while vec(hiSwap) > pivot
      hiSwap = hiSwap - 1
    wend
    '== Swap values if loSwap is less then hiSwap
    if loSwap < hiSwap then
      temp = vec(loSwap)
      vec(loSwap) = vec(hiSwap)
      vec(hiSwap) = temp
    End If
  loop while loSwap < hiSwap
  
  vec(loBound) = vec(hiSwap)
  vec(hiSwap) = pivot
  
  '== Recursively call function .. the beauty of Quicksort
    '== 2 or more items in first section
    if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1)
    '== 2 or more items in second section
    if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound)

End Sub  'QuickSort

Luego está la función que hice para obtener la mediana:

Código:
Function Mediana(x)
	CantidadElementos = UBound(x) + 1
	If CantidadElementos mod 2 = 0 then
		Inferior = (CantidadElementos / 2 - 1)
		Superior = Inferior + 1
		ValorMediana = (CDbl(x(Inferior)) + CDbl(x(Superior))) / 2
		Mensaje = "El conjunto es par y cuenta de " & CantidadElementos & " elementos.<br>" _
			& " El 'mediano inferior' es el de índice " & Inferior & " dentro del array" _
			& " (con valor " & x(Inferior) & ") y el 'mediano superior' es el de índice " _
			& Superior & " dentro del array (con valor " & x(Superior) & ").<br>" _
			& " Por lo tanto la mediana es: "
		Mediana = Mensaje & ValorMediana
	Else
		Mensaje = "El conjunto es imparpar y cuenta de " & CantidadElementos & " elementos." _
			& "<br> Por lo tanto la mediana está en la posición "_
			& Int(CantidadElementos / 2) & " del array y es: "
		ValorMediana = x(Int(CantidadElementos / 2))
		Mediana = Mensaje & ValorMediana
		End if
End Function

Y la llamás así:

Código:
Valores = "15 , 13 , 11 , 14 , 16 , 10 , 12 , 18"
Valores = Replace(Valores, " ", "")
arrValores = Split(Valores, ",")

Call QuickSort(arrValores, LBound(arrValores), UBound(arrValores))
Response.Write Mediana(arrValores)
Saludos
__________________
...___...

Última edición por AlZuwaga; 27/10/2004 a las 13:02