Difference / order nlog(n) VS. n^2 algorithm.

Posted on 1998-09-19
Last Modified: 2006-11-17
What is the difference between an order nlog(n) and n^2 algorithm.  Please give a detailed explanation and examples of each in VB5.  I believe this relates to asymtotic boundaries and sorting heaps and such, but need a definitive answer that an MIS dude can understand.  Email to me if you have the time:

Question by:jwhowell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 1435718
Private Sub Form_Load()
MsgBox 10 ^ 2   '>> it is like 10 times 10
MsgBox 10 * Log(10) ' >> it is like 10 times log(base 10)of 10
End Sub


Author Comment

ID: 1435719

Thank you for the math lesson on natural logrithms, but I was hoping for a a bit more.  This relates to sorting algorithms and I need examples of each equation where I can generate a large random array or something and time the sorting of each algorithm based on the volume of the sort.  Do you have any code for timing sorting like I describe?


Accepted Solution

steve06 earned 110 total points
ID: 1435720
The "speed" of a sort algorythm is measured based on the number of operations to be done, compared to the number of elements to sort.

For example, the simple "bubble sort" is of order n^2, because the number of times you go through the loop and the test increases by the square of the number of values to sort:

For i = 1 to NbElement - 1
    For j = i to NbElement
        if strArray(i) > strArray(j) then
            strTemp = strArray(i)
            strArray(i) = strArray(j)
            strArray(j) = strTemp
        end if
    Next j
Next i

If NbElement is 100, you need about 5,000 iterations.
If NbElements is 1,000, you need about 500,000 iterations, 100 times more while the number of elements is only 10 times greater.

There is one sort algorithm which is of order n*ln(n): it is called QuickSort. Here is the routine:

Sub QuickSort_VB(iLow As Long, iHigh As Long)
                            ' QuickSort works by picking a random "pivot" element in lInput, then
                            ' moving every element that is bigger to one side of the pivot, and every
                            ' element that is smaller to the other side.  QuickSort is then called
                            ' recursively with the two subdivisions created by the pivot.  Once the
                            ' number of elements in a subdivision reaches two, the recursive calls end
                            ' and the array is sorted.
   Dim i As Long
   Dim j As Long
   Dim iRand As Long
   Dim lPartition As Long
   Dim lTemp As Long
   If iLow < iHigh Then

                            ' Only two elements in this subdivision; swap them if they are out of
                            ' order, then end recursive calls:
      If iHigh - iLow = 1 Then
         If bt(iLow) > bt(iHigh) Then
            lTemp = bt(iLow)
            bt(iLow) = bt(iHigh)
            bt(iHigh) = lTemp
         End If

                            ' Pick a pivot element at random, then move it to the end:
         iRand = iLow + Int(Rnd * (iHigh - iLow + 1))
         lTemp = bt(iHigh)
         bt(iHigh) = bt(iRand)
         bt(iRand) = lTemp
         lPartition = bt(iHigh)

                            ' Move in from both sides towards the pivot element:
            i = iLow
            j = iHigh
            Do While (i < j) And (bt(i) <= lPartition)
               i = i + 1
            Do While (j > i) And (bt(j) >= lPartition)
               j = j - 1

                            ' If we haven't reached the pivot element, it means that two
                            ' elements on either side are out of order, so swap them:
            If i < j Then
               lTemp = bt(i)
               bt(i) = bt(j)
               bt(j) = lTemp
            End If
         Loop While i < j

                            ' Move the pivot element back to its proper place in the array:
         lTemp = bt(i)
         bt(i) = bt(iHigh)
         bt(iHigh) = lTemp

                            ' Recursively call the QuickSort procedure (pass the smaller
                            ' subdivision first to use less stack space):
         If (i - iLow) < (iHigh - i) Then
            QuickSort_VB iLow, i - 1
            QuickSort_VB i + 1, iHigh
            QuickSort_VB i + 1, iHigh
            QuickSort_VB iLow, i - 1
         End If
      End If
   End If
End Sub

The procedure above sorts the array bt().

I think some mathematicians have demonstrated that this sort is much faster, although it is impossible to exactly predict hw much time it will take to sort an array. It depends on the "disorder' of the initial array.

Hope this help,

Author Comment

ID: 1435721

You are the man!  Thanks a bunch.  I may want to Email you about the small comparison app I'll build using your algorithms.  Could you send me your Email address?  I also ordered a book called 'Ready-To-Run Visual Basic Algorithms.'  Do you know this book?

Here are the points!

Jason W. Howell

Expert Comment

ID: 1435722

I don't know the book you mention. May be it contains sort algorithms as well.

I am happy that I could answer your question. My e-mail is


Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Script Host failed (Access is denied.) error 6 480
Add and format columns in vb6 7 85
Dinamic report to Crosstab query 9 60
Assign a value in Excel V-Basic 3 38
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

752 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