Solved

How to do a reverse sort (descending) of filenames using Quicksort function

Posted on 2006-10-26
5
638 Views
Last Modified: 2008-03-03
I'm having a problem trying to do a reverse sort (descending order; Z to A; n to 0).
The Quicksort function (mentioned below) does a sort in ascending order (A to Z; 0 to n) and is an excerpt from a C++ book called "Data Abstractions & Structures using C++". I've used the following code to do a sort of filenames/folders in a customized Enhanced Browser Page for IIS 5.0 and WORKS!!! I've tried to manipulate variables within the function to do a reverse sort however I've had no luck....

My question:  How/What do I change the Quicksort function (execerpt below) to sort in descending order (Z to A; n to 0)?

'___________________________________________________________________
Sub QuickSort(vec,loBound,hiBound,SortField)
  '==--------------------------------------------------------==
  '== 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.                                           ==
  '==--------------------------------------------------------==

  Dim pivot(),loSwap,hiSwap,temp,counter
  Redim pivot (Ubound(vec,2))
  '== Two items to sort
  if hiBound - loBound = 1 then
    if vec(loBound,SortField) > vec(hiBound,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
  loSwap = loBound + 1
  hiSwap = hiBound
 
  Do
    '== Find the right loSwap
    while loSwap < hiSwap and vec(loSwap,SortField) <= pivot(SortField)
      loSwap = loSwap + 1
    wend
    '== Find the right hiSwap
    while vec(hiSwap,SortField) > pivot(SortField)
      hiSwap = hiSwap - 1
    wend
    '== 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)
    '== 2 or more items in second section
    if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField)

End Sub  'QuickSort
0
Comment
Question by:HCAAdmin
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:shinobun
ID: 17815778
The only thing you should need to change is the comparison part.  Let me see if I can remember some VB...
0
 
LVL 9

Expert Comment

by:shinobun
ID: 17815843
I think this will do the trick:

...
  '== Two items to sort
  if hiBound - loBound = 1 then
    if vec(loBound,SortField) <= vec(hiBound,SortField) then  ' reversed!
      Call SwapRows(vec,hiBound,loBound)
    End If
  End If
...
    '== Find the right loSwap
    while loSwap < hiSwap and vec(loSwap,SortField) > pivot(SortField) ' reversed!
      loSwap = loSwap + 1
    wend
    '== Find the right hiSwap
    while vec(hiSwap,SortField) <= pivot(SortField) ' reversed!
      hiSwap = hiSwap - 1
    wend
...
0
 

Author Comment

by:HCAAdmin
ID: 17821839
Thanks for the reply....

However it still doesn't seem to work. Perhaps I need to change something in the subroutine called "Swaprows"?
The following is the subroutine called within the Quicksort() function:

Parameters being passed into the subroutine:
SwapRows(vec,hiBound,loBound)
-------------------------------------------------------------
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
0
 
LVL 9

Accepted Solution

by:
shinobun earned 250 total points
ID: 17823866
SwapRows looks fine.

In addition to the changes I posted earlier, you need to restrict hiSwap from getting lower than the loBound:

    '== Find the right hiSwap
    While hiSwap > loBound And vec(hiSwap, SortField) <= pivot(SortField)
0
 

Author Comment

by:HCAAdmin
ID: 17833601
It works!!! Thank you very much! :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now