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

Algorithm Design Techniques

The document discusses various algorithm design techniques including brute force, divide and conquer, and decrease and conquer. Brute force algorithms try all possibilities to find a solution, examples given are linear search, selection sort, and bubble sort. Divide and conquer algorithms break a problem into subproblems, merge sort and quicksort are provided as examples. Decrease and conquer reduces the problem size recursively until a base case is reached, insertion sort and binary search are given as examples that decrease the problem size by a constant amount each iteration.

Uploaded by

Muhammad hanzla
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Algorithm Design Techniques

The document discusses various algorithm design techniques including brute force, divide and conquer, and decrease and conquer. Brute force algorithms try all possibilities to find a solution, examples given are linear search, selection sort, and bubble sort. Divide and conquer algorithms break a problem into subproblems, merge sort and quicksort are provided as examples. Decrease and conquer reduces the problem size recursively until a base case is reached, insertion sort and binary search are given as examples that decrease the problem size by a constant amount each iteration.

Uploaded by

Muhammad hanzla
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Algorithm Design

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)));
    }

 int[] val = {60, 100, 120};


 int[] wt = {10, 20, 30};
 int W = 5
knapSack(W, weights, val, n )
Divide and Conquer
• A divide-and-conquer algorithm works by recursively breaking down
a problem into two or more sub-problems of the same or related
type, until these become simple enough to be solved directly.
Merge Sort
Merge Sort is a divide and conquer algorithm. It works by continually splitting a list in half until both halves
are sorted, then the operation merge is performed to combine two lists into one sorted new list.
Quick Sort
Quick sort is one of the most famous sorting algorithms based on divide and conquers strategy which results in
an O(n log n) complexity. So, the algorithm starts by picking a single item which is called pivot and moving all
smaller items before it, while all greater elements in the later portion of the list.
Decrease-and-Conquer
1. Reduce problem instance to smaller instance of the same problem
2. Solve smaller instance
3. Extend solution of smaller instance to obtain solution to original
instance

• Can be implemented either top-down or bottom-up


• Also referred to as inductive or incremental approach
Types of Decrease and Conquer
• Decrease by a constant (usually by 1):
• Insertion Sort

• Decrease by a constant factor (usually by half)


• Binary Search
• Fake Coin Problem
• Variable-size decrease
• Searching in Binary Search Tree
• Interpolation Search
Insertion Sort
Fake coin Algorithm
Searching in Binary Search Tree
Interpolation Search
Binary Search always checks the value at middle index. But,
interpolation search may check at different locations based on the
value of element being searched.
Example: Interpolation Search

Time complexity?

You might also like