yuva hpc 1 (1) (5)
yuva hpc 1 (1) (5)
PRACTICAL: 03
AIM : Using Divide and Conquer Strategies design a class for Concurrent Quick Sort
using C.
Here's an introduction to divide and conquer strategies :Divide and conquer is a powerful
problem-solving technique used in computer science and algorithm design. It involves breaking down
a complex problem into smaller, more manageable subproblems that are easier to solve. Once the
subproblems are solved, their solutions are combined to produce the solution to the original problem
1. Divide: The problem is divided into smaller subproblems that are similar in structure to the
original problem.
2. Conquer: The subproblems are solved recursively. If the subproblems are small enough, they
are solved directly.
3. Combine: The solutions to the subproblems are combined to produce the solution to the original
problem.
Example:
A classic example of a divide and conquer algorithm is merge sort. Merge sort works by dividing an
unsorted list into two halves, recursively sorting each half, and then merging the sorted halves back
together.
Advantages of Divide and Conquer:
Efficiency: Divide and conquer algorithms can be very efficient, especially for large
problems.This is because they break down the problem into smaller pieces that can be solved
independently and in parallel.
Simplicity: Divide and conquer algorithms can be easier to understand and implement than other
types of algorithms. This is because they follow a clear and concise three-step approach.
Modularity: Divide and conquer algorithms are modular, which means that they can be easily
reused to solve different problems. This is because the same basic approach can be applied to a
wide variety of problems.
Pg.12
Erp:-2203031240806
Faculty of Engineering & Technology
Subject Name: High Performance Computing
Subject Code: 303105356
BTech-CSE (AI) 3rd Year 6th Semester
Divide and conquer strategies are used in a wide variety of applications, Sorting
including: algorithms (e.g., merge sort, quick sort).
1.Searching algorithms (e.g., binary search)
2.Finding the closest pair of points
3.Matrix multiplication
4.Fast Fourier Transform (FFT)
1. Choose a pivot: Select an element from the array. This element will be used to partition the array.
A common choice for the pivot is the first or last element.
2. Partition: Rearrange the array so that all elements smaller than the pivot are placed before it, and
all elements greater than the pivot are placed after it. The pivot is now in its final sorted position.
3. Recursively sort subarrays: Apply the Quicksort algorithm recursively to the subarrays on the left
and right of the pivot. This continues until the entire array .
Program :
%%writefile quicksort.c
#include <stdio.h>
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
Pg.13
Erp:-2203031240806
Faculty of Engineering & Technology
Subject Name: High Performance Computing
Subject Code: 303105356
BTech-CSE (AI) 3rd Year 6th Semester
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
Pg.14
Erp:-2203031240806
Faculty of Engineering & Technology
Subject Name: High Performance Computing
Subject Code: 303105356
BTech-CSE (AI) 3rd Year 6th Semester
Output :
Conclusion :
The C code implements Quicksort using divide and conquer for efficient sorting. It
has an average time complexity of O(n log n), making it fast for large datasets. The algorithm sorts in-
place, minimizing memory usage. Recursion enables elegant and concise code structure. Quicksort is a
robust and adaptable sorting solution widely used in practice.
Pg.15
Erp:-2203031240806