Python development [Chapter 28]: algorithm, chapter 28 of python

Source: Internet
Author: User

Python development [Chapter 28]: algorithm, chapter 28 of python
Algorithm Basics

1. What is an algorithm?

Algorithm (Algorithm): a method for solving problems in a computing process

 

2. Review: Recursion

Recursive features:

  • Call itself
  • End Condition

Comparison of two important recursive functions:

# Def func3 (x): if x> 0: print (x) func3 (x-1) # func3 (5) #5 4 3 2 1 # from small to large def func4 (x): if x> 0: func4 (x-1) print (x) func4 (5) #1 2 3 4 5

  

3. Time Complexity

Time Complexity: One thing used to evaluate the algorithm running efficiency:

print('Hello World')            # 0(1)for i in range(n):                  # O(n)    print('Hello World')for i in range(n):                   # O(n^2)    for j in range(n):        print('Hello World')for i in range(n):                   # O(n^3)    for j in range(n):        for k in range(n):            print('Hello World')

The first prints a time complexity of O (1), the second prints n times, so the time complexity is O (n), and the third and fourth are the 3rd power of n and

So let's take a look at the time complexity in the following code?

# O (3) but O (1) print ('Hello World') print ('Hello python') print ('Hello Algorithm ') # Not O (n² + n) But O (n²) for I in range (n): print ('Hello World') for j in range (n ): print ('Hello World') # Not O (1/2n ²) But O (n2) for I in range (n): for j in range (I ): print ('Hello World ')

Let's look at the following code:

# Time complexity O (log2n) or O (logn) while n> 1: print (n) n = n // 2 # n = 64 output: #64 #32 #16 #8 #4 #2

Summary:

Time complexity is a sub-Statement (unit) used to estimate the algorithm running time ).

Generally, algorithms with low time complexity are faster than algorithms with low complexity.

Common time complexity (sort by Time)

  • O (1) <O (logn) <O (n) <O (nlogn) <O (n2) <O (n2logn) <O (n3)

Uncommon time complexity (just look at it)

  • O (n !) O (2n) O (nn )...

How can we determine the time complexity at a glance?

  • Process O (logn)
  • Several cycles are the complexity of n to the power.

 

4. spatial complexity

Space complexity: A formula used to evaluate the memory usage of an algorithm

 

List Search

List query: searches for a specified element from the list.

  • Input: List and elements to be searched
  • Output: Element subscript or element not found

1. Sequential search

Starts from the first element in the list and searches sequentially until it is found.

def linear_search(data_set, value):      for i in data_set:               if data_set[i] == value:                return i      return

The time complexity is O (n)

 

2. Binary Search

Starting from the data [0: n] of the candidate region in the ordered list, you can reduce the number of candidate regions by half by comparing the search value with the Intermediate Value of the candidate region.

# Authentic binary search data = [I for I in range (9000000)] import timedef cal_time (func): def inner (* args, ** kwargs): t1 = time. time () re = func (* args, ** kwargs) t2 = time. time () print ('time cost: ', func. _ name __, t2-t1) return re return inner @ cal_timedef bin_search (data_set, val): low = 0 high = len (data_set)-1 while low <= high: mid = (low + high) // 2 if data_set [mid] = val: return mid elif data_set [mid] <val: low = mid + 1 else: high = mid-1bin_search (data, 999) # Time cost: bin_search 0.0005002021789550781

The time complexity is O (logn). After running, it will find that the execution is fast. Next we will compare the binary search described by Alex.

# Alex's low version data = [I for I in range (9000000)] import timedef cal_time (func): def inner (* args, ** kwargs): t1 = time. time () re = func (* args, ** kwargs) t2 = time. time () print ('time cost: ', func. _ name __, t2-t1) return re return innerdef _ binary_search (dataset, find_num): if len (dataset)> 1: mid = int (len (dataset)/2) if dataset [mid] = find_num: # find it print ("find number", dataset [mid]) elif dataset [mid]> find_num: # return _ binary_search (dataset [0: mid], find_num) else: # return _ binary_search (dataset [mid + 1:], find_num) @ cal_timedef binary_search (dataset, find_num): _ binary_search (dataset, find_num) binary_search (data, 999) # Time cost: binary_search 0.4435563087463379

 

3. Exercise

There is a list of Student Information (sorted by id in ascending order) in the format:

[{Id: 1001, name: "Zhang San", age: 20}, {id: 1002, name: "Li Si", age: 25}, {id: 1004, name: "Wang Wu", age: 23 },{ id: 1007, name: "Zhao Liu", age: 33}]

Modify the binary search code, enter the student id, output the subscript of the student in the list, and output the complete student information.

Import randomimport time # generate random list def random_list (n): result = [] ids = list (range (1001,1001 + n) n1 = ['zhao ', 'wang ', 'sun', 'lil'] n2 = ['Guo ', 'ay', 'Qing', 'F', ''] n3 = ['Qing ', 'xuan ', 'yu', 'jun '] for I in range (n): age = random. randint (18,60) id = ids [I] name = random. choice (n1) + random. choice (n2) + random. choice (n3) dic = {'id': id, 'name': name, 'age': age} result. append (dic) return result # Binary Search def cal_time (func): def inner (* args, ** kwargs): t1 = time. time () re = func (* args, ** kwargs) t2 = time. time () print ('time cost: ', func. _ name __, t2-t1) return re return inner @ cal_timedef bin_search (data_set, val): low = 0 high = len (data_set)-1 while low <= high: mid = (low + high) // 2 if data_set [mid] ['id'] = val: return mid elif data_set [mid] ['id'] <val: low = mid + 1 else: high = mid-1student_list = random_list (100000) bin_search (student_list, 999) # Time cost: bin_search 0.0004999637603759766
Student id query. py

 

List sorting

Overview

List sorting:

Change unordered list to ordered list

Application scenarios:

  • Various lists
  • Various tables
  • For binary sorting
  • For other algorithms
Sort low B three-person group: bubble sort select sort insert sort quick sort NB two-person group: Heap Sort merge sort no one sort: Base sort Hill sort bucket sort
Comparison of several sort types

 

1. bubble sort (low)

Sorting method: first, the list contains two adjacent numbers. If the front edge is greater than the back side, the two numbers are exchanged and the maximum number is selected for each loop, sort the list in sequence;

import randomimport timedef call_time(func):    def inner(*args,**kwargs):        t1 = time.time()        re = func(*args,**kwargs)        t2 = time.time()        print('Time cost:',func.__name__,t2-t1)        return re    return inner@call_timedef bubble_sort(data):    for i in range(len(data)-1):        for j in range(len(data)-i-1):            if data[j] > data[j+1]:                data[j],data[j+1] = data[j+1],data[j]    return datadata = list(range(10000))random.shuffle(data)bubble_sort(data)# Time cost: bubble_sort 17.608235836029053

Time Complexity: O (n²)

Optimization:

@ Call_timedef bubble_sort_1 (data): for I in range (len (data)-1 ): exchange = False # No swapping indicates that the front ends have been arranged and the execution exits for j in range (len (data)-i-1): if data [j]> data [j + 1]: data [j], data [j + 1] = data [j + 1], data [j] exchange = True if not exchange: break return data

  

2. Select sort (low)

Sorting principle: Put the smallest number of records in one traversal to the first position; then, traverse the smallest number in the remaining list of records to continue placement; or put the largest traversal to the end.

import randomimport timedef call_time(func):    def inner(*args,**kwargs):        t1 = time.time()        re = func(*args,**kwargs)        t2 = time.time()        print('Time cost:',func.__name__,t2-t1)        return re    return inner@call_timedef select_sort(li):    for i in range(len(li)-1):        max_loc = 0        for j in range(len(li)-i-1):            if li[max_loc] < li[j]:                max_loc = j        li[len(li)-i-1],li[max_loc] = li[max_loc],li[len(li)-i-1]data = list(range(10000))random.shuffle(data)select_sort(data)# Time cost: select_sort 10.837876319885254

Time Complexity: O (n²)

def select_sort(li):    for i in range(len(li)-1):        min_loc = i        for j in range(i+1,len(li)):            if li[j] < li[min_loc]:                min_loc = j        li[i],li[min_loc] = li[min_loc],li[i]
Start from the front

 

3. Insert sorting (low)

Insert idea: the list is divided into two parts: ordered area and unordered area. The first ordered area has only one element. Select an element from the unordered area and insert it to the position of the ordered area until the unordered area becomes empty.

import randomimport timedef call_time(func):    def inner(*args,**kwargs):        t1 = time.time()        re = func(*args,**kwargs)        t2 = time.time()        print('Time cost:',func.__name__,t2-t1)        return re    return inner@call_timedef insert_sort(li):    for i in range(1,len(li)):        for k in range(i-1,-1,-1):            if li[i] < li[k]:                li[i],li[k] = li[k],li[i]                i = k            else:                breakdata = list(range(10000))random.shuffle(data)insert_sort(data)# Time cost: insert_sort 9.358688592910767

Time Complexity: O (n²)

def insert_sort(li):    for i in range(1,len(li)):        tmp = li[i]        j = i -1         while j >=0 and li[j] > tmp:            li[j+1] = li[j]            j = j -1        li[j+1] = tmp
Instructor writing

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.