You are on page 1of 16

Practical File

of

Design & Analysis of Algorithms Lab


(PCC-CS-407)

submitted in partial fulfillment of the requirement for the award of degree of

Bachelor of Technology (B.Tech)


in
Computer engineering(hindi)
by
Shobhit Kumar
(21001050018)

Under the guidance of


Ms.Disha
Assistant Professor

Department of Computer Engineering

J. C. BOSE UNIVERSITY OF SCIENCE & TECHNOLOGY, YMCA


SECTOR-6 FARIDABAD
HARYANA-121006
Given a sorted array of n elements, possibly with duplicates, find the number of
occurrences of the target element.

Code:
#include <stdio.h>

int count(int arr[],int n,int x)


{
int i,count =0;
i=binary(arr,10,x);
while(i>=0)
{

i f(arr[i]==x)

i--;

else

break ;

while(i<=n)

if(arr[i+1]==x)
{
i++;
count++;
}
else
break ;
}
return count;
}

int binary(int arr[],int n,int x)


{
int mid,l=0,h=n;
while(h>l)
{
mid=(h+l)/2;
if(arr[mid]==x)
return mid;
else if(arr[mid]>x)
h=mid;
else
l=mid;

}
1. }

int main()
{
int arr[10],i,a,x;
printf("Enter the Array:");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
printf("Enter number duplicate count");
scanf("%d",&a);
x=count(arr,10,a);
printf(" here count %d",x);

return 0;
}

Output :

Time Complexity :
Best case: Ω(1)
Average case: θ(log n)
Worst case: O(n)
2. Given a 0-indexed integer array nums, find a peak element, and return its index. If
the array contains multiple peaks, return the index to any of the peaks.
*A peak element is an element that is strictly greater than its neighbors

Code :
#include <stdio.h>

int peak(int arr[],int n)

{
int i;
for(i=0;i<n;i++)
{
if((arr[i+1]>arr[i])&&(arr[i+1]>arr[i+2]))
return i+1;
}
return -1;
}int main()
{
int nums[10],i,n;
printf("Enter the array size\n");
scanf("%d",&n);
printf("Enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&nums[i]);
i=peak(nums,n);
printf("Peak at %d",i);
return 0;
} Output :

Time Complexity :
case: Ω(1)
Average case: θ(n)
Worst case: O(n)
3. Given an array arr of positive integers sorted in a strictly increasing order, and an
integer k.
Write a function to return the kth positive integer that is missing from this array.

Code :
#include <stdio.h>

void missing(int arr[],int n,int ki)

int i,miss[10],j,k;

for(i=0,j=0,k=0;i<n;j++,k++)

if(arr[i]==j)

i++;

k--;

else

miss[k]=j;

for(i=0;i<ki;i++)

printf("%d ",miss[i]);

int main()

int nums[10],i,n,k;

printf("Enter the array size\n");

scanf("%d",&n);

printf("Enter the array elements");

for(i=0;i<n;i++)
scanf("%d",&nums[i]);

printf("Enter the value of k");

scanf("%d",&k);

missing(nums,n,k);

return 0;

Output :

Time Complexity :
Best case: Ω(n)
Average case: θ(n)
Worst case: O(n)
4. Given an integer array nums and an integer k, return the kth largest element in the
array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
You must solve it in O(n) time complexity

Code :
#include <stdio.h>

void swap(int *a, int *b) {

int t = *a;

*a = *b;

*b = t;

int partition(int array[], int low, int high) {

int pivot = array[high];

int i = (low - 1);

for (int j = low; j < high; j++) {

if (array[j] <= pivot) {

i++;

swap(&array[i], &array[j]);

swap(&array[i + 1], &array[high]);

return (i + 1);

int quickSort(int array[], int low, int high,int k) {

if (low < high) {

int pi = partition(array, low, high);

if(pi==k)

return pi;

else if(pi>k)

quickSort(array, low, pi - 1,k);

else
quickSort(array, pi + 1, high,k);

}}

int main() {

int data[10],k,i,n;

printf("Enter the Size");

scanf("%d",&n);

printf("Enter Array element");

for(i=0;i<n;i++)

scanf("%d",&data[i]);

printf("Enter the k value");

scanf("%d",&k);

i= quickSort(data, 0, n - 1,n-k);

printf("Element is %d ",data[i]);

return 0;

Output :

Time Complexity :
Best case: Ω(1)
Average case: θ(n)
Worst case: O(nlogn)
5. Wiggle Sort 1:
Given an integer array nums, reorder it such that nums[0] <=nums[1] >=nums[2]
<=nums[3]....

Code :
#include <stdio.h>

void swap(int *a, int *b) {

int t = *a;

*a = *b;

*b = t; }

int partition(int array[], int low, int high) {

int pivot = array[high];

int i = (low - 1);

for (int j = low; j < high; j++) {

if (array[j] <= pivot) {

i++;

swap(&array[i], &array[j]);

}}

swap(&array[i + 1], &array[high]);

return (i + 1);

void quickSort(int array[], int low, int high) {

if (low < high) {

int pi = partition(array, low, high);

quickSort(array, low, pi - 1);

quickSort(array, pi + 1, high);

}}

void wiggle(int arr[],int low,int high) {

int i;

quickSort(arr, low, high);


for(i=1;i<high;i=i+2)

swap(&arr[i],&arr[i+1]);

int main() {

int data[10],i,n;

printf("Enter the Size");

scanf("%d",&n);

printf("Enter Array element");

for(i=0;i<n;i++)

scanf("%d",&data[i]);

wiggle(data, 0, n-1);

printf("Array is :\n");

for(i=0;i<n;i++)

printf("%d ",data[i]);

return 0;

Output :

Time Complexity :
Best case: Ω(nlogn)
Average case: θ(nlogn)
Worst case: O(nlogn)
6. Given the weights and values of N items, in the form of {value, weight} put these
items in a knapsack of capacity W to get the maximum total value in the knapsack.
In Fractional Knapsack, we can break items for maximizing the total value of the
knapsack

# include<stdio.h>

void knapsack(int n, float weight[], float profit[], float capacity) {

float x[20], tp = 0;

int i, j, u

u = capacity;

for (i = 0; i < n; i++)

x[i] = 0.0;

for (i = 0; i < n; i++) {

if (weight[i] > u)

break;

else {

x[i] = 1.0;

tp = tp + profit[i];

u = u - weight[i]; } }

if (i < n)

x[i] = u / weight[i];

tp = tp + (x[i] * profit[i]);

printf("The result vector is:- ");

for (i = 0; i < n; i++)

printf("%f\t", x[i]);

printf("Maximum profit is:- %f", tp); }

int main() {

float weight[20], profit[20], capacity;

int num, i, j;

float ratio[20], temp;

printf("Enter the no. of objects:- ");


scanf("%d", &num);

printf("Enter the wts and profits of each object:- ");

for (i = 0; i < num; i++) {

scanf("%f %f", &weight[i], &profit[i]); }

printf("Enter the capacityacity of knapsack:- ");

scanf("%f", &capacity);

for (i = 0; i < num; i++) {

ratio[i] = profit[i] / weight[i]; }

for (i = 0; i < num; i++) {

for (j = i + 1; j < num; j++) {

if (ratio[i] < ratio[j]) {

temp = ratio[j];

ratio[j] = ratio[i];

ratio[i] = temp;

temp = weight[j];

weight[j] = weight[i];

weight[i] = temp;

temp = profit[j];

profit[j] = profit[i];

profit[i] = temp; } }}

knapsack(num, weight, profit, capacity);

return(0); }
7: Given an array of jobs where every job has a deadline and associated
profit if the job is finished before the deadline. It is also given that every job
takes a single unit of time, so the minimum possible deadline for any job is 1.
Maximize the total profit if only one job can be scheduled at a time .

#include <stdbool.h>

#include <stdio.h>

#include <stdlib.h>

typedef struct Job {

char id;

int dead;

int profit;

} Job;

int compare(const void* a, const void* b){

Job* temp1 = (Job*)a;

Job* temp2 = (Job*)b;

return (temp2->profit - temp1->profit); }

int min(int num1, int num2){

return (num1 > num2) ? num2 : num1; }

void printJobScheduling(Job arr[], int n){

qsort(arr, n, sizeof(Job), compare);

int result[n];

bool slot[n];

for (int i = 0; i < n; i++)

slot[i] = false;

for (int i = 0; i < n; i++) {

for (int j = min(n, arr[i].dead) - 1; j >= 0; j--) {

if (slot[j] == false) {

result[j] = i;

slot[j] = true;

break;} } }

for (int i = 0; i < n; i++)


if (slot[i])

printf("%c ", arr[result[i]].id); }

int main() {

Job arr[] = { { 'a', 2, 100 },

{ 'b', 1, 19 },

{ 'c', 2, 27 },

{ 'd', 1, 25 },

{ 'e', 3, 15 } };

int n = sizeof(arr) / sizeof(arr[0]);

printf("Following is maximum profit sequence of jobs \n");

printJobScheduling(arr, n);

return 0;

OUTPUT

Time Complexity: O(N ) 2

Space Complexity : O(N)


11:.WAP to implement 0/1 knapsack problem using greedy approach.
Code:

#include <stdio.h>

int max(int a,int b){


return (a>b)? a:b; }

int knapsack(int weight[],int profit[],int W,int n){


if(n==0 || W==0){
return 0; }
if(weight[n-1]>W){
return knapsack(weight,profit, W,n-1); }
else{
return max(profit[n-1]+knapsack(weight,profit,W-weight[n-1],n-1),knapsack(weight,profit,W,n-1)); }
}

int main() {
int n;
printf("enter the number of items\n");
scanf("%d",&n);
int W;
printf("enter the capacity of the Knapsack");
scanf("%d",&W);
int weight[n];
int profit[n];
for(int i=0;i<n;i++){
printf("enter the weight and profit for items I%d",i+1);
scanf("%d%d",&weight[i],&profit[i]); }
int ans=knapsack(weight,profit,W,n);
printf("%d",ans);
return 0; }

Time complexity :
Worst Case: 0(2^N)

Space Complexity: 0(N)


12. Given an integers array nums .find the subarray with the largest sum and
return its value.
nums={-2,1,-3,4,-1,2,1,-5,4}

#include <iostream>
#include<limits.h>
using namespace std;

int main() {
int nums[9]={-2,1,-3,4,-1,2,1,-5,4};
int maxi=INT_MIN;
for(int i=0;i<9;i++){
int sum=0;
for(int j=i;j<9;j++){
sum=sum+nums[j];
maxi=max(maxi,sum);
}
}
cout<<"maximum subarray sum is "<<maxi<<endl;
}

Time complexity:
Worst case: O(n*n)

You might also like