LABMANUALFORMATCNdocx 2022 08 28 20 44 43
LABMANUALFORMATCNdocx 2022 08 28 20 44 43
LABMANUALFORMATCNdocx 2022 08 28 20 44 43
of Algorithm
(3150703)
Lab Manual
1
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical 1
Algorithm:
2
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
printf("\nTime efficiency is %lf\n", tc);
return 0;
}
Output:
Time Complexity:
Best case: O(n)
Worst case: O(n2)
Average case: O(n2)
3
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
4
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
tc = (difftime(end, start) / CLOCKS_PER_SEC);
printf("\ntime efficiency is %lf", tc);
}
Output:
Time Complexity:
Best case: O(n)
Worst case: O(n2)
Average case: O(n2)
5
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
#include <stdio.h>
#include <time.h>
int main()
{
int a[10], i, j, n, min, temp;
time_t start, end;
double tc;
printf("Please Enter the number of Element: ");
scanf("%d", &n);
printf("\nPlease Enter the Value of Elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
start = clock();
for (i = 0; i < n - 1; i++)
{
min = a[i];
for (j = i + 1; j < n; j++)
{
if (a[j] < a[i])
{
min = j;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
printf("%d,", a[i]);
}
printf("%d", a[i]);
end = clock();
tc = (difftime(end, start) / CLOCKS_PER_SEC);
6
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
printf("\ntime efficiency is %lf", tc);
return 0;
}
Output:
Time Complexity:
Best case: O(n2)
Worst case: O(n2)
Average case: O(n2)
7
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
8
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
}
while (j < n2)
{
arr[k] = M[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);
}
}
// Print the array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
clock_t t;
double total_t;
int n = 10, i;
int arr[50] = {15, 94, 82, 72, 66, 59, 48, 38, 25, 51};
t = clock();
mergeSort(arr, 0, n - 1);
t = clock() - t;
total_t = (double)(t) / CLOCKS_PER_SEC;
printf("\nMerge Sort: ");
printArray(arr, n);
printf("Time efficiency is %f", total_t);
}
9
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Output:
Time Complexity:
Best case: O(nlogn)
Worst case: O(nlogn)
Average case: O(nlogn)
10
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
11
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
{
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
clock_t t;
double total_t;
int n = 10, i;
int arr[50] = {10, 94, 67, 50, 66, 90, 55, 38, 25, 51};
t = clock();
quickSort(arr, 0, n - 1);
t = clock() - t;
total_t = (double)(t) / CLOCKS_PER_SEC;
printf("\nQuick Sort: ");
printArray(arr, n);
printf("Time efficiency is %f", total_t);
}
Output:
Time Complexity:
Best case: O(nlogn)
Worst case: O(n2)
Average case: O(nlogn)
12
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
13
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical -2
Algorithm:
14
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Output:
Time Complexity:
Best case: O(1)
Worst case: O(nlogn)
Average case: O(nlogn)
15
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
16
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Output:
Time Complexity:
Best case: O(1)
Worst case: O(nlogn)
Average case: O(nlogn)
17
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical-3
Algorithm:
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
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) {
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
18
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {1, 12, 8, 5, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
printf("Sorted array is \n");
printArray(arr, n);
}
Output:
Time Complexity:
Best case: O(nlogn)
Worst case: O(nlogn)
Average case: O(nlogn)
19
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical-4
Algorithm:
20
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
end = clock();
cpu = ((double)(end - start) / CLOCKS_PER_SEC);
Output:
Time Complexity:
Best case: O(n)
Worst case: O(n)
Average case: O(n)
21