while(i < n):
			m = mergeBottomUp(a, i, min(i + width, n), min(i+2*width, n), b)			
			i += 2*width
		(a,b) = (b,a)
		width *= 2
	return a

def mergeBottomUp(a,p,q,r,b):		
	(i, k, j) = (p, q, p)
	while(i < q and k < r):
		if(a[i] < a[k]):						
			b[j] = a[i]
			i+=1
		else: 			
			b[j] = a[k]
			k+=1
		j += 1
	while(i < q):
		b[j] = a[i]
		i += 1
		j += 1
	while(k < r):		
		b[j] = a[k]
		k += 1
		j += 1

measure.time(mergeSort)
measure.time(mergeSort2)
measure.time(mergeSort3)
measure.time(mergeSortBottomUp)
measure.time(sorted)
Exemple #2
0
            i += 2 * width
        (a, b) = (b, a)
        width *= 2
    return a


def mergeBottomUp(a, p, q, r, b):
    (i, k, j) = (p, q, p)
    while (i < q and k < r):
        if (a[i] < a[k]):
            b[j] = a[i]
            i += 1
        else:
            b[j] = a[k]
            k += 1
        j += 1
    while (i < q):
        b[j] = a[i]
        i += 1
        j += 1
    while (k < r):
        b[j] = a[k]
        k += 1
        j += 1


measure.time(mergeSort)
measure.time(mergeSort2)
measure.time(mergeSort3)
measure.time(mergeSortBottomUp)
measure.time(sorted)
	i = p
	k = r - 1
	while(i < k):
		if(a[i] < pivot):
			i += 1
		else:
			k -= 1
			a[i], a[k] = a[k], a[i]			
	a[i], a[r-1] = a[r-1], a[i]
	return i

def quickSort4(a):
	return quickSort3Impl(a,0,len(a))

def quickSort4Impl(a, p, r):
	n = r - p
	if n == 0: return []
	while(n > 1):
		mid = partition3(a, p, r)	
		quickSort4Impl(a,p,mid)
		p = mid + 1
		n = r - p		
	return a

measure.time(quickSort)
measure.time(quickSort2)
measure.time(quickSort3)
measure.time(quickSort4)


Exemple #4
0
    k = r - 1
    while (i < k):
        if (a[i] < pivot):
            i += 1
        else:
            k -= 1
            a[i], a[k] = a[k], a[i]
    a[i], a[r - 1] = a[r - 1], a[i]
    return i


def quickSort4(a):
    return quickSort3Impl(a, 0, len(a))


def quickSort4Impl(a, p, r):
    n = r - p
    if n == 0: return []
    while (n > 1):
        mid = partition3(a, p, r)
        quickSort4Impl(a, p, mid)
        p = mid + 1
        n = r - p
    return a


measure.time(quickSort)
measure.time(quickSort2)
measure.time(quickSort3)
measure.time(quickSort4)
import functools
import measure

def getKthMax(a, k):
	n = len(a)
	return getKthMin(a, n-k-1)

def getKthMin(a, k):
	n = len(a)	
	if n == 1:
		return a[0]
	(a, mid) = partition(a)
	if k == mid: return a[mid]
	if(k > mid): return getKthMin(a[mid:], k - mid)
	else: return getKthMin(a[:mid], k)

def partition(a):	
	n = len(a)
	pivot = a[n-1]
	l = [x for x in a[:-1] if x <= pivot]
	r = [x for x in a[:-1] if x > pivot]
	a = l + [pivot] + r	
	return (a, len(l))

p = functools.partial(getKthMax, k = 150000)
measure.time(p)
import measure
import functools

def countingsort(a, n):
	c = [0] * n
	b = [None] * len(a)
	for x in a:
		c[x] += 1
	for i in xrange(1, len(c)):
		c[i] = c[i] + c[i-1]
	for x in a:
		b[c[x]-1] = x
		c[x] -= 1
	return b

func = functools.partial(countingsort, n = 300000)
print measure.time(func)
Exemple #7
0
import measure
import math
import functools

def sort_by_digit(a, digit_num, base):
	buckets = [[] for i in range(base)]
	for x in a:
		digit = int(round(x / (base ** digit_num)) % base)
		buckets[digit].append(x)
	i = 0
	for b in buckets:
		for x in b:
			a[i] = x
			i += 1


def radixSort(a, base):	
	passes = int(round(math.log(max(abs(x) for x in a), base))) + 1
	for i in range(passes):		
		sort_by_digit(a, i, base)
	return a

func = functools.partial(radixSort, base = 30000)
print measure.time(func)
import functools
import measure


def getKthMax(a, k):
    n = len(a)
    return getKthMin(a, n - k - 1)


def getKthMin(a, k):
    n = len(a)
    if n == 1:
        return a[0]
    (a, mid) = partition(a)
    if k == mid: return a[mid]
    if (k > mid): return getKthMin(a[mid:], k - mid)
    else: return getKthMin(a[:mid], k)


def partition(a):
    n = len(a)
    pivot = a[n - 1]
    l = [x for x in a[:-1] if x <= pivot]
    r = [x for x in a[:-1] if x > pivot]
    a = l + [pivot] + r
    return (a, len(l))


p = functools.partial(getKthMax, k=150000)
measure.time(p)