forked from ambujraj/hacktoberfest2018
-
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
193 changed files
with
10,699 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" Knapsack problem by dynamic programming """ | ||
weight = [1,3,4,5] | ||
value = [1,4,5,7] | ||
n = len(weight)+1 | ||
max_wt = 7 | ||
v = [[0 for j in range(max_wt+1)] for i in range(n)] | ||
|
||
def print_items(i, j): | ||
if i>0 and j>0 : | ||
if v[i][j] != v[i-1][j] : | ||
print(str(weight[i-1])+"\t\t\t"+str(value[i-1])) | ||
print_items(i-1, v[i-1].index(v[i][j]-value[i-1])) | ||
else : | ||
print_items(i-1, j) | ||
|
||
|
||
def knapsack(): | ||
for i in range(1, n): | ||
for j in range(1, max_wt+1): | ||
if j-weight[i-1] >= 0 : | ||
v[i][j] = max([v[i-1][j], v[i-1][j-weight[i-1]]+value[i-1]]) | ||
else : | ||
v[i][j] = v[i-1][j] | ||
print("Selected Weight\t\tValue") | ||
print_items(n-1, max_wt) | ||
print("\nMaximum Value : "+str(v[n-1][max_wt])) | ||
|
||
knapsack() |
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,32 @@ | ||
#include<stdio.h> | ||
|
||
int main(){ | ||
int st1[8]={2,4,7,4,8,5,5,2},st2[8]={3,2,7,5,2,4,1,1},st3[8]={2,4,4,1,5,1,2,4},st4[8]={3,2,5,4,3,2,1,3}; | ||
int i,st1f,st2f,st3f,st4f; | ||
/* printf("Enter st1: "); | ||
for(i=0;i<8;i++) | ||
scanf("%d",&st3[i]); | ||
*/ | ||
printf("ST1 : "); | ||
for(i=0;i<8;i++){ | ||
printf("%d ",st1[i]); | ||
} | ||
printf("\nST2 : "); | ||
for(i=0;i<8;i++){ | ||
printf("%d ",st1[i]); | ||
} | ||
printf("\nST3 : "); | ||
for(i=0;i<8;i++){ | ||
printf("%d ",st1[i]); | ||
} | ||
printf("\nST4 : "); | ||
for(i=0;i<8;i++){ | ||
printf("%d ",st1[i]); | ||
} | ||
printf("\nEnter fitness functions of st1, st2, st3 and st4 : "); | ||
scanf("%d%d%d%d",&st1f,&st2f,&st3f,&st4f); | ||
printf("Fitness function :\n st1=%d\t st2=%d\t st3=%d\t st4=%d ",st1f,st2f,st3f,st4f); | ||
|
||
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
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,12 @@ | ||
int binary_search(int sorted_list[], int low, int high, int element) | ||
{ | ||
if (high < low) | ||
return -1; | ||
int middle = low + (high - low)/2; | ||
if (element < sorted_list[middle]) | ||
return binary_search(sorted_list, low, middle-1, element); | ||
else if (element > sorted_list[middle]) | ||
return binary_search(sorted_list, middle+1, high, element); | ||
else | ||
return middle; | ||
} |
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,44 @@ | ||
#include<bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int n, i, arr[50], el, low, high, mid; | ||
cout << "Enter size of list:"; | ||
cin >> n; | ||
cout << "Enter elements :"; | ||
for (i=0; i<n; i++) | ||
{ | ||
cin >> arr[i]; | ||
} | ||
cout << "Enter element to be searched:"; | ||
cin >> el; | ||
low = 0; | ||
high = n-1; | ||
mid = (low+high)/2; | ||
while (low <= high) | ||
{ | ||
if(arr[mid] < el) | ||
{ | ||
low = mid + 1; | ||
|
||
} | ||
else if(arr[mid] == el) | ||
{ | ||
cout << el <<" found at location " << mid <<"\n"; | ||
break; | ||
} | ||
else | ||
{ | ||
high = mid - 1; | ||
} | ||
mid = (low + high)/2; | ||
} | ||
if(low > high) | ||
{ | ||
cout << "Not found! " << el << " is not present in the list."; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.