c++ - What is the difference between inbuilt qsort function and stable sort function? -
from various sources cited know in-built c function, stable_sort stable qsort unstable. if case why use qsort @ all? isn't redundant? why not use stable_sort instead?
the reason choose quick sort on stable sort speed: qsort
faster stable_sort
, should come no surprise, because stable_sort
comes stronger guarantee.
o(n·log2(n)). if additional memory available, complexity o(n·log(n)).
space consideration: qsort
done in place, meaning no additional memory allocation required. stable_sort
, on other hand, tries temporary allocation of size equal array being sorted.
this function attempts allocate temporary buffer equal in size sequence sorted. if allocation fails, less efficient algorithm chosen.
note rcgldr's comment: (the hp / microsoft implementation of std::stable_sort
uses temporary buffer 1/2 size of sequence. second half sorted second half of sequence, first half temporary buffer, temporary buffer , second half of sequence merged sequence.
Comments
Post a Comment