Posted By

elightbo on 11/03/06


Tagged

sort array multi-dimensional bubblesort


Versions (?)

ASP sort multi-dimensional array


 / Published in: ASP
 

'==--------------------------------------------------------== '== Sort a 2 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 (2nd dimension value) == '== loBound and hiBound are simply the upper and lower == '== bounds of the array's 1st dimension. It's probably == '== easiest to use the LBound and UBound functions to == '== set these. == '==--------------------------------------------------------==

  1. Sub QuickSort(vec,loBound,hiBound,SortField)
  2.  
  3. '==--------------------------------------------------------==
  4. '== Sort a 2 dimensional array on SortField ==
  5. '== ==
  6. '== This procedure is adapted from the algorithm given in: ==
  7. '== ~ Data Abstractions & Structures using C++ by ~ ==
  8. '== ~ Mark Headington and David Riley, pg. 586 ~ ==
  9. '== Quicksort is the fastest array sorting routine for ==
  10. '== unordered arrays. Its big O is n log n ==
  11. '== ==
  12. '== Parameters: ==
  13. '== vec - array to be sorted ==
  14. '== SortField - The field to sort on (2nd dimension value) ==
  15. '== loBound and hiBound are simply the upper and lower ==
  16. '== bounds of the array's 1st dimension. It's probably ==
  17. '== easiest to use the LBound and UBound functions to ==
  18. '== set these. ==
  19. '==--------------------------------------------------------==
  20.  
  21. Dim pivot(),loSwap,hiSwap,temp,counter
  22. Redim pivot (Ubound(vec,2))
  23.  
  24. '== Two items to sort
  25. if hiBound - loBound = 1 then
  26. if vec(loBound,SortField) > vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound)
  27. End If
  28.  
  29. '== Three or more items to sort
  30.  
  31. For counter = 0 to Ubound(vec,2)
  32. pivot(counter) = vec(int((loBound + hiBound) / 2),counter)
  33. vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)
  34. vec(loBound,counter) = pivot(counter)
  35. Next
  36.  
  37. loSwap = loBound + 1
  38. hiSwap = hiBound
  39.  
  40. do
  41. '== Find the right loSwap
  42. while loSwap < hiSwap and vec(loSwap,SortField) <= pivot(SortField)
  43. loSwap = loSwap + 1
  44. wend
  45. '== Find the right hiSwap
  46. while vec(hiSwap,SortField) > pivot(SortField)
  47. hiSwap = hiSwap - 1
  48. wend
  49. '== Swap values if loSwap is less then hiSwap
  50. if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)
  51.  
  52.  
  53. loop while loSwap < hiSwap
  54.  
  55. For counter = 0 to Ubound(vec,2)
  56. vec(loBound,counter) = vec(hiSwap,counter)
  57. vec(hiSwap,counter) = pivot(counter)
  58. Next
  59.  
  60. '== Recursively call function .. the beauty of Quicksort
  61. '== 2 or more items in first section
  62. if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField)
  63. '== 2 or more items in second section
  64. if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField)
  65.  
  66. End Sub 'QuickSort
  67. %>

Report this snippet  

You need to login to post a comment.