Array Sorting
Cameron
Posts: 56
Hi Guys,
Does anyone know the best way, with NetLinx, to sort an Array? For example;
Sort[] = {'aaa','adc','abb','aab','acc'} = {'aaa','aab','abb','acc','adc'}
OR
Sort[] = {1,5,3,4} = {1,3,4,5}
I could attempt to write it but my life would be easier if someone has already written it
Thanks,
C
Does anyone know the best way, with NetLinx, to sort an Array? For example;
Sort[] = {'aaa','adc','abb','aab','acc'} = {'aaa','aab','abb','acc','adc'}
OR
Sort[] = {1,5,3,4} = {1,3,4,5}
I could attempt to write it but my life would be easier if someone has already written it
Thanks,
C
0
Comments
An efficient sorting algorithm can solve the problem in N logN time and a simple one can solve it N^2.
Your nRunCount variable seems to says 'I'm reinventing the wheel. The square wheel'.
You can easily implement a selection sort or an insertion sort if you can accept a quadratic execution time, otherwise a merge sort or a quicksort.
Usually in netlinx we have small arrays (length << 1000), so one of the first two algorithms is enough and very very simple to implement
http://xkcd.com/1185/
Kim has handy functions on his github - array.axi has a quicksort algorithm you could adapt for multi-dimensional arrays:
https://github.com/KimBurgess/netlinx-common-libraries