PDC Lab Manual
PDC Lab Manual
Lab No 6
Write a serial C program to implement the Bubble Sort algorithm. The program should take an array
of integers and sort it in ascending order. The program should also measure the time taken to sort an
array of size 1,000,000.
Hints:
Bubble Sort compares adjacent elements and swaps them if they are in the wrong order. This
process is repeated until the array is sorted.
The time complexity of Bubble Sort is O(n²), so for large arrays, the program will take a
significant amount of time.
Example:
Write a parallel version of your sorting program using the Merge Sort algorithm and POSIX Threads
for parallelization. The program should measure the time required to sort an array of integers of size
1,000,000. Compare the speed-up between the serial and parallel versions.
Hints:
Merge Sort is a divide-and-conquer algorithm with a time complexity of O(n log n).
Use POSIX threads to divide the array into smaller subarrays and merge them in parallel.
Example:
University of Central Punjab
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)
FACULTY OF INFORMATION TECHNOLOGY
Write a serial C program to multiply two square matrices of size n x n (where n is a user input) and
print the resulting matrix. The program should measure the time taken to perform the multiplication.
Hints:
Matrix multiplication is done by taking the dot product of the rows of the first matrix and the
columns of the second matrix.
The time complexity of matrix multiplication is O(n³), so for large matrices, the computation
can be time-consuming.
Example:
Input matrices:
A = [1 2 3]
[4 5 6]
[7 8 9]
B = [9 8 7]
[6 5 4]
[3 2 1]
Output matrix C = A * B:
C = [30 24 18]
[84 69 54]
[138 114 90]
University of Central Punjab
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)
FACULTY OF INFORMATION TECHNOLOGY
Write a parallel C program to multiply two square matrices using POSIX Threads to distribute the
workload across multiple threads. Measure the time taken to multiply matrices of size n x n for large n
(e.g., n = 1,000) and compare the time with the serial implementation.
Hints:
Matrix multiplication can be parallelized by dividing the rows of the first matrix among
multiple threads.
Use POSIX threads to parallelize the outermost loop of the matrix multiplication.
Example:
Write a C program that calculates the Fibonacci sequence recursively. The program should prompt the
user to enter a number n, and then calculate the nth Fibonacci number. Measure the time taken to
compute the nth Fibonacci number for large values of n.
Hints:
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2)
University of Central Punjab
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)
FACULTY OF INFORMATION TECHNOLOGY
The time complexity of this recursive approach is exponential, so it can be very slow for large
values of n.
Example:
Input: n = 10
Output: F(10) = 55
Write a C program that calculates the Fibonacci sequence iteratively. The program should prompt the
user to enter a number n and then calculate the nth Fibonacci number. Measure the time taken to
compute the nth Fibonacci number for large values of n.
Hints:
The iterative approach uses a loop to calculate Fibonacci numbers, which has a time
complexity of O(n) compared to the recursive approach's exponential time complexity.
Example:
Input: n = 10
Output: F(10) = 55