Algorithmic Toolbox__4
一、Search Algorithms
1.Linear Search
#以下是伪代码所解释的原理
Lin(c[id(0).......id(n)]:
for i from 0 to len(c)-1:
minidex=i
for j from i+1 to len(c)-1:
if c[j]<c[minidex]:
minidex=j
swap(i,minidex)
return c
2.Binary Search
Binary search can be used when the original list has already been sorted, then we can do as the fake code shown below:
#Binary Search fake code
Bin(c[id(0)......],s_v):
if len(c)==2:
if c[0]!=s_v and c[1]!=s_v
return False
else:
return True
mid=round(len(c)/2,0)
if s_v<c[mid]:
return Bin(c[1:mid],s_v)
else:
return Bin(c[mid+1:,s_v)
二、Polynomial Multiplication
The algorithm that can solve this problem came from linear algebra, imagine that there are two polynomial which has the mode like “a_0+a_1x+a_2pow(x,2)+…a_npow(x,n)”, and we will multiply them together, what should we do?
Firstly, we can create a list which has the length as 2n+1, and we can put all coefficient in it.
Secondly, we let the A=D1pow(x,n/2)+D2 B=E1pow(n/2)+E2, then AB=D1D2pow(x,n)+[(D1+E1)(D2+E2)-D1D2-E1E2]pow(x,n/2)+D2E2
so we find that listlist become a new type of operator, we can treat that the same as "+,-,,/", so we can do as the fake code shown below.
#Polynomial Multiplication fake code
#To make it more simple we should let the length of both A and B are even
PL(A,B,n):
if n=0:
return [A*B]
R=[0 for i in range(2n)]
mid=round((n-1)/2,0)
D1,D2,E1,E2=A[:mid+1],A[mid+1:],B[:mid+1],B[mid+1:]
mid_A=D1+E1
mid_B=E2+D2
k_n=PL(D1,E1,mid)
k_n_2=PL(mid_A,mid_B,mid)
k=PL(D2,E2,mid)
for i in range(n):
R[n-1-i]+=k[i]
R[mid+n-1-i]+=K_n_w[i]
R[2n-1-i]=k_n[i]
return R
When we are solving accuracy problem, the function may to be modified.
三、Master Theorem
T(n)=aT(n/b)+f(n)
if f(n)<O(n^loga(b-beta)),beta>0 T(n)=O(n^logb(a))
if f(n)=O(n^loga(b)) T(n)=O(n^*logb(a)log(n))
if f(n)=O(n^loga(b+beta),beta>0 T(n)=O(f(n))
三、Some Sort Algorithms
1.Selection Sort
This is a very simple example:
Select(c):
for i in range(len(c)):
mini=i
for i=j in(i+1,len(c)):
if j<mini;
mini=j
a,b=c[i],c[mini]
c[mini]=a
c[i]=b
return c
2.Merge Sort
Merge Sort can sort the list in a faster way, meanwhile it is steady, so whenever we sort a list or array, using this is a wise choice.
def merge_sort(arr):
if len(arr) == 1:
return arr
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
return marge(merge_sort(left), merge_sort(right))
# we can see that keep the length of left is equal to the length of right is not neccessary, but in the polynomial multipication it is necceaasry
def marge(left, right):
result = []
while len(left) > 0 and len(right) > 0:
if left[0] <= right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
result += left
result += right
return result
And we can find the condition that can jump out the iteration, the is the length become 1, we can imagine two sorted list were coefficients of function merge, what will happen?
Well, finding these lists need function merge_sort which need function merge, however, if we find two sorted listed in some step, it will work. So where are they? If we treat each number as a list, it has already been sorted, in this way we create the condition of jumping out of iteration just in the second line.
3.Count Sort
This function can be used into sorted lists that were filled with integer and has the maximum one.
def countSort(arr):
output = [0 for i in range(256)]
count = [0 for i in range(256)]
ans = ["" for _ in arr]
for i in arr:
count[ord(i)] += 1
for i in range(256):
count[i] += count[i-1]
for i in range(len(arr)):
output[count[ord(arr[i])]-1] = arr[i]
count[ord(arr[i])] -= 1
for i in range(len(arr)):
ans[i] = output[i]
return ans
arr = "wwwrunoobcom"
ans = countSort(arr)
print ( "字符数组排序 %s" %("".join(ans)) )
4.Quick Sort
In this method, we first confirm a cardinality, place all those smaller than it on the left, and larger ones on the right, and then repeat the process for the lists on the left and right sides, until the length of the list becomes 1.
#So the condition that ca jump out the oteration is high<=low, it is the label of the length of sorting list become 1.
def QuickSort(list,low,high):
if high > low:
k = Partitions(list,low,high)
QuickSort(list,low,k-1)
QuickSort(list,k+1,high)
#This part of the code can dichotomize the specified part of the list and move the function of greater than part and less than part
def Partitions(list,low,high):
left = low
right = high
k = list[low]
while left < right :
while list[left] <= k:
left += 1
while list[right] > k:
right = right - 1
if left < right:
list[left],list[right] = list[right],list[left]
list[low] = list[right]
list[right] = k
return right
def quickSort(L, low, high):
i = low
j = high
if i >= j:
return L
key = L[i]
while i < j:
while i < j and L[j] >= key:
j = j-1
L[i] = L[j]
while i < j and L[i] <= key:
i = i+1
L[j] = L[i]
L[i] = key
quickSort(L, low, i-1)
quickSort(L, j+1, high)
return L