<%@ Language="VBScript" %><BR><%<BR><BR>'== This entire piece of code was shamelessly stolen from<BR>'== the 4 Guys From Rolla WebWeekly newsletter, translated<BR>'== to VBScript and changed into server-side ASP code.<BR>'== Every effort has been made to keep comments intact.<BR><BR><BR>'Addition to comment above. Shamelessly stolen is right.<BR>'His script sorts one row of the array. He printed the array<BR>'backwards. Take a look at this script. This is sorting one<BR>'row. This is no different than one-d script. It's the same<BR>'thing. I can't believe this is published on this web site.<BR>'http://www.4guysfromrolla.com/webtech/012799-3.shtml<BR>'This script below actually sorts the rows. It uses a column<BR>'to determine the rows order. I would hate to see this guy<BR>'design an SQL statement. That scares me.<BR><BR>Response.Write "<HTML><HEAD></HEAD><BODY BGCOLOR=""WHITE"">"<BR><BR>Sub QuickSort(vec,loBound,hiBound,numRows)<BR> Dim pivot,loSwap,hiSwap,temp<BR><BR> '== This procedure is adapted from the algorithm given in:<BR> '== Data Abstractions & Structures using C++ by<BR> '== Mark Headington and David Riley, pg. 586<BR> '== Quicksort is the fastest array sorting routine for<BR> '== unordered arrays. Its big O is n log n<BR><BR><BR> '== Two items to sort<BR> if hiBound - loBound = 1 then<BR> if vec(loBound,0) > vec(hiBound,0) then<BR><BR> for rowcount=0 to numRows<BR> temp=vec(loBound,rowcount)<BR> vec(loBound,rowcount) = vec(hiBound,rowcount)<BR> vec(hiBound,rowcount) = temp<BR> next<BR><BR> End If<BR> End If<BR><BR> '== Three or more items to sort<BR><BR> redim pivot(numRows+1)<BR> for rowcount=0 to numRows<BR> pivot(rowcount) = vec(int((loBound + hiBound) / 2),rowcount)<BR> vec(int((loBound + hiBound) / 2),rowcount) = vec(loBound,rowcount)<BR> vec(loBound,rowcount) = pivot(rowcount)<BR> next<BR><BR> loSwap = loBound + 1<BR> hiSwap = hiBound<BR><BR> do<BR> '== Find the right loSwap<BR> while loSwap < hiSwap and vec(loSwap,0) <= pivot(0)<BR> loSwap = loSwap + 1<BR> wend<BR> '== Find the right hiSwap<BR> while vec(hiSwap,0) > pivot(0)<BR> hiSwap = hiSwap - 1<BR> wend<BR> '== Swap values if loSwap is less then hiSwap<BR> if loSwap < hiSwap then<BR> for rowcount=0 to numRows<BR> temp = vec(loSwap,rowcount)<BR> vec(loSwap,rowcount) = vec(hiSwap,rowcount)<BR> vec(hiSwap,rowcount) = temp<BR> next<BR> End If<BR> loop while loSwap < hiSwap<BR><BR>for rowcount=0 to numRows<BR> vec(loBound,rowcount) = vec(hiSwap,rowcount)<BR> vec(hiSwap,rowcount) = pivot(rowcount)<BR>next<BR><BR><BR><BR><BR> '== Recursively call function .. the beauty of Quicksort<BR> '== 2 or more items in first section<BR> if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,numRows)<BR> '== 2 or more items in second section<BR> if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,numRows)<BR><BR>End Sub 'QuickSort<BR><BR>Sub PrintArray(vec,lo,hi)<BR> '== Simply print out an array from the lo bound to the hi bound.<BR> Response.Write "<strong>col1 col2 col3</strong><BR>"<BR> Dim i<BR> For i = lo to hi<BR> Response.Write "<i>"&vec(i,0) & "</i> " & vec(i,1) & " " & vec(i,2) & "<BR>"<BR> Next<BR>End Sub 'PrintArray<BR><BR><BR>Randomize<BR><BR>Di m x(3,2)<BR><BR>For z = 0 to 2<BR> x(z,0) = int(Rnd*1000)<BR> x(z,1) = "test"&z<BR> x(z,2) = "test-"&z<BR> If (Rnd < 0.5) then x(z,0) = x(z,0)-1000<BR>Next<BR><BR>Response.Write "Here is a jumbled array:<BR>"<BR>Call PrintArray(x,0,2)<BR><BR>Call QuickSort(x,0,2,2)<BR><BR>Response.Write "Now the array is sorted!</BR>"<BR>Call PrintArray(x,0,2)<BR><BR>Response.Write "</BODY></HTML>"<BR>%>