Permalink
Browse files

add heapsort

  • Loading branch information...
1 parent f3098a5 commit 04650695d5d8cce25b7fd082504d69fdf2046255 @goog committed Apr 2, 2013
Showing with 124 additions and 0 deletions.
  1. 0 Tf-idf/shake.txt
  2. +30 −0 Tf-idf/tf-idf.py
  3. +13 −0 search.py
  4. +81 −0 sort/sort.py
View
0 Tf-idf/shake.txt
No changes.
View
30 Tf-idf/tf-idf.py
@@ -0,0 +1,30 @@
+'''
+Created on May 21, 2012
+
+@author: googcheng
+'''
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import urllib
+import re
+
+filtone= re.compile(ur'href=#\d+')
+filttwo= re.compile(ur'</script>')
+chs=re.compile(ur'[\u4e00-\u9fa5]+')
+pattern=re.compile(ur'href=/view/.*</a>$')
+
+# do a tokenizer
+
+
+
+ if len(match):
+ for m in match:
+ #print 'match line before filter',m
+ if (not filtone.search(m)) and (not filttwo.search(m)):
+ m=m.split("<div")[0]
+ m=m.split(ur'\u3010')[0]
+ #print 'match line after filter',m
+ mat=re.sub(ur'[\u3011\u3001\uff1a]{1}',' ',m,re.UNICODE)
+ synlist = mat.split(' ')
+
View
13 search.py
@@ -0,0 +1,13 @@
+def biSEARCH(li,key):
+ mid = len(li)/2
+ if key == li[mid]:
+ print "find it"
+ elif key < li[mid]:
+ biSEARCH(li[:mid],key)
+ else:
+ biSEARCH(li[mid+1:],key)
+
+if __name__=="__main__":
+ li = [1,3,4,5,7,8,90,100]
+ biSEARCH(li,100)
+
View
81 sort/sort.py
@@ -0,0 +1,81 @@
+
+def bubbleSORT(li):
+ for i in reversed(range(len(li)-1)):
+ for j in range(i+1):
+ if li[j]>li[j+1]:
+ li[j],li[j+1]=li[j+1],li[j] ## swap two.
+ print li
+
+
+def straightINSERTIONsort(li):
+ for i in range(1,len(li)):
+ temp = li[i]
+ for j in reversed(range(i)):
+ print "i",i,"j",j
+ if temp < li[j]:
+ li[j+1]=li[j]
+ else:
+ li[j+1]=temp
+ break
+ print li
+
+
+def insertionSORT(list2):
+ for i in range(1, len(list2)):
+ save = list2[i]
+ j = i
+ while j > 0 and list2[j - 1] > save:
+ list2[j] = list2[j-1]
+ j -= 1
+ list2[j] = save
+ print list2
+
+def shellSORT(list2):
+ d = len(list2)/2
+ while d >0:
+ for i in range(d,len(list2)):
+ j=i
+ save= list2[i]
+ while j>=d and list2[j-d]> save:
+ list2[j]=list2[j-d]
+ j-=d
+ list2[j]=save ## j changed
+ d/=2
+ print list2
+
+
+
+
+def heapSORT(li):
+ n = len(li)
+ for i in range((n-1)/2,-1,-1): ## (n-1)/2 the last parent node
+ heapADJUST(li,i,n-1)
+ for i in range(n-1): ##
+ li[0],li[n-i-1] = li[n-i-1],li[0]
+ heapADJUST(li,0,n-i-2)
+ print li
+
+def heapADJUST(li,s,m):
+ rc = li[s]
+ j = 2*s+1
+ while j <= m:
+ if ((j < m) and (li[j]< li[j+1])):
+ j+=1
+ if (rc >= li[j]):
+ break
+ li[s]= li[j] #### place the bigger one
+ s= j
+ j = 2*j +1
+ li[s]=rc ## place the
+
+
+if __name__ == '__main__':
+ #bubbleSORT([100,15,13,1,23,12])
+ #straightINSERTIONsort([3,5,1])
+ #insertion_sort([1,2,5,3])
+ # shellSORT([21,34,2,23,45,65])
+ heapSORT([21,34,2,23,45])
+
+
+
+

0 comments on commit 0465069

Please sign in to comment.