Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Fractional_Knapsack.js #245

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions Fractional_Knapsack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,42 @@ class Item {
this.profit = profit;
this.weight = weight;
}
}



function cmp(a, b) {
let r1 = a.profit / a.weight;
let r2 = b.profit / b.weight;
return r1 > r2;
getRatio() {
return this.profit / this.weight;
}
}

function compareItems(a, b) {
// Compare based on profit-to-weight ratio in descending order
return b.getRatio() - a.getRatio();
}

function fractionalKnapsack(W, arr) {
// Sorting Item on basis of ratio
arr.sort(cmp);

let finalvalue = 0.0;
function fractionalKnapsack(capacity, items) {
// Sort items based on the profit-to-weight ratio
items.sort(compareItems);

for (let i = 0; i < arr.length; i++) {


if (arr[i].weight <= W) {
W -= arr[i].weight;
finalvalue += arr[i].profit;
}
let totalProfit = 0.0;


else {
finalvalue += arr[i].profit * (W / arr[i].weight);
for (let i = 0; i < items.length; i++) {
if (items[i].weight <= capacity) {
// Take the whole item
capacity -= items[i].weight;
totalProfit += items[i].profit;
} else {
// Take the fraction of the item that fits
totalProfit += items[i].profit * (capacity / items[i].weight);
break;
}
}

return finalvalue;
return totalProfit;
}

let W = 50;
let arr = [new Item(60, 10), new Item(100, 20), new Item(120, 30)];
const capacity = 50;
const items = [new Item(60, 10), new Item(100, 20), new Item(120, 30)];

console.log(fractionalKnapsack(capacity, items));

console.log(fractionalKnapsack(W, arr));


19 changes: 19 additions & 0 deletions Selection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def selection_sort(arr):
n = len(arr)

for i in range(n):
# Find the minimum element in the remaining unsorted list
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j

# Swap the found minimum element with the first element
arr[i], arr[min_index] = arr[min_index], arr[i]

# Example usage
arr = [64, 34, 25, 12, 22, 11, 90]
selection_sort(arr)

print("Sorted array:")
print(arr)
10 changes: 7 additions & 3 deletions fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
def fibonacci(n):
# Initialize the first two Fibonacci numbers
a, b = 0, 1
# Continue generating numbers while the current number is less than 'n'
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
print(a, end=' ') # Print the current Fibonacci number
a, b = b, a + b # Update the values: 'a' becomes 'b', and 'b' becomes 'a+b'
print() # Newline after the loop ends

# Generate Fibonacci sequence up to 1000
fibonacci(1000)