Solved

Sort the array

Posted on 2000-03-17
6
219 Views
Last Modified: 2010-05-02
Suppose I have a numerical array that contains 5 values
{45,23,100,54,12}. I want to sort the array in ascending order by using Bubble Sort. It should sort the array {12, 23, 45, 54, 100}
0
Comment
Question by:JeffBag
[X]
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
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2629701
This function will do that:

Sub NumericArraySort(InArray As Variant)
    'THIS FUNCTION SORTS AN NUMERIC ARRAY
    Dim lLoop1 As Long
    Dim lLoop2 As Long
    Dim tBuf As String
    For lLoop1 = UBound(InArray) To LBound(InArray) Step -1
        For lLoop2 = LBound(InArray) + 1 To lLoop1
            If InArray(lLoop2 - 1) > InArray(lLoop2) Then
                tBuf = InArray(lLoop2 - 1)
                InArray(lLoop2 - 1) = InArray(lLoop2)
                InArray(lLoop2) = tBuf
            End If
        Next lLoop2
    Next lLoop1
End Sub




Call it like this:

Dim x(5) As Integer
x(0) = 11
x(1) = 1
x(2) = 15
x(3) = 120
x(4) = 10
x(5) = 4
NumericArraySort x
For i = 0 To 5
    Debug.Print x(i)
Next i


Cheers!®©
0
 
LVL 9

Accepted Solution

by:
Ruchi earned 145 total points
ID: 2629706
'I posted the comments to the similar question a few weeks ago....

'Put a command button on the form.

Option Explicit
Dim marray(5) As Integer

Public Sub BubbleSort(theArray() As Integer)
   Dim pass As Integer, compare As Integer
   Dim hold As String

   For pass = 1 To (UBound(theArray) - 1)
       
      For compare = 1 To (UBound(theArray) - 1)
           
         If theArray(compare) > theArray(compare + 1) Then
            hold = theArray(compare)
            theArray(compare) = theArray(compare + 1)
            theArray(compare + 1) = hold
         End If

      Next compare

   Next pass
     
End Sub

Private Sub Command1_Click()
    Dim x As Integer
     
    Call BubbleSort(marray) ' Sort the array

    For x = 1 To UBound(marray)
        MsgBox marray(x)
    Next x
End Sub

Private Sub Form_Load()
    marray(1) = 45
    marray(2) = 23
    marray(3) = 100
    marray(4) = 54
    marray(5) = 12
End Sub

0
 

Author Comment

by:JeffBag
ID: 2629893
I am confused with Mcrider's answer. I took a note from my professor. His method for Bubble Sort is.

   If Arr(X) > Arr(X + 1) Then
         Temp = Arr(X)
          Arr(X) = Arr(X + 1)
          Arr(X + 1) = Temp
   End If

I have to reject Mcrider's answer.

I ran Ruchi's code and it worked perfectly.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:mcrider
ID: 2629937
I beg to differ... Please read the following KB article from microsoft.  The bublesort code is THE SAME code I provided... THE ONLY DIFFERENCE IS THE VARIABLE NAMES!


SAMPLE: Sorting Algorithms for Numeric Arrays

--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0

--------------------------------------------------------------------------------


SUMMARY
Sorted.exe is a sample that demonstrates how to sort algorithms for numeric arrays. This article describes the three methods of sorting a series of numbers in a code and provides sample code showing how to implement them.

The following file is available for download from the Microsoft Software Library:

~ Sorted.exe

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:


Q119591 : How to Obtain Microsoft Support Files from Online Services





MORE INFORMATION
It is often necessary to sort a series of numbers in code and there are various sorting algorithms available to do this.

The three methods discussed are:


Bubble sort.


Selection sort.


Shell sort.



Each routine simply receives an array full of numbers within the bounds of a LONG datatype, although this could be easily changed for different numeric datatypes. Note that the sort routines return the result of the sort operation in the array passed to the routine. Therefore, if you do not want the original array to be modified, copy the array to a variant, as shown in the example below, and then pass the variant to the relevant procedure.


Step-by-Step Example
Start a new project. Form1 is created by default.


Place a Command Button on the form.


Add the following code to the Form1 code window:
   Option Explicit

   sub Command1_Click()
     Dim lMyArray(0 TO 9) As Long
     Dim vTemp1 As Variant
     Dim vTemp2 As Variant
     Dim vTemp3 As Variant
     Dim iLoop As Integer
     Randomize
     For iLoop = LBound(lMyArray) To UBound(lMyArray)
       lMyArray(iLoop) = Int(Rnd * 100) + 1
     Next iLoop
     vTemp1 = lMyArray
     vTemp2 = lMyArray
     vTemp3 = lMyArray
     Call BubbleSortNumbers(vTemp1)
     Call SelectionSortNumbers(vTemp2)
     Call ShellSortNumbers(vTemp3)
   end sub

   Sub BubbleSortNumbers(iArray As Variant)
     Dim lLoop1 As Long
     Dim lLoop2 As Long
     Dim lTemp As Long
     For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
       For lLoop2 = LBound(iArray) + 1 To lLoop1
         If iArray(lLoop2 - 1) > iArray(lLoop2) Then
           lTemp = iArray(lLoop2 - 1)
           iArray(lLoop2 - 1) = iArray(lLoop2)
           iArray(lLoop2) = lTemp
         End If
       Next lLoop2
     Next lLoop1
   End Sub

   Sub SelectionSortNumbers(vArray As Variant)
     Dim lLoop1 As Long
     Dim lLoop2 As Long
     Dim lMin As Long
     Dim lTemp As Long
     For lLoop1 = LBound(vArray) To UBound(vArray) - 1
       lMin = lLoop1
         For lLoop2 = lLoop1 + 1 To UBound(vArray)
           If vArray(lLoop2) < vArray(lMin) Then lMin = lLoop2
         Next lLoop2
         lTemp = vArray(lMin)
         vArray(lMin) = vArray(lLoop1)
         vArray(lLoop1) = lTemp
     Next lLoop1
   End Sub

   Sub ShellSortNumbers(vArray As Variant)
     Dim lLoop1 As Long
     Dim lHold As Long
     Dim lHValue As Long
     Dim lTemp As Long
     lHValue = LBound(vArray)
     Do
       lHValue = 3 * lHValue + 1
     Loop Until lHValue > UBound(vArray)
     Do
       lHValue = lHValue / 3
       For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
         lTemp = vArray(lLoop1)
         lHold = lLoop1
         Do While vArray(lHold - lHValue) > lTemp
           vArray(lHold) = vArray(lHold - lHValue)
           lHold = lHold - lHValue
           If lHold < lHValue Then Exit Do
         Loop
           vArray(lHold) = lTemp
       Next lLoop1
     Loop Until lHValue = LBound(vArray)
   End Sub
 




Additional query words: 4.00 kbDSupport

Keywords          : kbsample kbVBp400 VB4WIN
Version           : 4.00 | 4.00
Platform          : NT WINDOWS
Issue type        :
 


Last Reviewed: June 8, 1999
) 1999 Microsoft Corporation. All rights reserved. Terms of Use.



0
 

Author Comment

by:JeffBag
ID: 2629979
I read the KB article and now I understand. How do I award you the 60 points?
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2630053
The only way to do so is to open a new question with the title "FOR MCRIDER ONLY" and apply the points there...
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month6 days, 21 hours left to enroll

623 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