python algorithms mastering basic algorithms in python language
python algorithms mastering basic algorithms in python language
Want to know python algorithms mastering basic algorithms in python language? we have a huge selection of python algorithms mastering basic algorithms in python language information on alibabacloud.com
in the code below?# Not O (3) but O (1) print (' Hello World ') print (' Hello Python ') print (' Hello algorithm ') # not O (n2+n) but O (N2) for I in Range (n): Print (' Hello World ') to J in range (n): print (' Hello World ') # not O (1/2n2) but O (N2) for I in Range (n): For J in Ran GE (i): print (' Hello world ')And look at the following code:# time Complexity O (log2n) or O (Logn) while n > 1: print (n) n
Combinatorial algorithms
The idea of this program is to open an array whose subscript represents 1 to M, and the value of the array element is 1 to indicate its subscript
The number represented is selected, and 0 is not selected.
First, the first n elements of the array are set to 1, which indicates the number of top N.
The "10" combination of the array element values is then scanned from left to right, and the first "10" combination is found to chan
next time . A the defShufflepile (self): + """in the current state, the tree is adjusted so that it becomes a heap""" - #downward adjustment from "heap bottom" to "heap top", which keeps the smallest elements rising $ #This allows the heap below the I node to be the local minimum heap. $ forIinchRange ((Len (self)-2)/2,-1,-1):#N/2,..., 0 - Self.siftdown (i) - the defdeletemin (self): - """Remove Minimum element"""Wuyit = self[0]#record the
#!/usr/local/python35/bin/python3.5## # # Insert Sortif __name__=="__main__": Var_list=[3,2,4,5,1] """from the second item, start by comparing it to the item in front of it, and if it is smaller than the previous item, go to the front. """ forIndexinchRange (1, Len (var_list)): Key=var_list[index]## Record The value of the second itemI=index-1## Record the subscript of the preceding paragraph whileI>=0 andKey## If the bar is satisfied, move the preceding paragraph backwards and mo
= [2,3,5,7,8,9,6,54,1,42] Print(List1) selectsort (list1)Print(List1)#Hill Sort#The entire sequence of elements to be sorted into a number of sub-sequences (consisting of elements separated by an "increment") for direct insertion sorting#then reduce the increments in turn and then sort the#when the elements in the entire sequence are basically ordered (the increment is small enough), then the whole element is sorted in a direct insert. defShellsort (data,flag):" ":p Aram Data:list, to be sort
Code:#Coding:utf-8#Author: Xubling#swap sort. Bubble sortL = [1, 3, 2, 32, 5, 4]defBubble_sort (L): forIinchRange (len (L)): forJinchRange (i+1, Len (L)):ifL[i]>L[j]:#temp = l[j] #L[j] = l[i] #L[i] = tempL[i], l[j] = L[j], l[i]#Exchange Order PrintLbubble_sort (L)The bubble sort should be the most familiar sort algorithm, so basically write this algorithm without encountering any problems. It is worth noting that the order in which two numbers are exchanged in
) print (data) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1 8, 19]Select sortIdea: A trip through the smallest number of records, put in the first position, and then go through the record remaining list of the smallest value, placedImport randomdef Select_sort (LI): for I in range (Len (LI)-1): min_loc = i for j in Range (I+1,len (LI)): If Li[j]Insert SortImport randomdef Insert_sort (LI): for I in range (1,len (LI)): temp = li[i]
1. Principle:2. Code implementationdefQuick_sort (alist, Start, end):"""Quick Sort""" #Recursive exit conditions ifStart >=End:return #sets the starting element as the datum element to find the positionMID =Alist[start]#Low is the left-to-right cursor for the left of the sequenceLow =Start#High is the right-to-left cursor to the right of the sequenceHigh =End whileLow High :#If low is not coincident with high, the element with high points is not smaller than the datum element, then high
at a time until an empty slot is found and the key is stored in that position. For example, the hash value of the conflict is H, followed by the order of H+1, h+2, h+3 ...To prevent aggregation (clustering), the skip slots method can be used.(ii) quadratic probing (square probe): that is, the conflicting hash value is H, then the next lookup is h+1, followed by h+4,h+9,h+16 ...(2) Chaining (linked list method): All elements of the same hash value are saved in a linked list. However, the more el
value.Similarly, when an element of the RightMark position is greater than or equal to the base value, RightMark moves to the left one position to continue scanning, and the scan stops when the element of the RightMark position is less than the base value.After stopping the scan, we compare the size of the Leftmark and RightMark, if the Rightmark After we have put the datum values in the correct position, we see that the elements on the left side of the base value are smaller than the datum va
"Select Sort"The selection sort is based on a bubbling sort (Bubble sort) that has been improved: each visit process (pass) needs to be exchanged at most.Each visit process, to find the maximum value, when the end of the visit, the maximum value is exchanged to the correct position;Then continue to repeat the process in the remaining sublist until the n-1 visit is completed (n is the length of the list);At this point, the remaining elements in the list are automatically aligned to the correct po
Recommend a Visual Web site "Visual Algo": Url= ' https://visualgo.net/en/sorting 'This website gives the principles and processes of various sorting algorithms, which are visualized through dynamic forms. The related pseudo-code are also given, as well as the specific steps to execute to code."Bubble Sort"You need to repeatedly visit the sequence of columns that need to be sorted. The size of the adjacent two items is compared during the visit, and i
Recently learned about Python implementation of common machine learning algorithms on GitHubDirectory
First, linear regression
1. Cost function2. Gradient Descent algorithm3. Normalization of the mean value4. Final running result5, using the linear model in the Scikit-learn library to implement
Second, logistic regression
1. Cost function2. Gradient3. Regularization4, S-type func
Eight, the base sort
Basic idea: the Cardinal sort (radix sort) is "assigned sort" (distribution sort), also known as "bucket method" (bucket sort) or bin sort, as the name implies, it is part of the information through the key value, The elements to be sorted are assigned to some "buckets", in order to achieve the order, the cardinal sort method is a sort of stability, its time complexity is O (Nlog (r) m), where R is the cardinality taken, and M
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.