Professional Documents
Culture Documents
LABMANUALFORMATCNdocx 2022 08 28 20 44 43
LABMANUALFORMATCNdocx 2022 08 28 20 44 43
of Algorithm
(3150703)
Lab Manual
Practical 5
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical 6
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical 7
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical 8
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical 12
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
Output:
Time Complexity:
Best case: O(n)
Worst case: O(n2)
Average case: O(n2)
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
Output:
Time Complexity:
Best case: O(n)
Worst case: O(n2)
Average case: O(n2)
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);
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)
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
Output:
Time Complexity:
Best case: O(nlogn)
Worst case: O(nlogn)
Average case: O(nlogn)
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
Output:
Time Complexity:
Best case: O(nlogn)
Worst case: O(n2)
Average case: O(nlogn)
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical -2
Algorithm:
Output:
Time Complexity:
Best case: O(1)
Worst case: O(nlogn)
Average case: O(nlogn)
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Algorithm:
Output:
Time Complexity:
Best case: O(1)
Worst case: O(nlogn)
Average case: O(nlogn)
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);
}
}
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)
FACULTY OF ENGINEERING
Information Technology
Analysis and Design of Algorithm
Practical-4
Algorithm:
Output:
Time Complexity:
Best case: O(n)
Worst case: O(n)
Average case: O(n)