Does anyone have a (royalty-free) Navision C/AL- code that performs a bubble sort or a quick sort on an array? Thanks! [:D]
Why not use Temporary tables instead of Arrays. After the “invention” of temporary tables I never had to use Arrays again
By the way, Bubblesort is one of the worst (with respect to runtime) sorting algorithms ever devised, with mean runtime O(NN) [B)] Use Heapsort, Quicksort or Mergesort (all with O(Nlog N)) instead. Be aware that Quicksort’s performance can degrade to O(NN), though, if not carefully implemented. PS: O(NN) means that the computational effort for the algorithm in question is proportional to the square of the dataset’s size, disregarding any constant factors or lower-order terms.
Thought I’d put my penny’s worth in. It depends how what your sorting is jumbled and the number of things to sort. The poor old bubble sort always gets much maligned in favour of more exotic sorting methods. But for sorting a dozen or so items it is actually better in terms of computer resources as there is no overhead compared to quicksort.
Is this what you are looking for? http://navisioner.com/html/jitsort.htm
Just a few quick notes, Quicksort doesnt degrade do NN if it is not carefully implemented, It degrades to NN when the data is already sorted. And Yes, Bubble Sorting doesnt “eat” memory, but it guarantees /which is good and bad, depends from the point of view ;)/ the slowest sorting of an array. If I had to implement some sorting in Navision I’ll stick to Temporary tables method or to take a look at the “jitsort” suggested by THaug.
quote:
Originally posted by koleto
Just a few quick notes, Quicksort doesnt degrade do NN if it is not carefully implemented, It degrades to NN when the data is already sorted.
Right, that’s why I wrote Quicksort’s performance can degrade if not carefully implemented. It degrades when the data is already sorted and no precautions have been taken for this case.
I would not use quicksort if I only had to sort a few items, but having said that a temp table would probably be more flexible except it uses up a table. By the way, how many vaiables does it take to swap two integers in C? Regards
Yup, the O(…) notation completely ignores any terms of lower magnitude or constant factors. There might be some overhead involved which makes a fast algorithm slower for a small number of input data - which happens to be the case for Quicksort. The answer is 2 - but I once read that this “trick” is not recommended for some reasons. I forgot why [;)]