0% found this document useful (0 votes)
35 views7 pages

Design and Analysis of Algorithm (DAA) : 7.determine Product of 2 Integers (A B) Using Multiplication As Repeated Sums

The document discusses different sorting algorithms like bubble sort, merge sort, and binary search. It provides code implementations in C language for multiplying two integers using repeated addition, binary search on an array using iterative and recursive approaches, sorting an array using merge sort, and sorting an array using bubble sort. It also includes sample outputs of each code showing the algorithms working on sample input data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views7 pages

Design and Analysis of Algorithm (DAA) : 7.determine Product of 2 Integers (A B) Using Multiplication As Repeated Sums

The document discusses different sorting algorithms like bubble sort, merge sort, and binary search. It provides code implementations in C language for multiplying two integers using repeated addition, binary search on an array using iterative and recursive approaches, sorting an array using merge sort, and sorting an array using bubble sort. It also includes sample outputs of each code showing the algorithms working on sample input data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Design and Analysis of Algorithm(DAA)

7.Determine product of 2 integers (a*b) using multiplication as


repeated sums.
Code:---

==>Iteration & Recursion:---

#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the number1:-");
scanf("%d",&a);

printf("Enter the number2:-");


scanf("%d",&b);

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:---

pc27@pc27:~/krishna/DAA$ gcc -o k product_iteration.c


pc27@pc27:~/krishna/DAA$ ./k
Enter the number1:-8
Enter the number2:-7
Product of 8 and 7 is 56

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:---

Enter size of a list: 10


Generating random numbers
83 86 77 15 93 35 86 92 49 21
Enter key to search
21
Key found

9.Sort a given sequence of numbers using merge sort.

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:---

Enter total number of elements:5


Enter the elements:
12
36
22
76
54
After merge sort:
12 22 36 54 76

10. Sort a given sequence of numbers using Bubble sort.

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:---

How many numbers you want to sort: 5


Enter 5 numbers : 345 3 20 35 333
Sorted array is : 3 20 35 333 345

PG. 7

You might also like