Bubble sortThe basic idea of bubble sorting is to compare two adjacent elements each time and swap them out if they are in the wrong order.If there are n numbers to sort, just n?1 the number of digits, which meansN-1 operation. The "Every trip" requires a comparison of two adjacent numbers starting from the 1th position, and the smaller oneIn the back, when the comparison is complete, move back one to continue comparing the size of the two adjacent numbers below, repeat this step until the lastN
) 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
(large) on the child node, i.e.A[parent (i)] >= a[i] ... ①The latter makes it easy to say nothing about redundancy, only the maximum heap.So the first goal of the heap is to establish a maximum heap that satisfies the condition, and to establish the maximum heap, it is necessary to have the maximum heap criterion, namely the ① type. After the maximum heap is established, the root node is found, saved, and rejected, and the remaining sequence continues to be the largest heap, repeating the proce
In the interview, often encounter some of the problem of sorting algorithm, here, I simply listed a few of the most common sorting algorithms for everyone to learn, perhaps the next day interview exactly use, the second half of the article introduces the collections module, because this module relative to the basic data structure provided by Python (list,tuple,dict) are not familiar to people, but if you kn
: def __init__ (self,data): = None = None = None = Data A node code is given, followed by a two-fork search tree Red-black tree is discussed in detail.Hash list (hash table)"Hash list is the generalization of the concept of ordinary arrays" one of the words to explain the hash list is better.You can think of the array as a hash function for hash (x) = x% max (max ratio so the value is large) to get the hash value under this subscript to process the dataPros: Ve
Implementation of various sorting algorithms in python and java (1)
First, Implement Bubble Sorting in the simplest way:
#-*-Coding: UTF-8-*-intarray = [, 7] def bubble (array): for I in range (1, len (array): for j in range (I): if array [j]> array [I]: array [j], array [I] = array [I], array [j] # traverse the array def print_list (array): for I in intarray: print I, # execute the sort bubble (intarray)
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
data structures and algorithms (Python)Bubble SortBubble Sort (English: Bubble sort) is a simple sorting algorithm. It iterates over the sequence of columns to be sorted, compares two elements at a time, and swaps them if their order is wrong. The work of iterating through the series is repeated until no more swapping is needed, meaning that the sequence is sorted. The algorithm is named because the smaller
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
# !/usr/bin/env python#! _*_ coding:utf-8 _*_ fromQueueImportQueueImportTimeque=Queue () Time_begin=time.time ()#if a+b+c=1000, and A^2+b^2=c^2,a,b,c is the natural number, find out all the a,b,c combinations#calculating results Using enumeration method forAinchRange (1001): forBinchRange (1001): forCinchRange (1001): ifA + b + c = = 1000 anda**2 + b**2 = = C**2: Que.put ({'a'A'b': B,'C': C}) Time_end=time.time ()Print "The run
Python data structures and algorithms-Data TypesWe will review the built-in atomic data types from the atomic data types. python has two main built-in data classes: int and float. standard Arithmetic Operations: +,-, *,/, and ** (Power Operation). You can use parentheses to change the operation Priority. other very useful operators are the remainder operation %,
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.