0% found this document useful (0 votes)
22 views6 pages

Divide and Conquer Technique

The document describes the divide and conquer approach, which involves breaking down a problem into sub-problems, solving them recursively, and combining their solutions. It focuses on two sorting algorithms: Merge Sort, which is stable and has a guaranteed worst-case time complexity of O(n log n), and Quick Sort, which is efficient but can have poor performance in specific cases. The document also outlines the advantages and disadvantages of both sorting algorithms, highlighting their applications in various fields.
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)
22 views6 pages

Divide and Conquer Technique

The document describes the divide and conquer approach, which involves breaking down a problem into sub-problems, solving them recursively, and combining their solutions. It focuses on two sorting algorithms: Merge Sort, which is stable and has a guaranteed worst-case time complexity of O(n log n), and Quick Sort, which is efficient but can have poor performance in specific cases. The document also outlines the advantages and disadvantages of both sorting algorithms, highlighting their applications in various fields.
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/ 6

Divide and Conquer

Divide and conquer approach breaks down a problem into multiple sub-problems recursively
until it cannot be divided further. These sub-problems are solved first and the solutions are
merged together to form the final solution.
The common procedure for the divide and conquer design technique is as follows −
• Divide − we divide the original problem into multiple sub-problems until they cannot
be divided further.
• Conquer − Then these sub problems are solved separately with the help of recursion
• Combine − Once solved, all the sub problems are merged/combined together to
form the final solution of the original problem.
Sorting Algorithms
Sorting is the process of arranging the elements of an array so that they can be placed either
in ascending or descending order.

1. Merge Sort
▪ Merge sort is a sorting technique based on divide and conquer technique. With worst-
case time complexity being Ο(n log n), it is one of the most used and approached
algorithms.
▪ Merge sort first divides the array into equal halves and then combines them in a sorted
manner.

How Merge Sort Works?

To understand merge sort, we take an unsorted array as the following −

We know that merge sort first divides the whole array iteratively into equal halves unless the
atomic values are achieved. We see here that an array of 8 items is divided into two arrays of
size 4.

This does not change the sequence of appearance of items in the original. Now we divide these
two arrays into halves.

We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note
the color codes given to these lists.

We first compare the element for each list and then combine them into another list in a sorted
manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target
list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42
and 44 are placed sequentially.

In the next iteration of the combining phase, we compare lists of two data values, and merge
them into a list of found data values placing all in a sorted order.

After the final merging, the list becomes sorted and is considered the final solution.

Merge Sort Algorithm

Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is considered sorted. Then, merge sort combines
the smaller sorted lists keeping the new list sorted too.

Step 1: If it is only one element in the list, consider it already sorted, so return.
Step 2: Divide the list recursively into two halves until it can no more be divided.
Step 3: Merge the smaller lists into new list in sorted order.

Merge Sort code:


Applications of Merge Sort:
• Sorting large datasets: Merge sort is particularly well-suited for sorting large datasets
due to its guaranteed worst-case time complexity of O(n log n).
• External sorting: Merge sort is commonly used in external sorting, where the data to be
sorted is too large to fit into memory.
• Custom sorting: Merge sort can be adapted to handle different input distributions, such
as partially sorted, nearly sorted, or completely unsorted data.
• Inversion Count Problem

Advantages of Merge Sort:


• Stability: Merge sort is a stable sorting algorithm, which means it maintains the relative
order of equal elements in the input array.
• Guaranteed worst-case performance: Merge sort has a worst-case time complexity of
O(N logN), which means it performs well even on large datasets.
• Parallelizable: Merge sort is a naturally parallelizable algorithm, which means it can be
easily parallelized to take advantage of multiple processors or threads.

Drawbacks of Merge Sort:


• Space complexity: Merge sort requires additional memory to store the merged sub-
arrays during the sorting process.
• Not in-place: Merge sort is not an in-place sorting algorithm, which means it requires
additional memory to store the sorted data. This can be a disadvantage in applications
where memory usage is a concern.
• Not always optimal for small datasets: For small datasets, Merge sort has a higher
time complexity than some other sorting algorithms, such as insertion sort. This can result
in slower performance for very small datasets.
2: Quick Sort
Quicksort is a sorting algorithm based on the divide and conquer approach where

1. An array is divided into subarrays by selecting a pivot element (element selected from
the array). While dividing the array, the pivot element should be positioned in such a way
that elements less than pivot are kept on the left side and elements greater than pivot
are on the right side of the pivot.
2. The left and right subarrays are also divided using the same approach. This process
continues until each subarray contains a single element.
3. At this point, elements are already sorted. Finally, elements are combined to form a
sorted array.

Worst Case of Quick Sort:


Code:

Quicksort Applications
Quicksort's efficiency and adaptability make it suitable for many applications, including but not
limited to:

1. Sorting Algorithms: Quicksort is frequently used as a building block for hybrid sorting
algorithms, such as Timsort (used in Python's built-in sorting function).
2. Database Systems: Quicksort plays a vital role in database management systems for sorting
records efficiently.
3. Computer Graphics: Rendering and graphics applications often involve sorting operations,
where Quicksort can be employed to optimize rendering performance.
4. Network Routing: Quicksort can be utilized in various networking algorithms, particularly
routing tables.
5. File Systems: File systems use Quicksort to manage and organize files efficiently.

Advantages Disadvantages
The quick sort is regarded as the best The slight disadvantage of quick sort is
sorting algorithm. that its worst-case performance is
similar to average performances of the
bubble, insertion or selections sorts.
It is able to deal well with a huge list of If the list is already sorted than bubble
items. sort is much more efficient than quick
sort
Because it sorts in place, no additional If the sorting element is integers than
storage is required as well radix sort is more efficient than quick
sort.

You might also like