Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting an array

Status
Not open for further replies.

MikeT

IS-IT--Management
Feb 1, 2001
376
US
I have an array of 36 numbers, which I need sorted. The only problem is, the index is related to its value; i.e. the 16th element's value is useless unless I know that its the *16th's* element's value. If I did a traditional sort, the values and their indices would become all jumbled up.
What I've figured out so far is that the sorted array will be two-dimensional (at least I think). So, if in the original array, the 16th indice had the highest value, the first indice (0,0)of the new two dimensional array would be 16, and the index of (0,1) would have the value of whatever it was in the original array.
It makes sense if you think about it...

Any Ideas?
 
You do not need two dimensions. You need two parallel arrays. One carries keys, the other related data.
Bubble sort works ok for small volume. At 100 items you perform 5500 compares.
Worst case is roughly
N*(N+1)/2 where N = number of items-1

Code:
Dim myIdx(35) as integer
Dim I as integer
For I = 0 to 35
    myIdx(I) = I
Next
MySort myaray(),myIdx()
''''''''''''''''''''''''''''''''''''
Sub MySort (ary() as integer, aryIdx() as integer)  
    Dim I as Integer
    Dim J as Integer
    Dim strHold as string
    Dim intHold as integer
    blnOutSeq = True
    
    For J = UBound(ary) to 0 Step -1
        ' On each pass the largest of the remaining
        ' items, "bubbles" to its position
        blnOutSeq = false          ' Early detect in-seq
        For I = LBound+1 To J
            DO                          ' Do is code block container only
                If Ary(I-1) <= Ary(I) then exit Do                                 
                strInt = Ary(I-1)    ' Swap places
                Ary(I-1) = Ary(I)
                Ary(I) = intHold
                intHold = aryIdx(I-1)   ' Swap places
                aryIdx(I-1) = aryIdx(I)
                aryIdx(I) = intHold
                blnOutSeq = true
            Exit Do: Loop
        Next
        if blnOutSeq = false then exit for ' Early exit if in seq
    Next
End Sub
 
Hmmmmmmmmmmmmmm,

Not to dispute the assertion, but just provide the answere to the question as asked:

Code:
Public Function basSortW_Index(ArayIn As Variant) As Variant

    'Returns a two dimensional array from a one dimensional array.
    'The Added Index is the original Position in the input array
    Dim NewArray() As Variant
    Dim MyBound As Long
    MyBound = UBound(ArayIn)

    ReDim NewArray(MyBound, 2)

    'Just capture the original Index/position
    For Idx = 0 To UBound(NewArray, 1)
        NewArray(Idx, 1) = ArayIn(Idx)
        NewArray(Idx, 2) = Idx
    Next Idx

    'Sort the Thinggy on the oth Element
    Do While Not Sorted
        Sorted = True       'Assume it arrives properly sorted
        For Idx = 0 To UBound(NewArray, 1) - 1
            If (NewArray(Idx, 1) > NewArray(Idx + 1, 1)) Then
                tmp = NewArray(Idx, 1)
                tmpX = NewArray(Idx, 2)
                NewArray(Idx, 1) = NewArray(Idx + 1, 1)
                NewArray(Idx, 2) = NewArray(Idx + 1, 2)
                NewArray(Idx + 1, 1) = tmp
                NewArray(Idx + 1, 2) = tmpX
                Sorted = False
            End If
        Next Idx
    Loop

    basSortW_Index = NewArray

End Function
Public Function basTestSortW_Index()

    Dim MyVals(36) As Single
    Dim SortVals() As Variant

    For Idx = 0 To UBound(MyVals)
        MyVals(Idx) = Rnd() * 100
    Next Idx

    SortVals = basSortW_Index(MyVals)

    For Idx = 0 To UBound(SortVals, 1)
        Debug.Print SortVals(Idx, 1), SortVals(Idx, 2)
    Next Idx

    Debug.Print

End Function
[/code
 MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Hello, all.

Like to make a couple of notes.

[1] It seems to me that if you start with a one-dimensional &quot;numerical&quot; array, rather than &quot;functional&quot;, as given and attempt to sort it by classical methods, then, you are not going to solve the problem. Nothing wrong though with sorting method. The problem is rather the definition of the problem on hand. The above proposed solutions, it seems to me, fall short of taking this aspect into account.

[2] I've taken a quick look into the scripts above. I like to correct a mistake so as those read may not be frustrated. In John Yangling's script, a line within the Do Loop, namely :

Ary(I) = intHold _typo_

should be read as :

Ary(I) = strInt

As to the definition of the problem, I am thinking of posting in another followup message.

regards - tsuji

 
Okay, I appreciate all you guys' time on this. I thought I'd post the solution I'm using-
I started w/ a two dimensional array, like this:
myArray(1,0)= the number itself (the key)
myArray(1,1)= the data number
Then, I sorted it using this simple (but inefficient) code:
action=true
do while action=true
action=false
for x=1 to 35
if totals(x,1)>totals(x+1,1) then
temp1=totals(x+1,0)
temp2=totals(x+1,1)
totals(x+1,0)=totals(x,0)
totals(x+1,1)=totals(x,1)
totals(x,0)=temp1
totals(x,1)=temp2
action=true
end if
next
loop
--
This is all vbscript code in an .asp web page. The page loads in the blink of an eye, so the inefficiency is irrelevant. It's simple, compact, and does the job.
Your posts were very informative, and I learned from them.
If you have comments or suggestions w/ my solution, please share them.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top