![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def merge_sort(a,l,mid,r): #(l,mid) (mid+1,r) b = [0] * (r-l+1) i,j,t = 0,1,0 while True: if l + i > mid: b[t:] = a[mid+j:r+1] break elif mid + j > r: b[t:] = a[l+i:mid+1] break elif a[l + i] > a[mid + j]: b[t] = a[mid + j] j += 1 t += 1 elif a[l + i] <= a[mid + j]: b[t] = a[l + i] i += 1 t += 1 a[l:r+1] = b def partition_sort(a,l,r): if l<r: mid = l + (r - l) // 2 partition_sort(a,l,mid) partition_sort(a,mid+1,r) merge_sort(a,l,mid,r) a = [6, 1 , 2, 7, 9 , 3 , 4 ,5 ,10 , 8] partition_sort(a,0,len(a)-1) print(a)
堆排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution(object): def swap(self,x,i,j): tmp,x[i] = x[i],x[j] x[j] = tmp def partitionSort(self,x,l,r): i,j = l,r key = l while True: while x[j] >= x[key] and j > l:j -= 1 while x[i] <= x[key] and i < r:i += 1 if i >= j:break self.swap(x,i,j) self.swap(x,j,key) return j def quickSort(self,x,l,r): if l >= r:return p = self.partitionSort(x,l,r) print(x) self.quickSort(x,l,p-1) self.quickSort(x,p+1,r) x = [6, 1 , 2, 7, 9 , 3 , 4 ,5 ,10 , 8] Solution().quickSort(x,0,len(x)-1) print(x)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution(object): def minimumRepresentation(self, s): i,j,k,l = 0,1,0,len(s) while k<l and i<l and j<l: if s[(i+k)%l] == s[(j+k)%l]: k+=1 else: if s[(i+k)%l]>s[(j+k)%l]:i=max(i+k+1,j+1) else:j=max(j+k+1,i+1) k=0 print('i'+str(i)+' j'+str(j)+' k'+str(k)) return min(i,j) ''' babba zzzzz ''' while 1: x = 'cacacabaaa' s =Solution().minimumRepresentation(x) print(s) print(x[s:]+x[:s]) break
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def maxSubSum(nums): """ :type nums: List[int] :rtype: int """ b, sub_sum = 0, float('-inf') for x in nums: if b > 0: b += x else: b = x if b > sub_sum: sub_sum = b return sub_sum
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def get_pnext(p): pnext, L = [0], len(p) for i in range(1, L): k = pnext[i - 1] while k | 0 and p[k] != p[i]: k = pnext[k - 1] if p[k] == p[i]: pnext.append(k + 1) else: pnext.append(0) print(pnext) return pnext def strStr(s, p): """ :type haystack: str :type needle: str :rtype: int """ if not p: return 0 Ls, Lp = len(s), len(p) if Ls < Lp: return -1 i, j, pnext = 0, 0, get_pnext(p) print(pnext) while i < Ls and j < Lp: if s[i] == p[j]: i += 1 j += 1 elif j != 0: j = pnext[j - 1] else: i += 1 if j == Lp: return i - Lp return -1 p = ['a','b','a','b','a','a','b','c','a','b','c','d','e','a','b'] get_pnext(p)