5 - Divide and Conquer
5 - Divide and Conquer
Overview
• Binary Tree Traversal
• Quick Sort
• Merge Sort
• Binary Search
Divide and Conquer
• The divide-and-conquer paradigm is often used to find an optimal
solution of a problem.
• Merge Sort is also a sorting algorithm. The algorithm divides the array
in two halves, recursively sorts them and finally merges the two
sorted halves.
Advantages
• Solving difficult problems
• a way of breaking the problem into sub-problems, of solving the trivial cases and of
combining sub-problems to the original problem.
• Algorithm efficiency
• helps in the discovery of efficient algorithms.
• Parallelism
• naturally adapted for execution in multi-processor machines, especially shared-
memory systems where the communication of data between processors does not
need to be planned in advance
• Memory access
• naturally tend to make efficient use of memory caches. The reason is that once a
sub-problem is small enough, it and all its sub-problems can, in principle, be solved
within the cache, without accessing the slower main memory
• Roundoff control
• In computations with rounded arithmetic, e.g. with floating-point numbers, a divide-
and-conquer algorithm may yield more accurate results than a superficially
equivalent iterative method.
Binary Search
Quick sort
• a Divide and Conquer algorithm.
• It picks an element as pivot and partitions the given array around the
picked pivot.
• There are many different versions of quickSort that pick pivot in
different ways.
• Always pick first element as pivot. (implemented below)
• Always pick last element as pivot
• Pick a random element as pivot.
• Pick median as pivot.
• A quick sort first selects a value, which is called the pivot value.
• Although there are many different ways to choose the pivot value, we
will simply use the first item in the list.
• The role of the pivot value is to assist with splitting the list.
• The actual position where the pivot value belongs in the final sorted
list, commonly called the split point, will be used to divide the list for
subsequent calls to the quick sort.
Quick Sort - Example
Now we can exchange these two items and then repeat the
process again
Quick Sort - Example
The pivot value can be exchanged with the contents of the split point and the pivot value is now in place
In addition, all the items to the left of the split point are less than the pivot value, and all the items to the right
of the split point are greater than the pivot value.
The list can now be divided at the split point and the quick sort can be invoked recursively on the two halves.
Pseudocode
Quick Sort analysis
• Merge sort repeatedly breaks down a list into several sublists until
each sublist consists of a single element and merging those sublists in
a manner that results into a sorted list.
Merge Sort
Merge sort - Example
Pseudocode – Merge Sort
Merge sort - Analysis