0% found this document useful (0 votes)
35 views42 pages

5 - Divide and Conquer

The document discusses the divide-and-conquer paradigm, which involves breaking down complex problems into simpler subproblems, solving them recursively, and combining their solutions. It highlights key algorithms that utilize this approach, including Binary Search, Quick Sort, and Merge Sort, along with their advantages such as improved algorithm efficiency and parallelism. Additionally, it provides insights into the mechanics of Quick Sort and Merge Sort, including examples and pseudocode.

Uploaded by

klavidaesbella56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views42 pages

5 - Divide and Conquer

The document discusses the divide-and-conquer paradigm, which involves breaking down complex problems into simpler subproblems, solving them recursively, and combining their solutions. It highlights key algorithms that utilize this approach, including Binary Search, Quick Sort, and Merge Sort, along with their advantages such as improved algorithm efficiency and parallelism. Additionally, it provides insights into the mechanics of Quick Sort and Merge Sort, including examples and pseudocode.

Uploaded by

klavidaesbella56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

DIVIDE & 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.

• Its basic idea is to decompose a given problem into two or more


similar, but simpler, subproblems, to solve them in turn, and to
compose their solutions to solve the given problem.

• Problems of sufficient simplicity are solved directly.


Divide & Conquer
• This technique can be divided into the following three parts:
• Divide: This involves dividing the problem into some sub problem.
• Conquer: Sub problem by calling recursively until sub problem solved.
• Combine: The Sub problem Solved so that we will get find problem
solution.

• Also known as DANDC


• DANDC are mostly recursive
Examples
• The following are some standard algorithms that follows Divide and
Conquer algorithm.

• Binary Search is a searching algorithm. In each step, the algorithm


compares the input element x with the value of the middle element
in array. If the values match, return the index of the middle.
Otherwise, if x is less than the middle element, then the algorithm
recurs for left side of middle element, else recurs for the right side of
the middle element.
• Quicksort is a sorting algorithm. The algorithm picks a pivot element,
rearranges the array elements in such a way that all elements smaller
than the picked pivot element move to left side of pivot, and all
greater elements move to right side. Finally, the algorithm recursively
sorts the subarrays on left and right of pivot element.

• 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

• 54 will serve as our first pivot value


• The partition process will happen next. It will find the split point and
at the same time move other items to the appropriate side of the list,
either less than or greater than the pivot value.
Quick Sort - Example

• Partitioning begins by locating two position markers – leftmark and


rightmark – at the beginning and end of the remaining items in the
list ( positions 1 and 8 for the example)
• The goal of the partition process is to move items that are on the
wrong side with respect to the pivot value while also converging on
the split point.
Quick Sort - Example
We begin by incrementing leftmark until we locate a value that
is greater than the pivot value.

We then decrement rightmark until we find a value that is less


than the pivot value.

At this point we have discovered two items that are out of


place with respect to the eventual split point.

For our example, this occurs at 93 and 20.

Now we can exchange these two items and then repeat the
process again
Quick Sort - Example

At the point where rightmark becomes less than leftmark, we stop.

The position of rightmark is now the split point.


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

To analyze the quickSort function, note


that for a list of length n, if the partition
always occurs in the middle of the list,
there will again be logn divisions.

In order to find the split point, each of


the n items needs to be checked against
the pivot value. The result is nlogn
Merge Sort
• Merge sort is one of the most efficient sorting algorithms.

• It works on the principle of Divide and Conquer.

• 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

You might also like