0% found this document useful (0 votes)
22 views

HPC Practical 3 Vish-23-25

hpc practical
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 views

HPC Practical 3 Vish-23-25

hpc practical
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/ 3

Faculty of Engineering & Technology

High Performance Computing Laboratory


(203105430)
B. Tech CSE 4th Year 7th Semester

PRACTICAL : 03

AIM: Using Divide and Conquer Strategies design a class for Concurrent
Quick Sort

INTRODUCTION:
Divide and Conquer is a powerful algorithmic technique used to solve complex problems by
breaking them down into simpler subproblems, solving each subproblem independently, and
then combining their solutions to form a solution to the original problem. Here's a step-by-step
introduction to the concept:

Basic Steps of Divide and Conquer

1. Divide: Split the problem into smaller, independent subproblems. The goal is to break
down the problem into subproblems of similar type, but of reduced complexity.

2. Conquer: Solve each subproblem independently. If the subproblems are still too large,
apply the divide and conquer strategy recursively to further break them down.

3. Combine: Combine the solutions of the subproblems to obtain the solution to the
original problem.

Example Algorithms Using Divide and Conquer

1) Merge Sort:

 Divide: Split the array into two halves.


 Conquer: Recursively sort each half.
 Combine: Merge the two sorted halves to produce the sorted array.

2) Quick Sort:

 Divide: Select a pivot element and partition the array into two halves such that elements
less than the pivot are on one side and elements greater than the pivot are on the other.
 Conquer: Recursively sort the partitions.
 Combine: Since the array is sorted in place, no additional work is needed to combine.

Enrollment No.: 210303105261


Name: Vidhi Joshi
P a g e | 23
Div: 7B25
Faculty of Engineering & Technology
High Performance Computing Laboratory
(203105430)
B. Tech CSE 4th Year 7th Semester

3) Binary Search:

 Divide: Compare the target value to the middle element of the array.
 Conquer: If the target value is equal to the middle element, return the index. If the
target value is less, recursively search in the left half. If the target value is greater,
recursively search in the right half.
 Combine: The combine step is trivial as the result is directly obtained from the
recursive call.

Advantages of Divide and Conquer

 Efficiency: By breaking a problem into smaller subproblems, we can reduce the time
complexity in many cases.
 Parallelism: Independent subproblems can often be solved in parallel, leading to
potential performance improvements on multi-core processors.
 Simplicity: Complex problems can be simplified into more manageable subproblems,
making the overall problem easier to understand and solve.

Disadvantages of Divide and Conquer

 Overhead: The process of dividing the problem and combining the results can
introduce additional overhead.
 Not Always Optimal: For some problems, divide and conquer may not lead to the most
efficient solution compared to other algorithms.

PSEUDOCODE:

Enrollment No.: 210303105261


Name: Vidhi Joshi
P a g e | 24
Div: 7B25
Faculty of Engineering & Technology
High Performance Computing Laboratory
(203105430)
B. Tech CSE 4th Year 7th Semester

CODE:

def sort(a, low, high):


if low < high:
pivot = low
i = low
j = high
while i < j:
while a[i] <= a[pivot]:
i += 1
while a[j] > a[pivot]:
j -= 1
if i < j:
a[i], a[j] = a[j], a[i]
a[j], a[pivot] = a[pivot], a[j]
sort(a, low, j - 1)
sort(a, j + 1, high)
return a

n = int(input("Enter te array size: "))


a = [int(x) for x in input("Enter the array elements: ").split()]
sort(a, 0, n - 1)
print("Sorted array is: ", *a)

OUTPUT:

Enrollment No.: 210303105261


Name: Vidhi Joshi
P a g e | 25
Div: 7B25

You might also like