0% found this document useful (0 votes)
11 views

Lab 08_BILAL

Uploaded by

eshanaliprivate
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab 08_BILAL

Uploaded by

eshanaliprivate
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 08 - Algorithms

Submitted By; Muhmmad Bilal


Reg; FA23-BCE-061
Course: Data Structures
Instructor: Dr. Omer Ahmed
Due Date: December 04, 2024
Task 1: Complete the functions for Selection Sort and Insertion Sort

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.

void selectionSort(int arr[], int n) {


int i, j, minIndex, temp;
for (i = 0; i < n-1; i++) {
minIndex = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

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.

void insertionSort(int arr[], int n) {


int i, j, key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

Task 2: Empirically Compute the Execution Times for Sorting Methods

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 Implementation:

Merge Sort is a recursive algorithm. The array is split into two halves, each half is sorted
recursively, and then the halves are merged.

void merge(int arr[], int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
int i, j, k;

for (i = 0; i < n1; i++)


L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

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++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

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(nlog⁡n)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(nlog⁡n)O(n \log n), which makes it more efficient for larger datasets. Through
this lab, you learned how to implement and evaluate sorting algorithms.

You might also like