0% found this document useful (0 votes)
49 views14 pages

Q5 Daa Ass2

Uploaded by

Agam Vishwas
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)
49 views14 pages

Q5 Daa Ass2

Uploaded by

Agam Vishwas
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/ 14

Q.5.

Write algorithms and analyse (Time and Space) the following sor ng algorithms: Bubble Sort,
Selec on Sort, Inser on Sort, Merge Sort, Quick Sort, and Heap Sort. Implement each of the
algorithms in C language.

ANS :-

BUBBLE SORT
ALGORITHM –
BubbleSort(array, n)
for i = 0 to n-1
for j = 0 to n-i-2
if array[j] > array[j+1]
swap(array[j], array[j+1])
end for
end BubbleSort

Time Complexity:
 Best Case: O(n) (When the array is already sorted)
 Average Case: O(n^2)
 Worst Case: O(n^2)
Space Complexity:
 Auxiliary Space: O(1) (In-place sorting)

C Implimentation
#include <stdio.h>

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


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

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


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

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

SELECTION SORT
ALGORITHM -
SelectionSort(array, n)
for i = 0 to n-1
minIndex = i
for j = i+1 to n-1
if array[j] < array[minIndex]
minIndex = j
end for
swap(array[minIndex], array[i])
end for
end SelectionSort

Time Complexity:
 Best, Average, and Worst Case: O(n^2)

Space Complexity:
 Auxiliary Space: O(1) (In-place sorting)

C Implementation –
#include <stdio.h>

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


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

temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

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


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

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

INSERTION SORT
ALGORITHM -
InsertionSort(array, n)
for i = 1 to n-1
key = array[i]
j = i-1
while j >= 0 and array[j] > key
array[j+1] = array[j]
j = j-1
end while
array[j+1] = key
end for
end InsertionSort

Time Complexity:
 Best Case: O(n)
 Average Case: O(n^2)
 Worst Case: O(n^2)
Space Complexity:
 Auxiliary Space: O(1)

C Implementation –
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

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


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

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

MERGE SORT
ALGORITHM -
MergeSort(array, left, right)
if left < right
mid = (left + right) / 2
MergeSort(array, left, mid)
MergeSort(array, mid + 1, right)
Merge(array, left, mid, right)
end if
end MergeSort

Merge(array, left, mid, right)


create temp arrays L and R
copy elements into L and R from array
i, j, k = 0
while L and R have elements
if L[i] <= R[j]
array[k] = L[i]
i++
else
array[k] = R[j]
j++
end if
k++
end while
copy remaining elements of L and R into array
end Merge

Time Complexity:
 Best, Average, and Worst Case: O(nlogn)
Space Complexity:
 Auxiliary Space: O(n)

C Implementation –
#include <stdio.h>
#include <stdlib.h>

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


int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];


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

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


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

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, arr_size - 1);
printf("Sorted array: \n");
printArray(arr, arr_size);
return 0;
}

QUICK SORT
ALGORITHM -
QuickSort(array, low, high)
if low < high
pivotIndex = Partition(array, low, high)
QuickSort(array, low, pivotIndex - 1)
QuickSort(array, pivotIndex + 1, high)
end if
end QuickSort

Partition(array, low, high)


pivot = array[high]
i = low - 1
for j = low to high-1
if array[j] < pivot
i++
swap(array[i], array[j])
end if
end for
swap(array[i+1], array[high])
return i+1
end Partition

Time Complexity:
 Best and Average Case: O(nlogn)
 Worst Case: O(n^2)
Space Complexity:
 Auxiliary Space: O(logn)

C Implementation –
#include <stdio.h>

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


int t = *a;
*a = *b;
*b = t;
}

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


int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; 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 n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

HEAP SORT

ALGORITHM -
HeapSort(array, n)
BuildMaxHeap(array, n)
for i = n-1 to 1
swap(array[0], array[i])
MaxHeapify(array, 0, i)
end for
end HeapSort

BuildMaxHeap(array, n)
for i = n/2 - 1 to 0
MaxHeapify(array, i, n)
end for
end BuildMaxHeap

MaxHeapify(array, i, n)
largest = i
left = 2*i + 1
right = 2*i + 2
if left < n and array[left] > array[largest]
largest = left
end if
if right < n and array[right] > array[largest]
largest = right
end if
if largest != i
swap(array[i], array[largest])
MaxHeapify(array, largest, n)
end if
end MaxHeapify

Overall Time Complexity:


 Building the heap: O(n)
 Extracting elements: O(nlogn)
 Total Time Complexity: O(nlogn)
Space Complexity:
 Auxiliary Space:O(1) because Heap Sort is an in-place sorting algorithm.

C Implementation –

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i) {


int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {

swap(&arr[0], &arr[i]);

heapify(arr, i, 0);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array:\n");
printArray(arr, n);

heapSort(arr, n);

printf("Sorted array using Heap Sort:\n");


printArray(arr, n);

return 0;
}

You might also like