Design and Analysis of Algorithm (DAA) : 7.determine Product of 2 Integers (A B) Using Multiplication As Repeated Sums
Design and Analysis of Algorithm (DAA) : 7.determine Product of 2 Integers (A B) Using Multiplication As Repeated Sums
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the number1:-");
scanf("%d",&a);
c=a*b;
printf("Product of %d and %d is %d\n",a,b,c);
}
int product(int a,int b)
{
if (a<b)
{
return product(b,a);
}
else if (b!=0)
{
return (a+product(a,b-1));
}
else
{
return 0;
}
}
Output:---
PG. 1
Design and Analysis of Algorithm(DAA)
8.Binary Search of an ordered array. Iterative and Recursive are
possible.
Code:---
==>Iteration:--
#include<stdio.h>
void main()
{
int f1,l1,m1,size,i,n,a[100];
printf("Enter the size of the list: ");
scanf("%d",&size);
printf("Enter %d size of value\n", size);
for (i = 0; i < size; i++)
{
scanf("%d",&a[i]);
}
printf("Enter value to be search: ");
scanf("%d", &n);
f1 = 0;
l1 = size - 1;
m1 = (f1+l1)/2;
while (f1 <= l1)
{
if (a[m1] < n)
f1= m1 + 1;
else if (a[m1] == n)
{
printf("Element found at index %d.\n",m1);
break;
}
else
{
l1 = m1-1;
}
m1=(f1 + l1)/2;
}
if (f1 > l1)
{
printf("Element Not found in the list.\n");
}
}
Output:---
Enter the size of the list: 3
Enter 3 size of value
22
23
45
Enter value to be search: 23
Element found at index 1.
PG. 2
Design and Analysis of Algorithm(DAA)
==> Recursion:----
#include <stdio.h>
void binary_search(int [], int, int, int);
void bubble_sort(int [], int);
int main()
{
int key, size, i;
int list[25];
printf("Enter size of a list: ");
scanf("%d", &size);
printf("Generating random numbers\n");
for(i = 0; i < size; i++)
{
list[i] = rand() % 100;
printf("%d ", list[i]);
}
bubble_sort(list, size);
printf("\n\n");
printf("Enter key to search\n");
scanf("%d", &key);
binary_search(list, 0, size, key);
}
void bubble_sort(int list[], int size)
{
int temp, i, j;
for (i = 0; i < size; i++)
{
for (j = i; j < size; j++)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void binary_search(int list[], int lo, int hi, int key)
{
int mid;
if (lo > hi)
{
printf("Key not found\n");
return;
}
mid = (lo + hi) / 2;
if (list[mid] == key)
{
PG. 3
Design and Analysis of Algorithm(DAA)
printf("Key found\n");
}
else if (list[mid] > key)
{
binary_search(list, lo, mid - 1, key);
}
else if (list[mid] < key)
{
binary_search(list, mid + 1, hi, key);
}
}}
Output:---
Code:---
#include <stdio.h>
void mergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50],i,size;
printf("Enter total number of elements:");
scanf("%d", &size);
printf("Enter the elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
partition(list, 0, size - 1);
printf("After merge sort:\n");
for(i = 0;i < size; i++)
{
printf("%d ",list[i]);
}
return 0;
}
void partition(int list[],int low,int high)
{
int mid;
PG. 4
Design and Analysis of Algorithm(DAA)
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
void mergeSort(int list[],int low,int mid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for (k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}
}
else
{
for (k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
list[k] = temp[k];
}
}
PG. 5
Design and Analysis of Algorithm(DAA)
Output:---
Code:---
#include <stdio.h>
void bubblesort(int arr[], int size)
{
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size - i; j++)
{
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
}
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int array[100], i, size;
printf("How many numbers you want to sort: ");
scanf("%d", &size);
printf("\nEnter %d numbers : ", size);
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
bubblesort(array, size);
printf("\nSorted array is ");
for (i = 0; i < size; i++)
{
printf(" %d ", array[i]);
PG. 6
Design and Analysis of Algorithm(DAA)
}
return 0;
}
Output:---
PG. 7