Assignment 2
Assignment 2
Problem Statement:
Write a program to implement Parallel Bubble Sort and Merge sort using OpenMP. Use existing
algorithms and measure the performance of sequential and parallel algorithms.
Theory:
Bubble Sort
Odd-Even Transposition
Sorting n = 8 elements, using the odd-even transposition sort algorithm. During each phase, n = 8
elements are compared.
Odd-Even Transposition
Sequential odd-even transposition sort algorithm
2. Each phase of the algorithm (either odd or even) requires Θ(n) comparisons.
2. There are n iterations, in each iteration, each processor does one compare-exchange.
4. This is cost optimal with respect to the base serial algorithm but not the optimal one.
2. The #pragma omp parallel for directive tells the compiler to create a team of threads to
execute the for loop within the block in parallel.
3. Each thread will work on a different iteration of the loop, in this case on comparing and
swapping the elements of the array.
4. The bubbleSort function takes in an array, and it sorts it using the bubble sort algorithm. The
outer loop iterates from 0 to n-2 and the inner loop iterates from 0 to n-i-1, where i is the
index of the outer loop. The inner loop compares the current element with the next
element, and if the current element is greater than the next element, they are swapped.
5. The main function creates a sample array and calls the bubbleSort function to sort it. The
sorted array is then printed.
6. This is a skeleton code and it may not run as is and may need some modification to work
with specific inputs and requirements.
7. It is worth noting that bubble sort is not an efficient sorting algorithm, specially for large
inputs, and it may not scale well with more number of threads. Also parallelizing bubble sort
does not have a significant improvement in performance due to the nature of the algorithm
itself.
9. The two #pragma omp parallel for inside while loop, one for even indexes and one for odd
indexes, allows each thread to sort the even and odd indexed elements simultaneously and
prevent the dependency.
Aodd and Aeven are defined as the set of elements of A with odd and even indices, respectively.
For example, Aodd = {a .. a3,a" ... } and Aeven = { ~,a4,a6" .. } regarding a set of elements A = {a…an}'
Similarly, let a set of elements B = {b ….bn}. We can then define the merge operation as:
Implementation:
Compiling command
To run file:
bubble
CONCLUSION
In conclusion, the implementation and performance evaluation of Parallel Bubble Sort and Merge
Sort using OpenMP have provided valuable insights into the effectiveness of parallelizing well-known
sorting algorithms. Parallelization offers advantages in terms of speedup, it also underscores the
need for careful consideration of load balancing and communication overhead.