DAAlab_file
DAAlab_file
ALGORITHMS
Algorithm Design:
The important aspects of algorithm design include creating an efficient algorithm
to solve a problem in an efficient way using minimum time and space. To solve a
problem, different approaches can be followed. Some of them can be efficient
with respect to time consumption, whereas other approaches may be memory
efficient. However, one has to keep in mind that both time consumption and
memory usage cannot be optimized simultaneously.
The Need for Analysis: Algorithms are often quite different from one another,
though the objectives of these algorithms are the same. Analysis of algorithm is the
process of analyzing the problem-solving capability of the algorithm in terms of
the time and size required. Generally, the following types of analysis are
performed −
Worst-case − the maximum number of steps taken on any instance of size a.
Best-case − the minimum number of steps taken on any instance of size a.
Average case − the average number of steps taken on any instance of size a.
Divide and Conquer: This is another effective way of solving many problems. In
Divide and Conquer algorithms, divide the algorithm into two parts; the first parts
divide the problem on hand into smaller sub problems of the same type. Then, in
the second part, these smaller problems are solved and then added together
(combined) to produce the problem’s final solution.
Problems such as Binary Search, Quick Sort, and Merge Sort can be solved using
this technique.
Brute Force Algorithm: This is one of the simplest algorithms in the concept. A
brute force algorithm blindly iterates all possible solutions to search one or more
than one solution that may solve a function. Think of brute force as using all
possible combinations of numbers to open a safe.
Problems such as Selection Sort, convex hull can be solved using this technique.
Branch and Bound: In Branch & Bound algorithm a given sub problem, which
cannot be bounded, has to be divided into at least two new restricted sub problems.
Branch and Bound algorithm are methods for global optimization in non-convex
problems. Branch and Bound algorithms can be slow, however in the worst case
they require effort that grows exponentially with problem size.
LIST OF LAB PROGRAMS
1.
Program for Heap Sort.
2.
Program for Merge Sort.
3.
4. Program for Selection Sort.
#include <stdio.h>
int binarySearch(int [], int, int, int);
int main()
{
int c, first, last, n, search, array[100], index;
first = 0;
last = n - 1;
if (index == -1)
printf("Not found! %d isn't present in the list.\n", search);
else
printf("%d is present at location %d.\n", search, index + 1);
return 0;
}
m = (s + e)/2;
Linear Search
#include<stdio.h>
int Linear_search(int arr[], int Search_ele, int n)
{
int i;
static int temp=0;
if(n>0)
{
i=n-1;
if(arr[i]==Search_ele)
temp=1;
Linear_search(arr,Search_ele,i);
}
return temp;
}
int main()
{
int n,j;
printf("Enter your array size:");
scanf("%d",&n);
int arr[n];
printf("Enter the Array Element:");
for(j=0;j<n;j++)
{
scanf("%d",&arr[j]);
}
int Search_ele;
printf("Enter the search element:");
scanf("%d",&Search_ele);
if(Linear_search(arr,Search_ele,n)==1)
printf("Element found....");
else
printf("Element not found....");
return 0;
}
Program -2 :
#include<stdio.h>
#include <conio.h>
void heapify_function(int arr[])
{
int i,n;
n=arr[0];
for(i=n/2;i>=1;i--)
adjust(arr,i);
}
void adjust(int arr[],int i)
{
int j,temp,n,k=1;
n=arr[0];
while(2*i<=n && k==1)
{
j=2*i;
if(j+1<=n && arr[j+1] > arr[j])
j=j+1;
void main()
{
int arr[100],n,temp,last,i;
clrscr();
printf("How many Numbers you want to enter in your array: \n");
scanf("%d",&n);
printf("Enter Elements in array:\n");
for(i=1;i<=n;i++)
scanf("%d",&arr[i]);
arr[0]=n;
heapify_function(arr);
while(arr[0] > 1)
{
last=arr[0];
temp=arr[1];
arr[1]=arr[last];
arr[last]=temp;
arr[0]--;
adjust(arr,1);
}
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
int main() {
int i;
printf("List before sorting\n");
sort(0, max);
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}
Program 5:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t, flag = 0;
return 0;
}
Program 6:
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
# include<stdio.h>
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
#include<stdio.h>
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{
int i,j;
completed[i]=0;
}
completed[city]=1;
printf("%d--->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
if(min!=999)
cost+=kmin;
return nc;
}
int main()
{
takeInput();
return 0;
}
Program 9:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\tImplementation of Kruskal's algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
Program 10:
#include<stdio.h>
#include<math.h>
char a[10][10];
int n;
void printmatrix() {
int i, j;
printf("\n");
int main() {
int i, j;
nqueen(0);
return (0);
}