Algorithm Study Notes 1----Introduction to Algorithms

1.1 An algorithm is a set of instructions to accomplish a task, and any piece of code can be considered code.

1.2 Binary search:

in an ordered list of elements (must be ordered). If the element to find is contained in the list, returns its position; otherwise, returns null.

How it works: Find from the middle of the list each time, excluding half of the numbers each time.

Example: Find a number I want in 1-100. Start with 50. If it is small, find the number 75 between 50 and 100. If it is large, find the number 25 between 1 and 50; and so on, reduce the number by half each time, and finally find the desired number.

100-->50-->25-->13-->7-->4-->2-->1   7步

1.3 Simple lookup. It's just a silly search, one by one in order, without missing any number.

Simple search takes n steps, while binary search takes log 2 n steps.

1.4 Python code to implement binary search

def binary_search(list, item):
    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) / 2
        guess = list[mid]
        if guess == item:
            return mid
        elif guess > item:
            high = mid - 1
        else:
            low = mid + 1
    return None

my_list = [1, 3, 5, 7, 9]

print(binary_search(my_list, 1))
print(binary_search(my_list, -1))

Exercise. (1). Assuming there is an ordered list of 128 names, how many steps are required to use binary search?

                    log 2 128 = 7 steps

        (2). How many steps will it take to double the above list?

                log 2 256 = 8 steps

1.5 Runtime.

Design programs, in general, should choose the most efficient algorithm to minimize running time or footprint.

Simple lookups have the same number of lookups as the list length, which is called linear time.

Binary search runs in logarithmic time.

1.6. Big O Notation

The running time of the algorithm increases at different rates.

Big O notation indicates how fast an algorithm is, that is, how fast the algorithm runs. Simple lookup runs in O(n). Binary search runs in O(log n).

Big O notation indicates the worst-case running time.

Some common Big O runtimes:

O(log n), also known as logarithmic time, such algorithms include binary search.

O(n), also called linear time, such algorithms include simple lookups.

O(n * log n), such algorithms include quicksort - a faster sorting algorithm.

O(n2), such algorithms include selection sort - a slower sorting algorithm.

O(n!), an algorithm that has always been very slow, similar to the solution algorithm for the traveling salesman problem.

practise:

(1). Look up the phone number by name in the phone book.

O (logn)

(2). Find someone according to the phone number in the phone book. (Hint: you have to look up the entire phone book.)

O(n)

(3). Read the phone number of everyone in the phone book.

O(n)

(4) Read the phone numbers of people whose names begin with A in the phone book. This is a trickier question, and it involves concepts from Chapter 4. The answer may surprise you!

O(n), big-O notation does not account for multiplying, dividing, adding or subtracting

1.7. Summary

  • Binary search is much faster than simple search
  • O(log n) is faster than O(n). The more elements that need to be searched, the faster the former is faster than the latter.
  • Algorithm running time is not measured in seconds
  • Algorithm running time is measured in terms of its speedup
  • Algorithm running time is expressed in big O notation




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324760279&siteId=291194637