Python Learning Notes-the basic "Fifth Week"-Algorithm (4*4 2-D array and bubble sort), time complexity

Source: Internet
Author: User

Algorithm BasicsRequirement: Generate a 4*4 2-d array and rotate it 90 degrees clockwise
#!_*_coding:utf-8_*_array=[[col for Col in Range (5)] for row in range (5)] #初始化一个4-in-A-Z array #array=[[col for col in ' ABCDE '] for Row in range (5)]for row in array: #旋转前先看看数组长啥样    print (row) print ('-------------') for I,row in Enumerate (array):    For index in range (I,len (row)):        tmp = array[index][i] #get Each rows ' data by column ' s index        array[index][i] = arr Ay[i][index] #        print Tmp,array[i][index]  #= tmp        Array[i][index] = tmp for    R in Array:print r    print ( '--one Big Loop--')

Bubble sort

Sort an irregular array in order from small to large

data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6]print ("before sort:", data) Previous = Data[0]for J in range (len (data)):    tmp = 0 for    i in range (len (data)-1):        if data[i] > data[i+1]:            tmp=data[i]            data[i] = data[i+1]            data[i+1] = tmp    print (data) print ("After sort:", data)

Code optimization (Improve performance)

1Count=02data = [10,4,33,21,1,54,3,8,11,5,22,2,1,17,13,6]3 #For Index,i in enumerate (Data[0:-1]):4 Print(len (data))5  forJinchRange (1, Len (data)):6      forIinchRange (len (data)-j):#j= 0 1 2 3 4 5 6 lifting the place7         ifData[i]>data[i+1]:8Tmp=data[i+1]9Data[i+1]=data[i]#assign a value of 10 to 4TenData[i]=tmp#assign 4 to ten . OneCount+=1 A     Print(data) - Print("Count", count)
Bubble Sort Complexity of Time
(1) Time frequency
The time it takes for an algorithm to execute is theoretically impossible to figure out and must be tested on the machine. But we can not and do not need to test each algorithm, just know which algorithm spends more time, which algorithm spends less time on it. And the time that an algorithm spends is proportional to the number of executions of the statement in the algorithm, which algorithm takes more time than the number of statements executed. The number of times a statement is executed in an algorithm is called a statement frequency or time frequency. Note as T (N). (2) Complexity of timeIn the time frequency mentioned just now, N is called the scale of the problem, and when N is constantly changing, the time frequency t (n) will change constantly. But sometimes we want to know what the pattern is when it changes. To do this, we introduce the concept of time complexity. In general, the number of iterations of the basic operation of the algorithm is a function of the problem size n, denoted by T (n), if there is an auxiliary function f (n), so that when N is approaching infinity, T (n)/f (n)The limit value is a constant that is not equal to zero, then f (n) is the same order of magnitude function of T (N). Recorded as T (n) =o (f (n)),Said O (f (n))For the algorithm's progressive time complexity, referred to as the complexity of time. Index TimeRefers to the computational time required for a problem solver m( N), exponential growth according to the size of the input data (that is, the amount of input data grows linearly, and the time it takes to grow exponentially)
for (I=1; i<=n; i++)       x + +; For (I=1, i<=n; i++) for     (j=1; j<=n; j + +)          x + +;

The time complexity of the first for loop is 0 (n) and the time complexity of the second for loop is 0 (n2), the time complexity of the entire algorithm is 0 (n+n2) =0 (n2).

Constant time

If the upper bound of an algorithm is independent of the input size, it is said to have a constant time , which is recorded as time. An example is accessing a single element in an array, because accessing it requires only one instruction. However, finding the smallest element in an unordered array is not, because it requires traversing all the elements to find the minimum value. This is a linear time operation, or time. But if you know the number of elements in advance and assume that the quantity remains the same, the operation can also be called a constant time.

Logarithmic time

If the algorithm's T(n) = O (log n), it is said to have a logarithmic time

The common algorithm with logarithmic time has two cross-tree related operations and binary search.

The logarithmic time algorithm is very effective because each additional input requires less extra computation time.

A simple example of this class function is to recursively chop a string and output it. It requires O (log n) time because we cut the string in half before each output. This means that if we want to increase the number of outputs, we need to double the string length.

 

Linear time

If the time complexity of an algorithm is O (n), the algorithm is said to have linear time, or O (n) time. Informally, this means that for large enough inputs, the size of the run time increases linearly with the input. For example, a program that computes all the elements of a list and takes the time to be proportional to the length of the list.

See details of Alex King's documents

Http://www.cnblogs.com/alex3714/articles/5143440.html

Python Learning Notes-the basic "Fifth Week"-Algorithm (4*4 2-D array and bubble sort), time complexity

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.