GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode_python/Basic Algorithm/3way_quicksort.py at master · xtymichael/Leetcode_python Expand file tree/ Copy path3way_quicksort.pyMore file actions 67 lines (54 loc) · 1.16 KB / Copy path3way_quicksort.pyFile metadata and controls67 lines (54 loc) · 1.16 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 # three-way quicksort def quicksort(alist, left, right): if left < right: lo, hi = partition(alist, left, right) quicksort(alist, left, lo - 1) quicksort(alist, hi + 1, right) def partition(alist, left, right): pivot = alist[left] i = l = left r = right while i <= r: if alist[i] < pivot: l += 1 i += 1 alist[i], alist[l] = alist[l], alist[i] elif alist[i] > pivot: r -= 1 alist[i], alist[r] = alist[r], alist[i] else: i += 1 return l, r sample = [54,26,93,17,77,31,44,55,20] quicksort(sample, 0, len(sample)) print sample # # quick sort, in-place 3-way partition # # David Eppstein, UCI, 17 Jan 2002 # import random # R = random.Random(42) # def qsort(L): # quicksort(L,0,len(L)) # def quicksort(L,start,stop): # if stop - start < 2: return # key = L[R.randrange(start,stop)] # e = u = start # g = stop # while u < g: # if L[u] < key: # swap(L,u,e) # e = e + 1 # u = u + 1 # elif L[u] == key: # u = u + 1 # else: # g = g - 1 # swap(L,u,g) # quicksort(L,start,e) # quicksort(L,g,stop) # def swap(A,i,j): # temp = A[i] # A[i] = A[j] # A[j] = temp # L = [3,1,4,1,5,9,2,6,5,3,5,8,9,7,9] # qsort(L) # print Lb |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |