• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

sorting algos

Dubbsta

Active member
Joined
Jul 13, 2017
Messages
208
would someone be kind enough to write some sorting algos? mainly selection and merge

i copied this from the web and it looks right on but getting bad results. i tried tracing it in my head but if i try one more time i think my head might explode.

Code:
For Local i:Int = 0 Until arr.Length()-1

Local min:Int = i

For Local j:Int = 1 Until arr.Length()



If arr[j] < arr[min]

min = j

End

Local temp:Int = arr[i]

arr[i] = arr[min]

arr[min] = temp

DrawText(arr[i],20,20+i*10)

Next



next
 
I implemented Bubble, Heap, Insertion, Merge, Quick, and Shell sorting methods here: https://bitbucket.org/Goodlookinguy/xaddon/src/default/basic/sorting/

You'll have to define a comparator as outlined by IComparator<T>

Code:
Import xaddon.basic.sorting

Class IntComparator Implements IComparator<Int>
    Method Compare:Int( o1:Int, o2:Int )
        If o1 < o2 Then Return -1
        Return o1 > o2
    End
   
    Method ValuesEqual:Bool( o1:Int, o2:Int )
        Return o1 = o2
    End
   
    Function Instance:IntComparator()
        If g_Instance = Null Then g_Instance = New IntComparator()
        Return g_Instance
    End
   
    Global g_Instance:IntComparator
End

Function Main:Int()
    Local sorter:ArraySorter<Int> = New ArrayMergeSort<Int>()
    Local arrOfValues:Int[] = [4, 1, 3, 4, 9, 2, -9, 5]
    sorter.Sort(arrOfValues, IntComparator.Instance())
    For Local i:Int = 0 Until arrOfValues.Length
        Print arrOfValues[i]
    Next
    Return 0
End

Note: I haven't tested this code, but assuming I didn't type anything wrong, it should work.
Note: It's still in Monkey, the files will have to be converted to cbx.
 
Thanks GLG I ended up getting selection sort and bubble sort to work last night. Thanks for your help!

Will study those notes ty.
 
Back
Top Bottom