Algorithm Design Techniques
Algorithm Design Techniques
Techniques
Tayaba Anjum
Algorithm Design Techniques
• Brute First
• Linear search, Selection Sort, Bubble Sort, Knapsack Problem
• Divide and Conquer
• Quick Sort, Merge Sort
• Decrease and conquer
Brute Force Algorithms
• Brute Force Algorithms refers to a programming style that does not
include any shortcuts to improve performance, but instead relies on
sheer computing power to try all possibilities until the solution to a
problem is found.
Examples: Linear Search
Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering
ascending order) from unsorted part and putting it at the beginning.
Bubble Sort
Bubble Sort compares each pair of array element unless the whole array is completely sorted in an
ascending order. ... If no swap has occurred, i.e. the array requires no more processing to
be sorted, it will come out of the loop.
Knapsack Problem
Knapsack Problem
Knapsack Problem
int knapSack(int W, int[] weights, int[] val, int n) {
if (n == 0 || W == 0)
return 0;
/*
If weight of the nth item is more than Knapsack
capacity W,then this item cannot be included
in the optimal solution
*/
if (weights[n] > W)
return knapSack(W, weights, val, n - 1);
/* Consider two cases, including item and excluding item.*/
else return max((val[n]+ knapSack(W - weights[n], weights, val, n - 1)),(knapSack(W, weights, val, n - 1)));
}
Time complexity?