Cartesian Tree - Application in Sorting

Application in Sorting

Levcopoulos & Petersson (1989) describe a sorting algorithm based on Cartesian trees. They describe the algorithm as based on a tree with the maximum at the root, but it may be modified straightforwardly to support a Cartesian tree with the convention that the minimum value is at the root. For consistency, it is this modified version of the algorithm that is described below.

The Levcopoulos–Petersson algorithm can be viewed as a version of selection sort or heap sort that maintains a priority queue of candidate minima, and that at each step finds and removes the minimum value in this queue, moving this value to the end of an output sequence. In their algorithm, the priority queue consists only of elements whose parent in the Cartesian tree has already been found and removed. Thus, the algorithm consists of the following steps:

  1. Construct a Cartesian tree for the input sequence
  2. Initialize a priority queue, initially containing only the tree root
  3. While the priority queue is non-empty:
    • Find and remove the minimum value x in the priority queue
    • Add x to the output sequence
    • Add the Cartesian tree children of x to the priority queue

As Levcopoulos and Petersson show, for input sequences that are already nearly sorted, the size of the priority queue will remain small, allowing this method to take advantage of the nearly-sorted input and run more quickly. Specifically, the worst-case running time of this algorithm is O(n log k), where k is the average, over all values x in the sequence, of the number of consecutive pairs of sequence values that bracket x. They also prove a lower bound stating that, for any n and k = ω(1), any comparison-based sorting algorithm must use Ω(n log k) comparisons for some inputs.

Read more about this topic:  Cartesian Tree

Famous quotes containing the word application:

    There are very few things impossible in themselves; and we do not want means to conquer difficulties so much as application and resolution in the use of means.
    François, Duc De La Rochefoucauld (1613–1680)