First, the base sort
ImportRandom fromTimewrapImport*defList_to_buckets (Li, iteration):#This is the number used to compare the size of each position . """because it is divided into 10 is ordered, so the discharge is orderly. :p Aram Li: List:p aram iteration: The bucket is the first iteration: return:"""Buckets= [[] for_inchRange (10)] Print('buckests', buckets) forNuminchLi:digit= (num//(iteration))% 10buckets[digit].append (num)Print(buckets)returnBucketsdefBuckets_to_list (buckets):#This is for a few. return[num forBucketsinchBuckets forNuminchBuckets]#li = [] #For buckets in buckets: #For num in buckets: #li.append (num)@cal_timedefRadix_sort (LI): maxval= Max (LI)#10000it =0 whileTen * * It <= maxval:#This is the sort of loop used, based on the previous sort. Li =buckets_to_list (List_to_buckets (Li, it)) it+ = 1returnLi#li = [random.randint (0,1000) for _ in range (100000)]Li = [Random.randint (0,10) for_inchRange (10)]li=[5555,5525,9939,9999,6,3,8,9]s=Radix_sort (LI)Print(s)
Ii. sort of Hill
Ideas:
- Hill sort is a grouping insertion sorting algorithm.
- First take an integer D1=N/2, the element is divided into D1 groups, each group of adjacent elements between the distance of D1, in each group of direct insertion sort;
- Take the second integer D2=D1/2, repeating the grouping sorting process until di=1, that is, all elements in the same group
- Hill sort each trip does not make certain elements orderly, but to make the overall data more and more orderly; The final sequencing makes all the data orderly.
Code implementation,
defInsert_sort (LI):#Insert Sort forIinchRange (1, Len (LI)):#I denotes the first number of unordered areasTMP = Li[i]#Touch the cardj = I-1#J points to the last position of the ordered area whileLI[J] > tmp andJ >=0:#cyclic termination Condition: 1. li[j] <= tmp; 2. J = =-1LI[J+1] =Li[j] J-= 1li[j+1] =tmpdefShell_sort (LI):#The difference between a hill sort and an insertion sort is to turn 1 into D .d = Len (LI)//2 whileD >0: forIinchRange (d, Len (LI)): TMP=Li[i] J= i-D whileLI[J] > tmp andJ >=0:li[j+D] =Li[j] J-=D li[j+D] =tmp D= d >> 1Li=[5,2,1,4,5,69,20,11]shell_sort (LI)Print(LI)
The complexity of hill sorting is particularly complex, depending on D, the length of the grouping two, the displacement operator
Third, counting sort:
Count how many times each number appeared
#Count Sort#0 0 1 1 2 4 3 3 1 4 5 5ImportRandomImportCopy fromTimewrapImport*@cal_timedefCount_sort (li, max_num = 100): Count= [0 forIinchRange (max_num+1)] forNuminchLi:count[num]+=1li.clear () forI, ValinchEnumerate (count): for_inchRange (val): Li.append (i) @cal_timedefSys_sort (LI): li.sort () Li= [Random.randint (0,100) forIinchRange (100000)]li1=copy.deepcopy (LI) count_sort (LI) sys_sort (LI1)
Counting sort so fast, why not count the sort? Because he is limited, you have to know the maximum number in the list
If a large number, such as 10000, is down, then the space occupied is so large,
Count sort takes up space and the scope of the list is related
The solution to this problem can be sorted in buckets, and all can be sorted in other ways. For example, insert sort.
Iv. sequencing of Buckets
In a count sort, how do you change the algorithm if the range of the elements is large (for example, between 1 and 100 million)?
Buckets are sorted by first sorting the elements in different buckets, and ordering the elements in each bucket.
Multi-keyword Sorting
Sort 10 bits first, then sort by 10 bits
To use two functions, one for the bucket, one for the barrel.
Default 10 barrels, find bits, 10 bits, respectively, placed in the corresponding bucket position
The performance of bucket sequencing depends on the distribution of the data. That is, you need to take a different bucket strategy when sorting different data.
Average time complexity: O (N+K)
Worst case time Complexity: O (N+K)
Space complexity: O (NK)
It is divided into buckets, and the barrels are sorted by insertion.
Example
1: Give two strings S and T to determine if T is the re-arranged word of S:
S= "Anagram", t= "Nagaram", return True
S= ' cat ', t= ' car ', return false
The code is as follows:
" Anagram " "nagaram"def SS (s,t): return sorted ( List (s)) = =sorted (list (t)) y=ss (s,t)print(y)
2, ""
Two-dimensional coordinates become one-dimensional coordinates
X*b +y =i
(x, y)->i
i//n----"X
i%n---->n
1 defSearchmatrix (Matrix, target):2m =len (Matrix)3 #print (' m ', m)4 ifm = =0:5 returnFalse6n =Len (matrix[0])7 ifn = =0:8 returnFalse9Low =0TenHigh = m * n-1 One #print (' High ', high) A whileLow <=High : -Mid = (low + high)//2 -X, y =Divmod (Mid, N) the ifMatrix[x][y] >Target: -High = Mid-1 - elifMatrix[x][y] <Target: -Low = mid + 1 + Else: - returnTrue + Else: A returnFalse at - -s = [ -[1, 2, 3], -[4, 5, 6], -[7, 8, 9] in ] - #Print (Searchmatrix (S, 1)) to #Print (Searchmatrix (S, 2)) + #Print (Searchmatrix (S, 3)) - #Print (Searchmatrix (S, 4)) the #Print (Searchmatrix (S, 5)) * #Print (Searchmatrix (S, 6)) $ Print(Searchmatrix (S, 7))Panax Notoginseng #Print (Searchmatrix (S, 8)) - #Print (Searchmatrix (s, 9))
View Code
3. Given a list and an integer, the design algorithm would look for two decimal numbers, making the sum of two numbers a given integer. There must be only one result guaranteed.
For example: List [1,2,5,4] with the target integer 3,1+2=3, the result is (0,1)
Way One:
Way two:
Mode three
Way four and three like
def twosum (num, target): = {} for in range (len (num)): print(dict) = Num[i] if inch dict: return Dict[target- x], I == [1, 2, 5, 4]print(Twosum (L, 7 ))
Cardinality, Hill, count, bucket sort