Discontinuous sort function

In my application I need to sort out a rather large array, which, as it turns out, is a standard problem, for example std::sort.

Being located in a GUI application, I would like to give some kind of answer to the sorting progress. My first attempt is to figure out the approximate number of comparisons needed ( n*log2(n)for std::sort), and then just count them in the comparison function passed to std::sort. This works quite well.

The sorting algorithm runs in a separate thread to support a graphical interface. It associates its progress with a graphical interface using Qt signals or some similar thread protection mechanism.

However, I would also like the sort operation to abort. That is, the user is provided with a button or something similar to interrupt the entire operation. At the moment, I see only two options:

  • Proactive thread termination ( pthread_canceletc.)
  • rewrite the sorting algorithm and insert explicit undo points.

Given the cancellation of the stream, as a last resort and refusing to rewrite standard library algorithms, I hold the wolf by the ears.

+4
source share
1 answer

Ask the comparison function to check the atomic flag and throw an exception if the flag is set. The sorting stream should catch the exception and exit cleanly. The GUI thread then just needs to set the flag.

+7
source

Source: https://habr.com/ru/post/1666096/


All Articles