2015 Sep 02 12:58 PM
Hello,
I have searched if someone posted this question before, but I couldn't find it:
How does the SORT instruction actually sort the table that is passed? Which is the sorting algorithm used?
And: Why is it an instruction?
Just curious...
Thanks a lot!,
Eloi
2015 Sep 02 4:06 PM
The documentation does not mention it but as far as I know the SORT command is implemented using quick sort. This also explains why the sort is not stable.
Since ABAP is a 4th generation language, it would be pretty weird to not have a SORT command. And an implementation in the kernel will always be faster than anything built in ABAP. Why you need it in your program is a whole different story, I see it misused a lot where the sorting could have been done during SELECT or the sort is only done to remove duplicates that could have been avoided with some smarter SELECTs.
Hello,
I have searched if someone posted this question before, but I couldn't find it:
How does the SORT instruction actually sort the table that is passed? Which is the sorting algorithm used?
And: Why is it an instruction?
Just curious...
Thanks a lot!,
Eloi
2015 Sep 02 1:11 PM
hi,
for sorting we follow the linear search,binary search and hash based search algorithms. These algorithms are depend on type of internal table.
2015 Sep 02 4:06 PM
The documentation does not mention it but as far as I know the SORT command is implemented using quick sort. This also explains why the sort is not stable.
Since ABAP is a 4th generation language, it would be pretty weird to not have a SORT command. And an implementation in the kernel will always be faster than anything built in ABAP. Why you need it in your program is a whole different story, I see it misused a lot where the sorting could have been done during SELECT or the sort is only done to remove duplicates that could have been avoided with some smarter SELECTs.
2015 Sep 03 11:14 AM
SORT command allows to use [STABLE] clause therefore it has to change, in that case, its sorting method.
It's interesting what you said about sorting misuse. So far I have read through the forums to be a good practice to assign the effort of sorting to the application server rather than the database one. No?
Thanks for your reply Gerrit.
2015 Sep 03 11:43 AM
As per me Sorting algorithm varies based on system to system and different parameters.
E.G :
1. Application server type : Linux/Unix/Windows/Sybase/Solaris.
2. System type : 32 bit/64 bit
3. Unicode system : Yes/No
4. Type of internal table : Hashed/Sorted/Standard
Check this note 193529 also
193529 - Sort as of 4.5A/EXSORT_NOT_ENOUGH_MEMORY
They use a term called "Disk sort" which is more closer to Merge Sort we have in C, when the data becomes higher than a certain limit and in some cases they use quick sort.
Also I agree in a typical 3 tier environment it is recommended to do the sorting in application server rather in database level but is about to change with SAP HANA.
R
2016 Feb 20 10:23 AM