Transform and Conquer, presorting
Transform and Conquer, presorting
Transform-and-conquer
Transform-and-conquer method works as two-stage procedure. First, in the transformation
stage, the problem’s instance is modified to be more amenable to solution. Then, in the
second or conquering stage, it is solved.
Presorting
Presorting is a technique where lists are sorted before performing further operations on them.
This can simplify many problems and improve the efficiency of certain algorithms.
Many questions about a list are easier to answer if the list is sorted. The efficiency of
algorithms that involve sorting depends on the sorting algorithm used. Sorting algorithms can
be categorized into elementary algorithms (like selection sort, bubble sort, and insertion sort)
which are quadratic (i.e, O(n2)) in the worst and average cases, and advanced algorithms (like
mergesort and quicksort) which have better efficiency O(n log n).
Examples of Presorting
Brute-force Solution is to compare each pair of elements until duplicates are found or all
pairs are checked. Worst-case efficiency in this case is Θ(n2).
Presorting Solution is to sort the array first, then check consecutive elements for duplicates.
Efficiency in this case is Θ(nlogn) (due to sorting) + Θ(n) (for checking) =Θ(nlogn).
ALGORITHM PresortElementUniqueness(A[0..n − 1])
sort the array A
for i ← 0 to n − 2 do
if A[i] = A[i + 1] return false
return true
Presorting Solution is to sort the array first, then find the longest run of adjacent equal
values.
Efficiency in this case is Θ(nlogn) (due to sorting) + Θ(n) (for finding mode) =Θ(nlogn).
Presorting Solution is to sort the array first, then apply binary search.
Efficiency is Θ(nlogn) (due to sorting) + Θ(logn) (for binary search) = Θ(nlogn).
Presorting is less efficient than sequential search for a single search but is justified if multiple
searches are performed.
Applications of Presorting
1. Geometric Algorithms: Sorting points by coordinates or other criteria simplifies
problems like the closest-pair and convex-hull problems.
2. Directed Acyclic Graphs (DAGs): Topological sorting can simplify finding longest
and shortest paths.
3. Greedy Algorithms: Often require presorting of inputs as part of their operations.