Skip to main content

Posts

Showing posts with the label Algorithm

Knapsack Algorithm Shortcut Method

The knapsack problem or rucksack 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. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. The main goal of Knapsack  is: You have a knapsack that has capacity (weight) W. You have several items I1,…,In. Each item Ij has a weight wj and a benefit bj.You want to place a certain number of copies of each item Ij in the knapsack so that: The knapsack weight capacity is not exceeded and The total benefit is maximal. There is a Shortcut Method  for Knapsack Algorithm , see the video:  

C++ Code of Bubble Sort

Bubble sort, often incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. Here is the C++ implantation of Bubble sort #include <iostream> using namespace std; int main() { int i,j, n; cout<< "How much number you will sort?" << endl; cin>>n; int a[n]; for(int x=1; x<=n;x++) { cout<< "Enter "<< " Number " << x <<": " << endl; cin>>a[x]; } for(int i=1; i<=n;i++) for(int j=i+1; j<=n;j++) { if (a[i]>a[j]){ int temp; temp = a[i]; a[i]=a[j]; a[j]=temp; } } cout<< "Sorted numbers are:" << endl; for(int x=1; x<=n;x++) { cout<< a[x] << ...

Algorithm and its Runtime Analysis

Algorithm: An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values as output. Simple  Algorithm Run-time algorithm specialization  is a methodology for creating efficient algorithms for costly computation tasks of certain kinds. The methodology originates in the field of  automated theorem proving  and, more specifically, in the Vampire theorem prover project. One of the most important aspects of an algorithm is how fast it is. It is often easy to come up with an algorithm to solve a problem, but if the algorithm is too slow, it's back to the drawing board. Since the exact speed of an algorithm depends on where the algorithm is run, as well as the exact details of its implementation, computer scientists typically talk about the runtime relative to the size of the input. Approximate completion time for algorithms, N = 100 O(Log(N)) 10 -7  seconds O(...