forked from sandesh-theMayGuy/DSA-in-C-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
252 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <stdio.h> | ||
|
||
// Function to perform linear search | ||
int linearSearch(int arr[], int n, int target) { | ||
for (int i = 0; i < n; i++) { | ||
if (arr[i] == target) { | ||
return i; // Return the index where the target is found | ||
} | ||
} | ||
return -1; // Return -1 if the target is not found in the array | ||
} | ||
|
||
int main() { | ||
int n; | ||
|
||
printf("Enter the number of elements in the array: "); | ||
scanf("%d", &n); | ||
|
||
int arr[n]; // Declare an array of size 'n' | ||
|
||
printf("Enter %d elements:\n", n); | ||
for (int i = 0; i < n; i++) { | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
int target; | ||
printf("Enter the element to search for: "); | ||
scanf("%d", &target); | ||
|
||
int result = linearSearch(arr, n, target); | ||
|
||
if (result != -1) { | ||
printf("Element %d found at index %d.\n", target, result); | ||
} else { | ||
printf("Element %d not found in the array.\n", target); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//FRACTIONAL KNAPSACK | ||
|
||
// greedy approach to solve the knapsack problem | ||
|
||
#include<iostream> | ||
using namespace std; | ||
|
||
class greedy{ | ||
public: | ||
int id; | ||
float weight; | ||
float profit; | ||
float pw; | ||
float amount; | ||
void ratio(){ | ||
pw=profit/weight; | ||
} | ||
|
||
|
||
}; | ||
|
||
int main(){ | ||
int n; | ||
float cap,totalm=0,totalp=0; | ||
cout<<"enter the no.of objects:"; | ||
cin>>n; | ||
cout<<"enter the bag capacity:"; | ||
cin>>cap; | ||
|
||
greedy obj[n]; //creating an array of object of size n | ||
for(int i=0;i<n;i++){ | ||
obj[i].id=i+1; | ||
obj[i].amount=0; | ||
cout<<"enter the weight of "<<i+1<<" : "; | ||
cin>>obj[i].weight; | ||
cout<<"enter the profit of "<<i+1<<" : "; | ||
cin>>obj[i].profit; | ||
} | ||
for(int i=0;i<n;i++){ //finding the profit to weight ratio of each object | ||
obj[i].ratio(); | ||
} | ||
|
||
for(int i=0;i<n-1;i++){ //Sorting the objects in descending order according to profit to weight ratio | ||
for(int j=0;j<n-1-i;j++){ | ||
if(obj[j].pw < obj[j+1].pw){ | ||
greedy temp=obj[j]; | ||
obj[j]=obj[j+1]; | ||
obj[j+1]=temp; | ||
} | ||
} | ||
} | ||
|
||
int i=0; | ||
while(totalm<cap){ //Looping until totalweight of selected items doesn't become equal to capacity of bag | ||
if (obj[i].weight <= (cap - totalm)) | ||
{ | ||
obj[i].amount = 1; //if object fits in the bag with remaining capacity then it is taken as whole | ||
totalm = totalm + obj[i].weight; | ||
totalp = totalp + obj[i].profit; | ||
} | ||
else if (obj[i].weight > (cap - totalm)) //else we take fraction of it | ||
{ | ||
obj[i].amount = (cap - totalm) / (obj[i].weight); | ||
totalm = totalm + (cap - totalm); | ||
totalp = totalp + (obj[i].profit) * (obj[i].amount); | ||
} | ||
i++; | ||
} | ||
|
||
cout<<"\n\nfinal table is::\n"<<"id "<<"fract "<<"weight "<<"profit "<<"pw ratio\n"; | ||
for(int i=0;i<n;i++){ | ||
cout<<obj[i].id<<" "<<obj[i].amount<<" "; | ||
cout<<obj[i].weight<<" "; | ||
cout<<obj[i].profit<<" "; | ||
cout<<obj[i].pw<<endl; | ||
} | ||
cout<<endl<<"total profit is::"<<totalp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Knapsack Algorithm Dynamic Approach | ||
*/ | ||
|
||
|
||
|
||
#include<iostream> | ||
#include<algorithm> | ||
using namespace std; | ||
|
||
class dclass{ | ||
public: | ||
int w; //w stands for weight of object | ||
int p; //p stands for profit of object | ||
int id; //id is used to see wether object is used or not where intially its 0 means not used | ||
}; | ||
|
||
int main(){ | ||
int n,cap; | ||
cout<<"enter the no of objects:"; | ||
cin>>n; | ||
cout<<"enter the capacity of bag:"; | ||
cin>>cap; | ||
|
||
dclass obj[n+1]; //declaring array of objects and matrix | ||
|
||
int mat[n+1][cap+1]; //initializing matrix with rows equal to no. of objects and columns equal to capacity of bag | ||
|
||
obj[0].w=0; //here we initialize attributes of 0th object which is considered by us for easy calculations | ||
obj[0].p=0; | ||
obj[0].id=0; | ||
|
||
for(int i=1;i<n+1;i++){ | ||
obj[i].id=0; //for putting id=0 for all objects to denote that no object is used till now; | ||
cout<<"enter the weight of "<<i<<":"; | ||
cin>>obj[i].w; | ||
cout<<"enter the profit of "<<i<<":"; | ||
cin>>obj[i].p; | ||
} | ||
|
||
for(int i=0;i<n;i++){ //sorting the objects in ascending order of weights | ||
for(int j=0;j<n-i;j++){ | ||
if(obj[j].w >obj[j+1].w){ | ||
dclass temp=obj[j]; | ||
obj[j]=obj[j+1]; | ||
obj[j+1]=temp; | ||
} | ||
} | ||
} | ||
|
||
for(int j=0;j<cap+1;j++){ //initializing first row and first column as 0 because if capacity of bag is 0 profit will be 0 and if 0 objects are selected profit will be 0 | ||
if(j<n+1){ | ||
mat[j][0]=0; | ||
} | ||
mat[0][j]=0; | ||
} | ||
|
||
for(int i=1;i<n+1;i++){ | ||
for(int j=1;j<cap+1;j++){ //finding values of matrix | ||
int p= mat[i-1][j]; | ||
int l= j-obj[i].w; | ||
int q=0; | ||
if(l<0){ | ||
q=0; | ||
} | ||
else{ | ||
q= mat[i-1][l]+obj[i].p; | ||
} | ||
mat[i][j]=max(p,q); | ||
} | ||
} | ||
|
||
|
||
cout<<endl<<"matrix is::\n"; | ||
for(int i=0;i<n+1;i++){ //print matrix | ||
for(int j=0;j<cap+1;j++){ | ||
cout<<mat[i][j]<<" "; | ||
}cout<<endl; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
int c=n,d=cap; //bottom up approach used to find which object are selected | ||
int finalp=mat[c][d]; | ||
while(finalp!=0){ | ||
while (1) | ||
{ | ||
if (mat[c - 1][d] == finalp) | ||
{ | ||
c--; | ||
} | ||
else | ||
{ | ||
obj[c].id = 1; | ||
finalp = finalp - obj[c].p; | ||
c--; | ||
break; | ||
} | ||
} | ||
for (int i = 0; i < cap + 1; i++) | ||
{ | ||
if (mat[c][i] == finalp) | ||
{ | ||
d = i; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
cout<<"\nweight "<<"profit "<<"remark \n"; | ||
for(int i=1;i<n+1;i++){ | ||
cout<<" "<<obj[i].w<<" "<<obj[i].p; | ||
if(obj[i].id==1) | ||
cout<<" Selected"; | ||
else | ||
cout<<" -"; | ||
cout<<endl; | ||
} | ||
|
||
cout<<"\n\ntotal profit is::"<<mat[n][cap]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters