20. juli 2007 - 18:20
Der er
6 kommentarer og
2 løsninger
Sorter 2 Dim Array med decimal tal
Hej
Jeg bruger sortering algoritmen forneden, men fordi jeg har decimal tal saa sorter den ikke ordentligt.
Forneden er de tal den sorter paa, saa det skulle gerne vaere 3, 3,5 , 10 etc
3,50
3,00
10
10
10
Er der nogen som har en ide hvorfor det hersen sker ?
Mange Tak
Sub QuickSort(vec,loBound,hiBound,SortField,SortDir)
'==--------------------------------------------------------==
'== Sort a multi dimensional array on SortField ==
'== ==
'== 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 ==
'== ==
'== Parameters: ==
'== vec - array to be sorted ==
'== SortField - The field to sort on (1st dimension value) ==
'== loBound and hiBound are simply the upper and lower ==
'== bounds of the array's "row" dimension. It's probably ==
'== easiest to use the LBound and UBound functions to ==
'== set these. ==
'== SortDir - ASC, ascending; DESC, Descending ==
'==--------------------------------------------------------==
if not (hiBound - loBound = 0) then
Dim pivot(),loSwap,hiSwap,temp,counter
Redim pivot (Ubound(vec,2))
SortDir = UCase(SortDir)
'== Two items to sort
if hiBound - loBound = 1 then
if (SortDir = "ASC") then
if FormatCompare(vec(loBound,SortField),vec(hiBound,SortField)) > FormatCompare(vec(hiBound,SortField),vec(loBound,SortField)) then Call SwapRows(vec,hiBound,loBound)
else
if FormatCompare(vec(loBound,SortField),vec(hiBound,SortField)) < FormatCompare(vec(hiBound,SortField),vec(loBound,SortField)) then Call SwapRows(vec,hiBound,loBound)
end if
End If
'== Three or more items to sort
For counter = 0 to Ubound(vec,2)
pivot(counter) = vec(int((loBound + hiBound) / 2),counter)
vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)
vec(loBound,counter) = pivot(counter)
Next
loSwap = loBound + 1
hiSwap = hiBound
do
'== Find the right loSwap
if (SortDir = "ASC") then
while loSwap < hiSwap and FormatCompare(vec(loSwap,SortField),pivot(SortField)) <= FormatCompare(pivot(SortField),vec(loSwap,SortField))
loSwap = loSwap + 1
wend
else
while loSwap < hiSwap and FormatCompare(vec(loSwap,SortField),pivot(SortField)) >= FormatCompare(pivot(SortField),vec(loSwap,SortField))
loSwap = loSwap + 1
wend
end if
'== Find the right hiSwap
if (SortDir = "ASC") then
while FormatCompare(vec(hiSwap,SortField),pivot(SortField)) > FormatCompare(pivot(SortField),vec(hiSwap,SortField))
hiSwap = hiSwap - 1
wend
else
while FormatCompare(vec(hiSwap,SortField),pivot(SortField)) < FormatCompare(pivot(SortField),vec(hiSwap,SortField))
hiSwap = hiSwap - 1
wend
end if
'== Swap values if loSwap is less then hiSwap
if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)
loop while loSwap < hiSwap
For counter = 0 to Ubound(vec,2)
vec(loBound,counter) = vec(hiSwap,counter)
vec(hiSwap,counter) = pivot(counter)
Next
'== 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,SortField,SortDir)
'== 2 or more items in second section
if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField,SortDir)
end if
End Sub 'QuickSort
Sub SwapRows(ary,row1,row2)
'==------------------------------------------==
'== This proc swaps two rows of an array ==
'==------------------------------------------==
Dim x,tempvar
For x = 0 to Ubound(ary,2)
tempvar = ary(row1,x)
ary(row1,x) = ary(row2,x)
ary(row2,x) = tempvar
Next
End Sub 'SwapRows
function FormatCompare(sOne,sTwo)
'==------------------------------------------==
'== Checks sOne & sTwo, returns sOne as a ==
'== Numeric if both pass isNumeric, if not ==
'== returns sOne as a string. ==
'==------------------------------------------==
if (isNumeric(Trim(sOne)) AND isNumeric(Trim(sTwo))) then
FormatCompare = CDbl(Trim(sOne))
else
FormatCompare = Trim(sOne)
end if
end function
Sub PrintArray(vec,loRow,hiRow,markCol)
'==------------------------------------------==
'== Print out an array Highlight the column ==
'== whose number matches param markCol ==
'==------------------------------------------==
Dim ColNmbr,RowNmbr
For RowNmbr = loRow to hiRow
Response.Write "<tr>"
For ColNmbr = 0 to (Ubound(vec,2) - 1)
If ColNmbr = markCol then
Response.Write "<td bgcolor=""FFFFCC"">"
Else
Response.Write "<td>"
End If
Response.Write vec(RowNmbr,ColNmbr) & "</td>"
Next
Response.Write "<td>" & vec(RowNmbr,ColNmbr) & "</td>"
Response.Write "</tr>"
Next
End Sub 'PrintArray
06. august 2007 - 02:28
#5
<%
Sub QuickSort(vec,loBound,hiBound,SortField,SortDir)
'==--------------------------------------------------------==
'== Sort a multi dimensional array on SortField ==
'== ==
'== 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 ==
'== ==
'== Parameters: ==
'== vec - array to be sorted ==
'== SortField - The field to sort on (1st dimension value) ==
'== loBound and hiBound are simply the upper and lower ==
'== bounds of the array's "row" dimension. It's probably ==
'== easiest to use the LBound and UBound functions to ==
'== set these. ==
'== SortDir - ASC, ascending; DESC, Descending ==
'==--------------------------------------------------------==
if not (hiBound - loBound = 0) then
Dim pivot(),loSwap,hiSwap,temp,counter
Redim pivot (Ubound(vec,2))
SortDir = UCase(SortDir)
'== Two items to sort
if hiBound - loBound = 1 then
if (SortDir = "ASC") then
if FormatCompare(vec(loBound,SortField),vec(hiBound,SortField)) > FormatCompare(vec(hiBound,SortField),vec(loBound,SortField)) then Call SwapRows(vec,hiBound,loBound)
else
if FormatCompare(vec(loBound,SortField),vec(hiBound,SortField)) < FormatCompare(vec(hiBound,SortField),vec(loBound,SortField)) then Call SwapRows(vec,hiBound,loBound)
end if
End If
'== Three or more items to sort
For counter = 0 to Ubound(vec,2)
pivot(counter) = vec(int((loBound + hiBound) / 2),counter)
vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)
vec(loBound,counter) = pivot(counter)
Next
loSwap = loBound + 1
hiSwap = hiBound
do
'== Find the right loSwap
if (SortDir = "ASC") then
while loSwap < hiSwap and FormatCompare(vec(loSwap,SortField),pivot(SortField)) <= FormatCompare(pivot(SortField),vec(loSwap,SortField))
loSwap = loSwap + 1
wend
else
while loSwap < hiSwap and FormatCompare(vec(loSwap,SortField),pivot(SortField)) >= FormatCompare(pivot(SortField),vec(loSwap,SortField))
loSwap = loSwap + 1
wend
end if
'== Find the right hiSwap
if (SortDir = "ASC") then
while FormatCompare(vec(hiSwap,SortField),pivot(SortField)) > FormatCompare(pivot(SortField),vec(hiSwap,SortField))
hiSwap = hiSwap - 1
wend
else
while FormatCompare(vec(hiSwap,SortField),pivot(SortField)) < FormatCompare(pivot(SortField),vec(hiSwap,SortField))
hiSwap = hiSwap - 1
wend
end if
'== Swap values if loSwap is less then hiSwap
if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)
loop while loSwap < hiSwap
For counter = 0 to Ubound(vec,2)
vec(loBound,counter) = vec(hiSwap,counter)
vec(hiSwap,counter) = pivot(counter)
Next
'== 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,SortField,SortDir)
'== 2 or more items in second section
if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField,SortDir)
end if
End Sub 'QuickSort
Sub SwapRows(ary,row1,row2)
'==------------------------------------------==
'== This proc swaps two rows of an array ==
'==------------------------------------------==
Dim x,tempvar
For x = 0 to Ubound(ary,2)
tempvar = ary(row1,x)
ary(row1,x) = ary(row2,x)
ary(row2,x) = tempvar
Next
End Sub 'SwapRows
function FormatCompare(sOne,sTwo)
'==------------------------------------------==
'== Checks sOne & sTwo, returns sOne as a ==
'== Numeric if both pass isNumeric, if not ==
'== returns sOne as a string. ==
'==------------------------------------------==
if (isNumeric(Trim(sOne)) AND isNumeric(Trim(sTwo))) then
FormatCompare = CDbl(Trim(sOne))
else
FormatCompare = Trim(sOne)
end if
end function
Sub PrintArray(vec,loRow,hiRow,markCol)
'==------------------------------------------==
'== Print out an array Highlight the column ==
'== whose number matches param markCol ==
'==------------------------------------------==
Response.Write "<table border>"
Dim ColNmbr,RowNmbr
For RowNmbr = loRow to hiRow
Response.Write "<tr>"
For ColNmbr = 0 to (Ubound(vec,2) - 1)
If ColNmbr = markCol then
Response.Write "<td bgcolor=""FFFFCC"">"
Else
Response.Write "<td>"
End If
Response.Write vec(RowNmbr,ColNmbr) & "</td>"
Next
Response.Write "<td>" & vec(RowNmbr,ColNmbr) & "</td>"
Response.Write "</tr>"
Next
Response.Write "</table>"
End Sub 'PrintArray
Dim a(4,1)
a(0,0) = 10.0
a(1,0) = 3.0
a(2,0) = 10.0
a(3,0) = 3.50
a(4,0) = 10.0
a(0,1) = 1
a(1,1) = 2
a(2,1) = 3
a(3,1) = 4
a(4,1) = 5
Response.Write "Before sort:"
call PrintArray(a, 0, 4, 3.50)
call QuickSort(a, 0, 4, 0, "ASC")
Response.Write "After sort:"
call PrintArray(a, 0, 4, 3.50)
%>