Skip to content

Commit

Permalink
Merge branch 'master' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
ambujraj authored Oct 19, 2018
2 parents 59b9618 + 4d7ed2e commit bb3d896
Show file tree
Hide file tree
Showing 193 changed files with 10,699 additions and 35 deletions.
28 changes: 28 additions & 0 deletions 1-0_knapsack_problem/1-0_knapsak_problem.py
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()
32 changes: 32 additions & 0 deletions 8 queen/queen.c
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;

}
4 changes: 4 additions & 0 deletions BigInteger_InJava/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public static void main(String[] args) {

System.out.println("GCD value : " + bigNum.gcd(BigInteger.valueOf(temp)));

System.out.println("ABS value : " + bigNum.abs(BigInteger.valueOf(temp)));

System.out.println("Divided and Remainder value : " + bigNum.divideAndRemainder(BigInteger.valueOf(temp)));


}
}
12 changes: 12 additions & 0 deletions Binary-Search-c++/binary_search_using_recursion.cpp
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;
}
44 changes: 44 additions & 0 deletions Binary-Search-c++/binarysearch_basic.cpp
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;
}
Loading

0 comments on commit bb3d896

Please sign in to comment.