sorting algos

Dubbsta

Active Member
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``````

got it.

Goodlookinguy

New Member
3rd Party Module Dev
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.

Dubbsta

Active Member
Thanks GLG I ended up getting selection sort and bubble sort to work last night. Thanks for your help!

Will study those notes ty.