You are on page 1of 9

Name: NAME:

ShamikDISHA GIDWANI
Puranik
BATCH:
Panel: F1
B
ROLL NO.: 06
Batch: B1
HPC LAB 5
Roll No: 09
HPC Lab – 05

Title: Write a C Program for parallel Quick Sort or


Breadth First Search or Depth first Search
of a large array of integers using OpenMP.

Aim: To compare the execution performance of a C


Program for parallel Quick Sort or Breadth First Search
or Depth first Search with respect to its serial approach
for a large array of integers using OpenMP.

Objective:
1. Write sequential Quick Sort or Breadth First Search or
Depth first Search
2. Calculate complexity of the program
3. Write parallel Quick Sort or Breadth First Search or
Depth first Search
4. Calculate complexity of the program
5. Measure and compare time taken by the parallel
Quick Sort or Breadth First Search or Depth first Search
with respect to the serial
Theory:

1) Write about serial quick sort.

 Given a list of numbers, we want to sort the


numbers in an increasing order
 The same as finding a suitable permutation
 Sequential quicksort algorithm: a recursive
procedure
 Select one of the numbers as pivot
 Divide the list into two sublists: a “low list”
containing numbers
 smaller than the pivot, and a “high list” containing
numbers larger
 than the pivot
 The low list and high list recursively repeat the
procedure to sort
 themselves
 The final sorted result is the concatenation of the
sorted low list,
 the pivot, and the sorted high list
 Quicksort is generally recognized as the fastest
sorting algorithm
 based on comparison of keys, in the average case
 Quicksort has some natural concurrency
 The low list and high list can sort themselves
concurrently
2) Write about parallel Quick sort.

 We consider the case of distributed memory


 Each process holds a segment of the unsorted list
 The unsorted list is evenly distributed among
the processes
 Desired result of a parallel quicksort algorithm:
 The list segment stored on each process is
sorted
 The last element on process i’s list is smaller
than the first element on process i + 1’s list

Mention the number of processors / processor cores


of your: 04

Input: Unsorted array of data points/values.


Output: Sorted Array of data points/values.
Platform: Ubuntu (give latest version) or Windows
Conclusion: Thus, successfully studied, analyzed serial
and parallel Quick Sort or Breadth First Search or Depth
first

Sr no Input size Time taken Time taken


by serial by parallel
1 500 0.002864 0.018147
2 1000 0.005603 0.030363
3 5000 0.028562 0.104258
4 10000 0.060230 0.914542
5 20000 0.119188 2.739227
6 34000 0.283355 7.062926
7 50000 Core dump Core dump

FAQs:

Q1) Explain important properties of ideal sorting


algorithm and complexity analysis of quick sort.

 The ideal sorting algorithm would have the following


properties:
 Stable: Equal keys aren't reordered.
 Operates in place, requiring O(1) extra space.
 Worst-case O(n·lg(n)) key comparisons.
 Worst-case O(n) swaps.
 Adaptive: Speeds up to O(n) when data is
nearly sorted or when there are few unique
keys.

 Complexity Analysis for best case:

Lets T(n) be the time complexity for best cases


n = total number of elements
then
T(n) = 2*T(n/2) + constant*n
2*T(n/2) is because we are dividing array into two
array of equal size
constant*n is because we will be traversing
elements of array in each level of tree

therefore,
T(n) = 2*T(n/2) + constant*n
further we will devide arrai in to array of equalsize
so
T(n) = 2*(2*T(n/4) + constant*n/2) + constant*n ==
4*T(n/4) + 2*constant*n

for this we can say that


T(n) = 2^k * T(n/(2^k)) + k*constant*n
then n = 2^k
k = log2(n)

therefore,
T(n) = n * T(1) + n*logn = O(n*log2(n))

 Complexity Analysis for worst case:

lets T(n) ne total time complexity for worst case


n = total number of elements

T(n) = T(n-1) + constant*n


as we are dividing array into two parts one consist
of single element and other of n-1
and we will traverse individual array
T(n) = T(n-2) + constant*(n-1) + constant*n = T(n-
2) + 2*constant*n - constant
T(n) = T(n-3) + 3*constant*n - 2*constant - constant
T(n) = T(n-k) + k*constant*n - (k-1)*constant ..... -
2*constant - constant

T(n) = T(n-k) + k*constant*n - constant*[(k-1) .... +


3 + 2 + 1]
T(n) = T(n-k) + k*n*constant - constant*[k*(k-1)/2]
put n=k
T(n) = T(0) + constant*n*n - constant*[n*(n-1)/2]
removing constant terms
T(n) = n*n - n*(n-1)/2
T(n) = O(n^2)

Q2) Explain steps required for parallel algorithm


development.

 The process of designing a parallel algorithm


consists of four steps:

□ decomposition of a computational problem into


tasks that can be executed simultaneously, and
development of sequential algorithms for individual
tasks;

□ analysis of computation granularity;


□ minimizing the cost of the parallel algorithm;
□ assigning tasks to processors executing the
parallel algorithm.
Code:

#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
long long k = 0;
int partition(long long *arr,long long lb,long long ub)
{
long long x = arr[ub];
long long i = lb-1;
long long j;
for(j=lb;j<ub;j++)
{
if(arr[j]<=x)
{
i=i+1;
long long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
long long temp = arr[i+1];
arr[i+1] = arr[ub];
arr[ub] = temp;
return i+1;

}
double quicksort_serial(long long *arr,long long lb,long
long ub)
{
long long j;
double start = omp_get_wtime();
if(lb<ub)
{
j = partition(arr,lb,ub);
quicksort_serial(arr,lb,j-1);
quicksort_serial(arr,j+1,ub);
}
double end = omp_get_wtime();
return end-start;
}
double quicksort_parallel(long long *arr,long long lb,long
long ub)
{
long long p;
double start = omp_get_wtime();
if(lb<ub)
{
p = partition(arr,lb,ub);

#pragma omp parallel sections


{
#pragma omp section
{
k = k+1;
quicksort_parallel(arr,lb,p-1);
}
#pragma omp section
{
k =k+1;
quicksort_parallel(arr,p+1,ub);
}
}
}
double end = omp_get_wtime();
return end - start;
}
int main()
{
long n,i;
printf("\nEnter number of elements: ");
scanf("%ld",&n);
long long *arr;
arr = (long long*)malloc(n*sizeof(long long));
for(i=0;i<n;i++)
{
arr[i] = rand() % n;
}
double tserial = quicksort_serial(arr,0,n-1);
printf("Time taken for serial: %lf",tserial);
double tparallel = quicksort_parallel(arr,0,n-1);
printf("\nTime taken for parallel: %lf\n",tparallel);
}

Output:

You might also like