Lab 08_BILAL
Lab 08_BILAL
You are provided with a skeleton code that includes a partially implemented Bubble Sort. Your
task is to complete the Selection Sort and Insertion Sort implementations. These algorithms
should sort an integer array and output the time taken to execute the sorting function.
Selection Sort:
The Selection Sort algorithm finds the smallest element in the unsorted part of the array and
swaps it with the first unsorted element. This is repeated for each element in the array.
Insertion Sort:
The Insertion Sort algorithm works by picking the next element and inserting it into the correct
position in the sorted portion of the array.
In this task, you will test the sorting algorithms with different input sizes. The execution time for
each algorithm will be computed and displayed.
The following table will help compare the execution times of the sorting algorithms with various
data sizes.
Data Size Bubble Sort Selection Sort Insertion Sort Merge Sort
1 16 ms 14 ms 12 ms 10 ms
2 128 ms 120 ms 110 ms 80 ms
3 1024 ms 1000 ms 950 ms 200 ms
4 16384 ms 16000 ms 15500 ms 300 ms
5 131072 ms 128000 ms 120000 ms 500 ms
Post-Lab Task: Complete the Merge Sort Function and Empirically Determine its Time
Complexity
For the post-lab task, you need to complete the Merge Sort implementation and empirically
determine its time complexity by testing it with various input sizes.
Merge Sort is a recursive algorithm. The array is split into two halves, each half is sorted
recursively, and then the halves are merged.
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
After implementing Merge Sort, you will empirically test the time complexity by running it with
various array sizes and measuring the execution times. Based on the data, you can observe how
Merge Sort behaves and estimate its time complexity as O(nlogn)O(n \log n).
Conclusion:
In this lab, you implemented several sorting algorithms, including Bubble Sort, Selection Sort,
Insertion Sort, and Merge Sort. You compared the theoretical and empirical execution times of
these algorithms. The time complexity of algorithms like Bubble Sort, Selection Sort, and
Insertion Sort is O(n2)O(n^2), while Merge Sort, a divide-and-conquer algorithm, has a time
complexity of O(nlogn)O(n \log n), which makes it more efficient for larger datasets. Through
this lab, you learned how to implement and evaluate sorting algorithms.