0% found this document useful (0 votes)
77 views

Knapsack Problem: Assignment - 03

The document discusses the knapsack problem and quicksort algorithms. It provides details on the knapsack problem, including that it involves determining the optimal selection of items with weights and values that fit within a maximum weight capacity while maximizing the total value. An algorithm for solving the knapsack problem is presented with time complexity of O(nW). Quicksort is also summarized as a divide and conquer algorithm that selects a pivot element, partitions elements into subarrays based on being less than or greater than the pivot, and recursively sorts the subarrays, with time complexities ranging from O(n log n) to O(n^2).

Uploaded by

enayat atal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Knapsack Problem: Assignment - 03

The document discusses the knapsack problem and quicksort algorithms. It provides details on the knapsack problem, including that it involves determining the optimal selection of items with weights and values that fit within a maximum weight capacity while maximizing the total value. An algorithm for solving the knapsack problem is presented with time complexity of O(nW). Quicksort is also summarized as a divide and conquer algorithm that selects a pivot element, partitions elements into subarrays based on being less than or greater than the pivot, and recursively sorts the subarrays, with time complexities ranging from O(n log n) to O(n^2).

Uploaded by

enayat atal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: Enayatullah Atal

ID: K1S20MCS0003 Date: 4/12/2020


Class: 1st Semester of MCS-BU Subj: Advanced Algorithm
D&A

Assignment_03
1. Make a review of Knapsack problem?
2. Quick sort program, review also these one?

Knapsack problem

The knapsack problem is a problem in combinatorial optimization: Given a set of


items, each with a weight and a value, determine the number of each item to
include in a collection so that the total weight is less than or equal to a given limit
and the total value is as large as possible
The dynamic programming algorithm for the knapsack problem has a time
complexity of O(nW) where n is the number of items and W is the capacity of
the knapsack.
In 0-1 knapsack problem, a set of items are given, each with a weight and a value.
We need to determine the number of each item to include in a collection so that the
total weight is less than or equal to the given limit and the total value is large as
possible.
Algorithm for solving knapsack problem
 Begin
 Input set of items each with a weight and a value
Set knapsack capacity
 Create a function that returns maximum of two integers.
 Create a function which returns the maximum value that can be put in a knapsack
of capacity W
int knapSack(int W, int w[], int v[], int n)
int i, wt;
int K[n + 1][W + 1]
 for i = 0 to n
 for wt = 0 to W
if (i == 0 or wt == 0)
Do K[i][wt] = 0
else if (w[i - 1] <= wt)
 Compute: K[i][wt] = max(v[i - 1] + K[i - 1][wt - w[i - 1]], K[i -1][wt])
else
K[i][wt] = K[i - 1][wt]
return K[n][W]
Call the function and print.
 End

Quicksort
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element
from the array and partitioning the other elements into two sub-arrays, according to
whether they are less than or greater than the pivot. The sub-arrays are
then sorted recursively.
The time complexity of Quicksort is O(n log n) in the best case, O(n log n) in the
average case, and O(n^2) in the worst case. But because it has the best
performance in the average case for most inputs, Quicksort is generally considered
the “fastest” sorting algorithm.

You might also like