A sorting array script for multi-d arrays

# Thread: A sorting array script for multi-d arrays

1. Member
Join Date
Dec 1969
Posts
59

## A sorting array script for multi-d arrays

Even mine was even backwards. The script he redid is totally upside down. He had the column position for a row position. This one below works.<BR><BR>&#060;%@ Language="VBScript" %&#062;<BR>&#060;%<BR><BR>&#039;== This entire piece of code was shamelessly stolen from<BR>&#039;== the 4 Guys From Rolla WebWeekly newsletter, translated<BR>&#039;== to VBScript and changed into server-side ASP code.<BR>&#039;== Every effort has been made to keep comments intact.<BR><BR><BR><BR>Response.Write "&#060;HTML&#062;&#060;HEAD&#062;&#060;/HEAD&#062;&#060;BODY BGCOLOR=""WHITE""&#062;"<BR><BR>Sub QuickSort(vec,loBound,hiBound,numRows)<BR> Dim pivot,loSwap,hiSwap,temp<BR><BR> &#039;== This procedure is adapted from the algorithm given in:<BR> &#039;== Data Abstractions & Structures using C++ by<BR> &#039;== Mark Headington and David Riley, pg. 586<BR> &#039;== Quicksort is the fastest array sorting routine for<BR> &#039;== unordered arrays. Its big O is n log n<BR><BR><BR> &#039;== Two items to sort<BR> if hiBound - loBound = 1 then<BR> if vec(0,loBound) &#062; vec(0,hiBound) then<BR><BR> for rowcount=0 to numRows<BR> temp=vec(rowcount,loBound)<BR> vec(rowcount,loBound) = vec(rowcount,hiBound)<BR> vec(rowcount,hiBound) = temp<BR> next<BR><BR> End If<BR> End If<BR><BR> &#039;== Three or more items to sort<BR><BR> redim pivot(numRows+1)<BR> for rowcount=0 to numRows<BR> pivot(rowcount) = vec(rowcount,int((loBound + hiBound) / 2))<BR> vec(rowcount,int((loBound + hiBound) / 2)) = vec(rowcount,loBound)<BR> vec(rowcount,loBound) = pivot(rowcount)<BR> next<BR><BR> loSwap = loBound + 1<BR> hiSwap = hiBound<BR><BR> do<BR> &#039;== Find the right loSwap<BR> while loSwap &#060; hiSwap and vec(0,loSwap) &#060;= pivot(0)<BR> loSwap = loSwap + 1<BR> wend<BR> &#039;== Find the right hiSwap<BR> while vec(0,hiSwap) &#062; pivot(0)<BR> hiSwap = hiSwap - 1<BR> wend<BR> &#039;== Swap values if loSwap is less then hiSwap<BR> if loSwap &#060; hiSwap then<BR> for rowcount=0 to numRows<BR> temp = vec(rowcount,loSwap)<BR> vec(rowcount,loSwap) = vec(rowcount,hiSwap)<BR> vec(rowcount,hiSwap) = temp<BR> next<BR> End If<BR> loop while loSwap &#060; hiSwap<BR><BR>for rowcount=0 to numRows<BR> vec(rowcount,loBound) = vec(rowcount,hiSwap)<BR> vec(rowcount,hiSwap) = pivot(rowcount)<BR>next<BR><BR><BR><BR><BR> &#039;== Recursively call function .. the beauty of Quicksort<BR> &#039;== 2 or more items in first section<BR> if loBound &#060; (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,numRows)<BR> &#039;== 2 or more items in second section<BR> if hiSwap + 1 &#060; hibound then Call QuickSort(vec,hiSwap+1,hiBound,numRows)<BR><BR>End Sub &#039;QuickSort<BR><BR>Sub PrintArray(vec,lo,hi)<BR> &#039;== Simply print out an array from the lo bound to the hi bound.<BR> Response.Write "&#060;strong&#062;col1 col2 col3&#060;/strong&#062;<BR>"<BR> Dim i<BR> For i = lo to hi<BR> Response.Write "&#060;i&#062;"&vec(0,i) & "&#060;/i&#062; " & vec(1,i) & " " & vec(2,i) & "<BR>"<BR> Next<BR>End Sub &#039;PrintArray<BR><BR><BR>Randomize<BR><BR>Di m x(3,3)<BR><BR>For z = 0 to 2<BR> x(0,z) = int(Rnd*1000)<BR> x(1,z) = "test"&z<BR> x(2,z) = "test-"&z<BR> If (Rnd &#060; 0.5) then x(0,z) = x(0,z)-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!&#060;/BR&#062;"<BR>Call PrintArray(x,0,2)<BR><BR>Response.Write "&#060;/BODY&#062;&#060;/HTML&#062;"<BR>%&#062;

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Much better...no overflow...

...among other things.<BR><BR>I rewrote your printarray and array construction code to be more flexible and do better testing:<BR><BR>************<BR>&#060;%<BR>...<BR> <BR>Sub PrintArray(vec) <BR> Response.Write "&#060;TABLE Border=1 CellPadding=5&#062;"<BR> For row = 0 TO UBound(vec,2) <BR> Response.Write "&#060;TR&#062;"<BR> For col = 0 To UBound(vec,1) <BR> Response.Write "&#060;td&#062;" & vec(col,row) & "&#060;/td&#062;"<BR> Next<BR> Response.Write "&#060;/TR&#062;"<BR> Next<BR> Response.Write "&#060;/TABLE&#062;&#060;HR&#062;&#060;P&#062;"<BR>End Sub <BR><BR><BR>Randomize <BR><BR>Dim x()<BR>ReDim x(1 + Int(rnd()*5), 3 + Int(Rnd()*20) ) <BR><BR>For row = 0 to UBound(x,2)<BR> x(0,row) = int(Rnd*2000) - 1000<BR> For col = 1 To UBound(x,1)<BR> x(col,row) = "Row " & row & ", Col " & col<BR> Next<BR>Next <BR><BR>PrintArray x <BR><BR>QuickSort x, 0, UBound(x,2), UBound(x,1)<BR><BR>PrintArray x <BR><BR>Response.Write "&#060;/BODY&#062;&#060;/HTML&#062;" <BR>%&#062;

3. Member
Join Date
Dec 1969
Posts
59

## RE: Much better...no overflow...

This script should belong to the hall of shame. I&#039;m having some fun here. Just a note rowcount should colcount and numRows should be numColumns. The whole is upside down. I&#039;m having a beer.

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## But the original *DOES* work...

...excepting only that he has the columns and rows reversed.<BR><BR>Still, I can&#039;t understand the use of any of this. Nobody *should* be sorting 2D arrays.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## And I would point out...

...that the article is one of the earlies on this site. January 27, 1999.<BR><BR>The site was just 5 months old at the time.<BR><BR>

6. Member
Join Date
Dec 1969
Posts
59

## RE: Much better...no overflow...

The old one is totally wrong. The print statement from the original 1-D printed the columns vertically. His version is printing the array backwards. Look at it closely. The highlighted column is actually a row flipped printing downward. And I&#039;m very interested in the other solution for sorting 2-D arrays. Select it sorted from the DB? I found this script at Yahoo and Google. And 100s of web sites have copied it as correct. Replace the sub calls with these lines and you will see it.<BR><BR>Response.Write "Here is a jumbled array:<BR>"<BR>Call PrintArray(x,0,9,col)<BR><BR>Response.Write "<BR>"<BR>Response.Write "<BR>"<BR><BR>Response.Write x(0,0)&" "&x(1,0)&" "&x(2,0)<BR><BR>Response.Write "<BR>"<BR>Response.Write "<BR>"<BR><BR>Call QuickSort(x,0,9,col)<BR><BR>Response.Write "<BR>Now the array is sorted!<BR>"<BR>Call PrintArray(x,0,9,col)<BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## I have no idea what...

...you are talking about.<BR><BR>I took his code, *VERBATIM*, and ran it on my machine. It worked perfectly.<BR><BR>You wrote:<BR> The highlighted column is actually a row flipped printing downward.<BR><BR>No, it is simply because he addresses cells via <BR> cell = theArray( row, column )<BR>But when he displays both the before and after arrays, he shows them in *HIS* row order and the array *is* sorted correctly.<BR><BR>I did not alter one single character of his code and it worked.<BR><BR>Period.<BR><BR>**************<BR><B R>And I really don&#039;t care where you found the code, it&#039;s an utter waste of time and effort, so far as I&#039;m concerned. I cannot imagine what the point of sorting a 2D array in VBScript is. I simply can&#039;t think of any circumstances where that&#039;s a smart thing to do. [Watch, somebody will find one odd case. Okay, fine. So I&#039;m only right the other 99.8736% of the time.]<BR><BR>

8. Member
Join Date
Dec 1969
Posts
59

## RE: I have no idea what...

OK he has row and columns reversed. You say it has no purpose. Then why did you publish it to begin with?

9. Member
Join Date
Dec 1969
Posts
59

## Just a note

Which one is correct? array(column,row) or array(row,column). If the first is correct, his script is backwards. If the second is correct, you proved me wrong. If pass an array using array(column,row), the script prints backwards.

10. Member
Join Date
Dec 1969
Posts
59

## I apologize

Bill,<BR><BR>If array(column,row) is the correct form, the build array is backwards.<BR><BR>Currently published like this...<BR>Dim x(9,5),z,y<BR>Const col = 0<BR><BR><BR>For z = 0 to 9<BR> For y = 0 to 5<BR> x(z,y) = int(Rnd*1000)<BR> If (Rnd &#060; 0.5) then x(z,y) = x(z,y)-1000<BR> Next<BR>Next<BR><BR><BR>Response.Write "Here is a jumbled array:<BR>"<BR>Call PrintArray(x,0,9,col)<BR><BR>Response.Write "<BR>"<BR>Response.Write "<BR>"<BR><BR>Response.Write x(0,0)&" "&x(0,1)&" "&x(0,2)<BR><BR>Response.Write "<BR>"<BR>Response.Write "<BR>"<BR><BR>Call QuickSort(x,0,9,col)<BR><BR>Response.Write "<BR>Now the array is sorted!<BR>"<BR>Call PrintArray(x,0,9,col)<BR><BR>I reveresed the array and it worked fine.<BR><BR>Dim x(5,9),z,y<BR>Const col = 0<BR><BR><BR>For z = 0 to 5<BR> For y = 0 to 9<BR> x(z,y) = int(Rnd*1000)<BR> If (Rnd &#060; 0.5) then x(z,y) = x(z,y)-1000<BR> Next<BR>Next<BR><BR><BR>Response.Write "Here is a jumbled array:<BR>"<BR>Call PrintArray(x,0,5,col)<BR><BR>Response.Write "<BR>"<BR>Response.Write "<BR>"<BR><BR>Response.Write x(0,0)&" "&x(0,1)&" "&x(0,2)<BR><BR>Response.Write "<BR>"<BR>Response.Write "<BR>"<BR><BR>Call QuickSort(x,0,5,col)<BR><BR>Response.Write "<BR>Now the array is sorted!<BR>"<BR>Call PrintArray(x,0,5,col)<BR><BR>This has purpose. But to talk about that is totally out of scope. I&#039;m use array(column,row) which printed backwards. The script works great.<BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•