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

1-3

The document outlines three practical implementations in C: Merge Sort, Quick Sort, and a selection procedure to find the k-th smallest element. Each section includes the aim, objectives, time complexity analysis, and the corresponding C code. The time complexities for Merge Sort and Quick Sort are O(n log n) in the best and average cases, while the selection procedure has a best and average case of O(n).

Uploaded by

rirakik320
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

1-3

The document outlines three practical implementations in C: Merge Sort, Quick Sort, and a selection procedure to find the k-th smallest element. Each section includes the aim, objectives, time complexity analysis, and the corresponding C code. The time complexities for Merge Sort and Quick Sort are O(n log n) in the best and average cases, while the selection procedure has a best and average case of O(n).

Uploaded by

rirakik320
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical 1: Implementation of Merge Sort

Aim: To implement the Merge Sort algorithm in C and analyze its time complexity.
Objective:
 To understand and implement the Divide and Conquer approach.
 To analyze the time complexity of Merge Sort.
 To sort an array using the Merge Sort technique.
Time Complexity:
 Best Case: O(n log n)
 Average Case: O(n log n)
 Worst Case: O(n log n)
Program:
#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {

if (L[i] <= R[j])


arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, size);
mergeSort(arr, 0, size - 1);

printf("Sorted array: ");


printArray(arr, size);
return 0;
}
Output:
Original array: 38 27 43 3 9 82 10
Sorted array: 3 9 10 27 38 43 82
PS C:\Users\user\OneDrive\Desktop\JS DOM>
Practical 2: Implementation of Quick Sort
Aim: To implement the Quick Sort algorithm in C and analyze its efficiency.
Objective:
 To understand and implement the Divide and Conquer approach.
 To analyze the performance of Quick Sort.
 To sort an array using the Quick Sort algorithm.
Time Complexity:
 Best Case: O(n log n)
 Average Case: O(n log n)
 Worst Case: O(n^2)
Program:
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {

i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {

int pi = partition(arr, low, high);


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, size);

quickSort(arr, 0, size - 1);

printf("Sorted array: ");


printArray(arr, size);
return 0;
}
Output
Original array: 10 7 8 9 1 5
Sorted array: 1 5 7 8 9 10
PS C:\Users\user\OneDrive\Desktop\JS DOM>
Practical 3: Implementation of Selection
Procedure
Aim: To implement a selection procedure in C to find the k-th smallest element in an array.
Objective:
 To understand and implement a selection procedure for finding the k-th smallest element.
 To analyze its time complexity.
 To use Quick Select algorithm for efficiency.
Time Complexity:
 Best Case: O(n)
 Average Case: O(n)
 Worst Case: O(n^2)
Program:
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

int quickSelect(int arr[], int low, int high, int k) {


if (low <= high) {
int pi = partition(arr, low, high);
if (pi == k)
return arr[pi];
else if (pi > k)
return quickSelect(arr, low, pi - 1, k);
else
return quickSelect(arr, pi + 1, high, k);
}
return -1;
}

int main() {
int arr[] = {7, 10, 4, 3, 20, 15};
int size = sizeof(arr) / sizeof(arr[0]);
int k = 3;

printf("%d-th smallest element is %d\n", k, quickSelect(arr, 0, size - 1, k - 1));


return 0;
}
Output:

ig }
3-th smallest element is 7
PS C:\Users\user\OneDrive\Desktop\JS DOM>

You might also like