Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Code:

// recursive array maximum minimum


#include <stdio.h>
int findMin(int arr[], int size)
{
if (size == 1)
return arr[0];
int curMin = findMin(arr + 1, size - 1);
if (arr[0] < curMin)
return arr[0];
else
return curMin;
}
int findMax(int arr[], int size)
{
if (size == 1)
return arr[0];

int curMax = findMax(arr + 1, size - 1);


if (arr[0] > curMax)
return arr[0];
else
return curMax;
}
int main()
{
int n, i, min, max;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
printf("Enter %d array elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
min = findMin(a, n);
max = findMax(a, n);
printf("Minimum = %d and maximum = %d", min, max);
return 0;
}

Output:
Enter the size of the array: 5
Enter 5 array elements:
10 20 30 40 50
Minimum = 10 and maximum = 50
Discussion:
In the worst case scenario, the size of the array is reversed by half in each recursive
call. So, the time complexity of the program is O(log n). The recursive calls results in an
increase in memory usage, which is proportional to the no of functions call made. So
the time complexity of the program is O(log n) and the space complexity is O(N).
Code:
//recursive binary search
#include <stdio.h>
int binarySearch(int arr[], int left, int right, int key){
if(right<left)
return -1;
int mid = (left + right)/2;
if(arr[mid]==key)
return mid;
else if(key<arr[mid])
binarySearch(arr,left,mid-1,key);
else if(key>arr[mid])
binarySearch(arr,mid+1,right,key);
}
int main(){
int n,key,i;
printf("Enter the size of the array: ");
scanf("%d",&n);
int a[n];
printf("Enter %d array elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter key value: ");
scanf("%d",&key);
int index= binarySearch(a,0,n,key);
if(key==-1){
printf("not found");
}
else
printf("Found in index %d",index);
return 0;
}

Output:
Enter the size of the array: 5
Enter 5 array elements:
12345
Enter key value: 6
Found in index -1.

Enter the size of the array: 7


Enter 7 array elements:
12 13 14 15 16 17 18
Enter key value: 15
Found in index 3.
Discussion:
The worst time complexity of this recursive binary search algorithm is O(log n) as the
input size is reduced half in each recursive call. The space complexity is O(log n) as
the maximum no of recursive calls is log n, where n is the size of the array.
Code:
//This a C program for merge sort
#include <stdio.h>
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;
int leftarray[n1], rightArray[n2];
for (i = 0; i < n1; i++)
leftarray[i] = a[beg + i];
for (j = 0; j < n2; j++)
rightArray[j] = a[mid + 1 + j];
i = 0;
j = 0;
k = beg;
while (i < n1 && j < n2)
{
if (leftarray[i] <= rightArray[j])
{
a[k] = leftarray[i];
i++;
}
else
{
a[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1)
{
a[k] = leftarray[i];
i++;
k++;
}
while (j < n2)
{
a[k] = rightArray[j];
j++;
k++;
}
}
void mergeSort(int a[], int beg, int end)
{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}
void printArray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
if (i != n - 1)
{
printf("%d, ", a[i]);
}
else
printf("%d", a[i]);
}
printf("\n");
}
int main()
{
int n, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++)
{
printf("Enter array elements:");
scanf("%d", &a[i]);
}
mergeSort(a, 0, n - 1);
printf("After sorted:\n");
printArray(a, n);
return 0;
}

Output:
Enter the size of the array: 5
Enter array elements:
54321
Array elements after sorting:
1, 2, 3, 4, 5

Discussion:
Merge sort is a divide and conquer algorithm with a time complexity of O(nlogn) and
space complexity of O(n). It works by dividing the unsorted list into n sub lists, then
repeatedly merging into sublists to produce new, sorted sublists until there is only
single sublists remaining.
Code:
#include <limits.h>
#include <stdio.h>
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
int MatrixChainOrder(int p[], int n)
{
/* For simplicity of the program, one extra row and one
extra column are allocated in m[][]. 0th row and 0th
column of m[][] are not used */
int m[n][n];
int i, j, k, L, q;
/* m[i, j] = Minimum number of scalar multiplications needed
to compute the matrix A[i]A[i+1]...A[j] = A[i..j] where
dimension of A[i] is p[i-1] x p[i] */
// cost is zero when multiplying one matrix.
for (i = 1; i < n; i++)
m[i][i] = 0;
// L is chain length.
for (L = 2; L < n; L++) {
for (i = 1; i < n - L + 1; i++) {
j = i + L - 1;
m[i][j] = INT_MAX;
for (k = i; k <= j - 1; k++) {
// q = cost/scalar multiplications
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][n - 1];
}
int main()
{
int arr[] = { 1, 2, 3, 4};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Minimum number of multiplications is %d ",
MatrixChainOrder(arr, size));
getchar();
return 0;
}

Output:
Minimum number of multiplications is 18

Discussion:
We solved the program using dynamic programming strategy. The problem is not
actually to perform the multiplications, but merely to decide the sequence of the
matrix multiplications involved.
The approach to the was Solution :
 Taking the sequence of matrices and separating it into two subsequences.
 Finding the minimum cost of multiplying out each subsequence.
 Adding these costs together, and adding in the cost of multiplying the two result
matrices.
 Doing this for each possible position at which the sequence of matrices can be
split, and taking the minimum over all of them.

You might also like