Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8519027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:08:10+00:00 2026-06-11T06:08:10+00:00

What I have here works fine for data which is 10 row bytes x

  • 0

What I have here works fine for data which is 10 row bytes x 10 column bytes =100 elements for example.
But now I tried it on 256 row bytes x 256 column bytes = 65536 elements and it’s taking about 30 minutes to sort the rows in proper lexicographical order. Anyway to optimize this function so it could take maybe 5 seconds maximum to complete.

I know I have to use some other sorting algorithm but I cannot really figure out what to do.

Function SortArrayOfArraysLexicoGraphically(ByRef data() As Byte) As Byte()
Dim lexicoGraphicalIndexes() As Byte

Dim dataSize As Long
dataSize = UBound(data) + 1
Dim squareRootMinusOne As Integer
Dim squareRoot As Integer
squareRoot = Sqr(dataSize)
squareRootMinusOne = squareRoot - 1

ReDim lexicoGraphicalIndexes(squareRootMinusOne)

Dim columnStart As Long
Dim row As Long
Dim column As Long
Dim rowSwapped As Boolean

For columnStart = 0 To UBound(lexicoGraphicalIndexes)
    lexicoGraphicalIndexes(columnStart) = columnStart
Next columnStart

'start column from the last element from the row and go backwards to first element in that row.
For columnStart = squareRootMinusOne To 0 Step -1
    Do
        rowSwapped = False
        Do
             If data((row * squareRoot) + columnStart) > data(((row + 1) * squareRoot) + columnStart) Then

                'Swaps a full row byte by byte.
                For column = 0 To squareRootMinusOne
                    Call SwapBytes(data, (row * squareRoot) + column, ((row + 1) * squareRoot) + column)
                Next column
                Call SwapBytes(lexicoGraphicalIndexes, row, row + 1)
                rowSwapped = True
            End If
            row = row + 1
        Loop Until row > squareRootMinusOne - 1
        row = 0
    Loop Until rowSwapped = False
Next columnStart

'returns a byte array of sorted indexes.
SortArrayOfArraysLexicoGraphically = lexicoGraphicalIndexes
End Function

Public Sub SwapBytes(data() As Byte, firstIndex As Long, secondIndex As Long)
    Dim tmpFirstByte As Byte
    tmpFirstByte = data(firstIndex)
    data(firstIndex) = data(secondIndex)
    data(secondIndex) = tmpFirstByte
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:08:12+00:00Added an answer on June 11, 2026 at 6:08 am

    The slow step in this is the copying, byte by byte, in a loop. I would take advantage of the RtlMoveMemory API call (often called CopyMemory). This does a block memory copy which is a lot faster. I also declare a module level array to act as the temporary buffer in the row swap. You could probably just merge the two procedures below, to make it self-contained:

    Private Declare Sub CopyMemory Lib "Kernel32.dll" Alias "RtlMoveMemory" (ByVal pDest As Long, ByVal pSrc As Long, ByVal nCount As Long)
    
    Private m_bytTemp() As Byte
    
    
    Function SortArrayOfArraysLexicoGraphically2(ByRef data() As Byte) As Byte()
    
        Dim lexicoGraphicalIndexes() As Byte
        Dim dataSize As Long
        Dim squareRootMinusOne As Integer
        Dim squareRoot As Integer
        Dim columnStart As Long
        Dim row As Long
        Dim column As Long
        Dim rowSwapped As Boolean
    
        dataSize = UBound(data) + 1
        squareRoot = Sqr(dataSize)
        ReDim m_bytTemp(1 To squareRoot)
        squareRootMinusOne = squareRoot - 1
        ReDim lexicoGraphicalIndexes(squareRootMinusOne)
    
        For columnStart = 0 To UBound(lexicoGraphicalIndexes)
            lexicoGraphicalIndexes(columnStart) = columnStart
        Next columnStart
    
        'start column from the last element from the row and go backwards to first element in that row.
        For columnStart = squareRootMinusOne To 0 Step -1
            Do
                rowSwapped = False
                Do
                    If data((row * squareRoot) + columnStart) > data(((row + 1) * squareRoot) + columnStart) Then
                        'Swaps a full row in a few copies.
                        SwapMultipleBytes data, (row * squareRoot), ((row + 1) * squareRoot), squareRoot
                        Call SwapBytes(lexicoGraphicalIndexes, row, row + 1)
                        rowSwapped = True
                    End If
                    row = row + 1
                Loop Until row > squareRootMinusOne - 1
                row = 0
            Loop Until rowSwapped = False
        Next columnStart
    
        'returns a byte array of sorted indexes.
        SortArrayOfArraysLexicoGraphically2 = lexicoGraphicalIndexes
    End Function
    
    Public Sub SwapMultipleBytes(ByRef data() As Byte, ByVal firstIndex As Long, ByVal secondIndex As Long, ByVal nCount As Long)
    
        CopyMemory VarPtr(m_bytTemp(1)), VarPtr(data(firstIndex)), nCount
        CopyMemory VarPtr(data(firstIndex)), VarPtr(data(secondIndex)), nCount
        CopyMemory VarPtr(data(secondIndex)), VarPtr(m_bytTemp(1)), nCount
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jQuery $.ajax call which works fine in all major browsers. Additionally
So i have an rldc, and a subreport which is just the same data
I have the following code which works: $(document).ready(function() { var xhr = false; func_two();
I've got a document based App using Core Data, and have a tableView which
I have a ListView which displays in every row an image pulled from the
i have a MySQL SELECT query which fetches data from 6 tables using Mysql
another weird problem with the iPhone SDK here. I have a UITableView which contains
I have a ComboBox in WPF which is databound, and has data template which
I have $('#structure').html(html); This works fine in FireFox. IE 8 doesn't display the html.
I have a SSIS package which reads an Excel File (Data Flow Source) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.