advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
Tip of the Day
Rate this item | 0 users have rated this item.
Tip formerly from VB2TheMax
Expertise: Advanced
Language: VB5, VB6
January 13, 2003
HeapSort - A compact routine that sorts data in place
' HeapSort.  A compact routine that sorts data in place (no extra memory needed)
'  and is guaranteed to run in O(N log N) time no matter how the input data is 
' arranged.  On the down side, it's slower than other N log N sorts (Quick and 
' Merge) and not stable (does not preserve the original order of records with 
' equal keys)  Works by organizing records into a "heap" data structure where 
' each node is larger than its two leaves, and then repeatedly selecting the 
' largest key from the root of the heap.  Two versions are given.  pHeapSortS 
' is an indirect (pointerized) version for strings,
'  which can be adapted to doubles by changing the declaration of A().  
' HeapSortL is a direct version for longs, which can be adapted to integers.
'
' Speed:  pHeapSortS sorts 500,000 random strings in 88 sec; sorts 100186 
' library call numbers in 16.2 sec; sorts 25479 dictionary words in 3.3 sec 
' (random order), 3.4 sec (presorted) or 3.2 sec (reverse sorted).  HeapSortL 
' sorts 500,000 random longs in 92 seconds.  Timed in Excel 2001 on an 800 mhz 
' PowerBook.
'
' Bottom line:  compact, adapts to any date type and guarantees N log N sorting 
' times, but not the fastest and not stable.

' Usage:  

Dim S1(L To R) As String
Dim P1(L To R) As Long
Dim L1(L To R) As Long
 
For I = L To R
    S1(I) = GetRandomString()
    P1(I) = I
    L1(I) = GetRandomLong()
Next I

pHeapSortS L, R, S1, P1
HeapSortL L, R, L1

' CODE:

Sub pHeapSortS(L As Long, R As Long, A() As String, P() As Long)
    Dim Node As Long
    Dim Last As Long
    Dim TMP As Long
    
    'build heap
    For Node = (R + L) \ 2 To L Step -1
        pHeapS Node, L, R, A, P
    Next Node

    'repeatedly select item at root of heap
    For Last = R To L + 1 Step -1
        'swap root item to final position
        TMP = P(L)
        P(L) = P(Last)
        P(Last) = TMP
        'get swapped item into right place in heap
        pHeapS L, L, Last - 1, A, P
    Next Last
End Sub

Sub pHeapS(ByVal Node As Long, L As Long, R As Long, A() As String, P() As Long)
    Dim LEAF As Long
    Dim TMP As Long

    Do
        LEAF = Node + Node - (L - 1)
        If LEAF > R Then Exit Sub
        'pick the larger leaf
        If LEAF < R Then If A(P(LEAF + 1)) > A(P(LEAF)) Then LEAF = LEAF + 1
        'if node > leaves, we're done
        If A(P(Node)) > A(P(LEAF)) Then Exit Sub
        'if not, swap larger leaf with node
        TMP = P(Node)
        P(Node) = P(LEAF)
        P(LEAF) = TMP
        'and move further into the heap
        Node = LEAF
    Loop
End Sub

Sub HeapSortL(L As Long, R As Long, A() As Long)
    Dim Node As Long
    Dim Last As Long
    Dim TMP AS Long
    
    For Node = (R + L) \ 2 To L Step -1
        HeapL Node, L, R, A
    Next Node
    For Last = R To L + 1 Step -1
    TMP = A(L)
    A(L) = A(Last)
    A(Last) = TMP
        HeapL L, L, Last - 1, A
    Next Last
End Sub

Sub HeapL(ByVal Node As Long, L As Long, R As Long, A() As Long)
    Dim LEAF As Long
    Dim TMP AS Long

    Do
        LEAF = Node + Node - (L - 1)
        If LEAF > R Then Exit Sub
        If LEAF < R Then If A(LEAF + 1) > A(LEAF) Then LEAF = LEAF + 1
        If A(Node) > A(LEAF) Then Exit Sub
        TMP = A(Node)
    A(Node) = A(LEAF)
    A(LEAF) = TMP
        Node = LEAF
    Loop
End Sub

David B. Ring
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES