标签:中间 number 有序 vertica 二分 ges mbed ems 统计
""" Print the running times for problem sizes that double, using a aingle loop """
import time
problemSize = 1000000 print("%12s%16s" % ("Problem Size", "Seconds")) for count in range(5):
start = time.time() #The start of algorithm work = 1 for x in range(problemSize): work += 1 work -= 1 #The end of algorithm elapsed = time.time() - start
print( "%12d%16.3f" %( problemSize,elapsed ) ) problemSize *= 2 |
Problem Size Seconds 1000000 1.065 2000000 2.078 4000000 4.433 8000000 7.733 16000000 18.676 |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: counting.py Prints the number of iterations for problem sizes that double, using a nested loop. """
problemSize = 1000 print( "%12s%15s" % ( "Problem Size", "Iteration" ) )
for counter in range(5): number = 0
#The start of the algorithm work = 1 for j in range( problemSize ): for k in range( problemSize ): number += 1 work += 1 work -= 1
#The end of the algorithm
print( "%12d%15d" % ( problemSize, number ) ) problemSize *= 2
|
Problem Size Iteration 1000 1000000 2000 4000000 4000 16000000 8000 64000000 16000 256000000 |
""" Print the numbers of calls of a recursive Fibonacci function with problem size that double """
from counter import Counter
def fibonacci( n, counter ): """Counter the numbers of calls of the fibonacci function.""" counter.increment()
if n < 3:return 1 else: return fibonacci( n - 2, counter ) + fibonacci( n - 1, counter)
problemSize = 2 print( "%12s%15s" % ( "Problem Size", "Calls" ) ) for count in range(5): counter = Counter()
#The start of the algorithm fibonacci( problemSize, counter ) #The end of the algorithm print( "%12d%15s" % ( problemSize, counter ) )
problemSize *= 2 |
Problem Size Calls 2 1 4 5 8 41 16 1973 32 4356617 |
def __lt__( self, other ): |
def selectionSort( lyst ): """ The selection sort algorithm """
i = 0 while i < len(lyst) - 1: minIndex = i j = i + 1 while j < len(lyst): if lyst[j] < lyst[minIndex]: minIndex = j j += 1 if minIndex != i: #Exchange if needed swap( lyst, i, minIndex ) i += 1 |
def bubbleSort( lyst ): n = len( lyst ) while n > 1: swapped = False i = 1 #start eack bubble while i < n: if lyst[i] < lyst[i-1]: swap( lyst, i, i - 1 ) #exchange if needed swapped = True i += 1 if not swapped: return n -= 1 |
def insertionSort( lyst ): i = 1 while i < len( lyst ): itemToInsert = lyst[i] j = i - 1 while j >= 0: if lyst[j] > itemToInsert: lyst[j+1] = lyst[j] j -= 1 else: break lyst[j + 1] = itemToInsert i += 1 |
def quicksort( lyst ): """ The quicksort algorithm """ quicksortHelper( lyst, 0, len( lyst ) - 1 )
def quicksortHelper( lyst, left, right ): """ The quicksort helper function """ if left < right: pivotLocation = partition( lyst, left, right ) quicksortHelper( lyst, left, pivotLocation - 1 ) quicksortHelper( lyst, pivotLocation + 1, right )
def partition( lyst, left, right ): """ partition the list with the middle item """
#find the pivot item and exchange it with the last item middle = ( left + right ) // 2 pivot = lyst[middle] swap(lyst, middle, right) #set boundary point to the first position boundary = left #move item less than pivot to the left for index in range( left, right ): if lyst[index] < pivot: swap( lyst, index, boundary ) boundary += 1
#exchange the pivot item and boundary item, then return the boundary swap( lyst, right, boundary ) return boundary |
from arrays import Array
def mergeSort( lyst ): """The mergeSort algorithm."""
#copyBuffer temporary space needed during merge copyBuffer = Array( len( lyst ) ) mergeSortHelper( lyst, copyBuffer, 0, len( lyst ) - 1 )
def mergeSortHelper( lyst, copyBuffer, left, right ): """ The helper function of mergeSort algorithm """
if left < right: middle = ( left + right ) // 2 mergeSortHelper( lyst, copyBuffer, left, middle ) mergeSortHelper( lyst, copyBuffer, middle + 1, right ) merge( lyst, copyBuffer, left, middle, right )
def merge( lyst, copyBuffer, left, middle, right ): """ merge two sorted list into a big one """ #i1: The start of the sublist1 #i2: The start of the sublist2
i1 = left i2 = middle + 1 for i in range( left, right + 1 ): if i1 > middle: copyBuffer[i] = lyst[i2] i2 += 1 elif i2 > right: copyBuffer[i] = lyst[i1] i1 += 1 elif lyst[i1] < lyst[i2]: copyBuffer[i] = lyst[i1] i1 += 1 else: copyBuffer[i] = lyst[i2] i2 += 1
for i in range( left, right + 1 ): lyst[i] = copyBuffer[i] |
""" File: arrays.py
An Array is a restricted list whose clients can use only [], len, iter, and str.
To instantiate, use
<variable> = array(<capacity>, <optional fill value>)
The fill value is None by default. """
class Array(object): """Represents an array."""
def __init__(self, capacity, fillValue = None): """Capacity is the static size of the array. fillValue is placed at each position.""" self._items = list() for count in range(capacity): self._items.append(fillValue)
def __len__(self): """-> The capacity of the array.""" return len(self._items)
def __str__(self): """-> The string representation of the array.""" return str(self._items)
def __iter__(self): """Supports iteration over a view of an array.""" return iter(self._items)
def __getitem__(self, index): """Subscript operator for access at index.""" return self._items[index]
def __setitem__(self, index, newItem): """Subscript operator for replacement at index.""" self._items[index] = newItem |
def fibonacci( n ): """ The liner algorithm of fibonacci """
first = 1 second = 1 count = 3 while count <= n: sum = first + second first = second second = sum count += 1
return sum |
探查器方法 | 作用 |
p.test( function, lyst = None, size = 10, unique = True, comp = Ture, exch = True, trace = False) | 用给定的设置运行Function,并输出结果 |
p.comparison() | 自增比较次数 |
p.exchange() | 自增交换次数 |
p.__str__() | 和 str(p) 相同 |
标签:中间 number 有序 vertica 二分 ges mbed ems 统计
原文地址:https://www.cnblogs.com/lijunjie9502/p/9892514.html