Intro to Algorithms

Intro to Algorithms

Intro to Algorithms

What is an algorithm?

A step-by-step list of directions to follow to solve a problem

Intro to Algorithms

Where do we start?

1. Establish the rules of the problem

    a. What are the inputs and output? 

    b. What are the constraints?

Intro to Algorithms

Establish the rules

Example: Find the lowest value in a list

Q: What is the format of the list (input)?

A: An array

Q: What format should the output be?

A: A single number

Q: What types of data does the list contain?

A: Integers

Q: Does it include negative integers?

A: Yes

Intro to Algorithms

Establish the rules

Example: Find the lowest value in a list

Q: What is the format of the list (input)?

A: An array

Q: What format should the output be?

A: A single number

Q: What types of data does the list contain?

A: Integers

Q: Does it include negative integers?

A: Yes

INPUT

Intro to Algorithms

Establish the rules

Example: Find the lowest value in a list

Q: What is the format of the list (input)?

A: An array

Q: What format should the output be?

A: A single number

Q: What types of data does the list contain?

A: Integers

Q: Does it include negative integers?

A: Yes

OUTPUT

CONSTRAINTS

Intro to Algorithms

Where do we start?

1. Establish the rules of the problem

    a. What are the inputs and output? 

    b. What are the constraints?

2. If using TDD, write a test that would verify a solution

    a. If this input => expect this output

3. Explore the problem and consider different techniques

    a. consider edge cases

    b. identify common patterns

4. Generate a simple plan that should solve the problem

    a. BEFORE you code, you must be able to explain the
        solution in plain language

Intro to Algorithms

Decide on a plan of action

1. Assume the first number is the lowest

2. Save that number as the 'lowest value'

3. Look at each number in the list

4. Compare it to the 'lowest value'

5. If it is lower, reassign the 'lowest value'

6. Else, discard it and continue looking

Intro to Algorithms

Pseudocode!

// provided some list of numbers

// take the first number, call it 'currentLowest'

// consider each number in the list

    // if the given number is lower than 'currentLowest'

        // assign 'currentLowest' to the given number

// return the 'currentLowest' as the result

Intro to Algorithms

Translate your steps into real code

public int findLowest( int[] list ) {

    int currentLowest = list[0];

    for (int i = 0; i < list.length; i++) {

        if (currentLowest > list[i]) {

            currentLowest = list[i];

        }

    } 

    return currentLowest;

}

// provided a list of numbers 

// take the first num, call it 'currentLowest'

// consider each num in the list

    // if a given num is lower than 'currentLowest'

        // assign 'currentLowest' to the given num

 

 

// return the 'currentLowest' as the result

Intro to Algorithms

By moringaschool

Intro to Algorithms

  • 442