From 85ec055f8915551797ccff800b53a2478efe7fec Mon Sep 17 00:00:00 2001 From: CorningSun Date: Wed, 3 Jan 2018 11:45:00 +0800 Subject: [PATCH] =?UTF-8?q?python=20selectionSort=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- 2.selectionSort.md | 13 +++- src/pythonSortTest.py | 150 +++++++++++++++++++++---------------- test/pythonSortTest.py | 165 ----------------------------------------- 4 files changed, 97 insertions(+), 234 deletions(-) delete mode 100644 test/pythonSortTest.py diff --git a/.gitignore b/.gitignore index 1a366fb..7297f31 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ _book # eBook build output *.epub *.mobi -*.pdf \ No newline at end of file +*.pdf +\.idea/ diff --git a/2.selectionSort.md b/2.selectionSort.md index 1c3ddbc..6e3b3a4 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -42,10 +42,15 @@ function selectionSort(arr) { ```python def selectionSort(arr): - for i in range(len(arr)-1): - for j in range(i+1, len(arr)): - if arr[j] < arr[i]: - arr[i], arr[j] = arr[j], arr[i] + for i in range(len(arr) - 1): + # 记录最小数的索引 + minIndex = i + for j in range(i + 1, len(arr)): + if arr[j] < arr[minIndex]: + minIndex = j + # i 不是最小数时,将 i 和最小数进行交换 + if i != minIndex: + arr[i], arr[minIndex] = arr[minIndex], arr[i] return arr ``` diff --git a/src/pythonSortTest.py b/src/pythonSortTest.py index eb5b137..ea765d2 100644 --- a/src/pythonSortTest.py +++ b/src/pythonSortTest.py @@ -2,77 +2,89 @@ # Create by LokiSharp(loki.sharp#gmail) at 2017-1-22 ''' -TOTAL=5000 +TOTAL = 5000 + def sortTest(func, total=1000): import random, copy, operator, math, time - arrList = [i for i in range(-math.floor(total/2),math.ceil(total/2))] + arrList = [i for i in range(-math.floor(total / 2), math.ceil(total / 2))] arrListR = copy.deepcopy(arrList) - while operator.eq(arrList,arrListR): + while operator.eq(arrList, arrListR): random.shuffle(arrListR) - #print("--- [Origin List]", arrList, "Use", func.__name__,"with Total:", len(arrList)) - #print("--> [Random List]", arrListR, "Use", func.__name__,"with Total:", len(arrList)) + # print("--- [Origin List]", arrList, "Use", func.__name__,"with Total:", len(arrList)) + # print("--> [Random List]", arrListR, "Use", func.__name__,"with Total:", len(arrList)) start = time.clock() arrListR = func(arrListR) end = time.clock() - runtime = end-start - #print("--> [Sorted List]", arrListR, "Use", func.__name__,"with Total:", len(arrList)) + runtime = end - start + # print("--> [Sorted List]", arrListR, "Use", func.__name__,"with Total:", len(arrList)) if operator.eq(arrList, arrListR): - print("[Success]", func.__name__,"with Total:", len(arrList),"in %.5fs" % runtime) + print("[Success]", func.__name__, "with Total:", len(arrList), "in %.5fs" % runtime) return True else: - print("[Fail]", func.__name__,"with Total:", len(arrList),"in %.5fs" % runtime) + print("[Fail]", func.__name__, "with Total:", len(arrList), "in %.5fs" % runtime) return False + def bubbleSort(arr): for i in range(1, len(arr)): - for j in range(0, len(arr)-i): - if arr[j] > arr[j+1]: + for j in range(0, len(arr) - i): + if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr + def selectionSort(arr): - for i in range(len(arr)-1): - for j in range(i+1, len(arr)): - if arr[j] < arr[i]: - arr[i], arr[j] = arr[j], arr[i] + for i in range(len(arr) - 1): + # 记录最小数的索引 + minIndex = i + for j in range(i + 1, len(arr)): + if arr[j] < arr[minIndex]: + minIndex = j + # i 不是最小数时,将 i 和最小数进行交换 + if i != minIndex: + arr[i], arr[minIndex] = arr[minIndex], arr[i] return arr + def insertionSort(arr): for i in range(len(arr)): - preIndex = i-1 + preIndex = i - 1 current = arr[i] while preIndex >= 0 and arr[preIndex] > current: - arr[preIndex+1] = arr[preIndex] - preIndex-=1 - arr[preIndex+1] = current + arr[preIndex + 1] = arr[preIndex] + preIndex -= 1 + arr[preIndex + 1] = current return arr + def shellSort(arr): import math - gap=1 - while(gap < len(arr)/3): - gap = gap*3+1 + gap = 1 + while (gap < len(arr) / 3): + gap = gap * 3 + 1 while gap > 0: - for i in range(gap,len(arr)): + for i in range(gap, len(arr)): temp = arr[i] - j = i-gap - while j >=0 and arr[j] > temp: - arr[j+gap]=arr[j] - j-=gap - arr[j+gap] = temp - gap = math.floor(gap/3) + j = i - gap + while j >= 0 and arr[j] > temp: + arr[j + gap] = arr[j] + j -= gap + arr[j + gap] = temp + gap = math.floor(gap / 3) return arr + def mergeSort(arr): import math - if(len(arr)<2): + if (len(arr) < 2): return arr - middle = math.floor(len(arr)/2) + middle = math.floor(len(arr) / 2) left, right = arr[0:middle], arr[middle:] return merge(mergeSort(left), mergeSort(right)) -def merge(left,right): + +def merge(left, right): result = [] while left and right: if left[0] <= right[0]: @@ -85,38 +97,43 @@ def merge(left,right): result.append(right.pop(0)); return result + def quickSort(arr, left=None, right=None): - left = 0 if not isinstance(left,(int, float)) else left - right = len(arr)-1 if not isinstance(right,(int, float)) else right + left = 0 if not isinstance(left, (int, float)) else left + right = len(arr) - 1 if not isinstance(right, (int, float)) else right if left < right: partitionIndex = partition(arr, left, right) - quickSort(arr, left, partitionIndex-1) - quickSort(arr, partitionIndex+1, right) + quickSort(arr, left, partitionIndex - 1) + quickSort(arr, partitionIndex + 1, right) return arr + def partition(arr, left, right): pivot = left - index = pivot+1 + index = pivot + 1 i = index - while i <= right: + while i <= right: if arr[i] < arr[pivot]: swap(arr, i, index) - index+=1 - i+=1 - swap(arr,pivot,index-1) - return index-1 + index += 1 + i += 1 + swap(arr, pivot, index - 1) + return index - 1 + def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i] + def buildMaxHeap(arr): import math - for i in range(math.floor(len(arr)/2),-1,-1): - heapify(arr,i) + for i in range(math.floor(len(arr) / 2), -1, -1): + heapify(arr, i) + def heapify(arr, i): - left = 2*i+1 - right = 2*i+2 + left = 2 * i + 1 + right = 2 * i + 2 largest = i if left < arrLen and arr[left] > arr[largest]: largest = left @@ -127,39 +144,44 @@ def heapify(arr, i): swap(arr, i, largest) heapify(arr, largest) + def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i] + def heapSort(arr): global arrLen arrLen = len(arr) buildMaxHeap(arr) - for i in range(len(arr)-1,0,-1): - swap(arr,0,i) - arrLen -=1 + for i in range(len(arr) - 1, 0, -1): + swap(arr, 0, i) + arrLen -= 1 heapify(arr, 0) return arr + def countingSort(arr, maxValue=None): - bucketLen = maxValue+1 - bucket = [0]*bucketLen - sortedIndex =0 + bucketLen = maxValue + 1 + bucket = [0] * bucketLen + sortedIndex = 0 arrLen = len(arr) for i in range(arrLen): if not bucket[arr[i]]: - bucket[arr[i]]=0 - bucket[arr[i]]+=1 + bucket[arr[i]] = 0 + bucket[arr[i]] += 1 for j in range(bucketLen): - while bucket[j]>0: + while bucket[j] > 0: arr[sortedIndex] = j - sortedIndex+=1 - bucket[j]-=1 + sortedIndex += 1 + bucket[j] -= 1 return arr -sortTest(bubbleSort, TOTAL) -sortTest(selectionSort, TOTAL) -sortTest(insertionSort, TOTAL) -sortTest(shellSort, TOTAL) -sortTest(mergeSort, TOTAL) -sortTest(quickSort, TOTAL) -sortTest(heapSort, TOTAL) + +if __name__ == '__main__': + sortTest(bubbleSort, TOTAL) + sortTest(selectionSort, TOTAL) + sortTest(insertionSort, TOTAL) + sortTest(shellSort, TOTAL) + sortTest(mergeSort, TOTAL) + sortTest(quickSort, TOTAL) + sortTest(heapSort, TOTAL) diff --git a/test/pythonSortTest.py b/test/pythonSortTest.py deleted file mode 100644 index eb5b137..0000000 --- a/test/pythonSortTest.py +++ /dev/null @@ -1,165 +0,0 @@ -''' -# Create by LokiSharp(loki.sharp#gmail) at 2017-1-22 -''' - -TOTAL=5000 - -def sortTest(func, total=1000): - import random, copy, operator, math, time - arrList = [i for i in range(-math.floor(total/2),math.ceil(total/2))] - arrListR = copy.deepcopy(arrList) - while operator.eq(arrList,arrListR): - random.shuffle(arrListR) - #print("--- [Origin List]", arrList, "Use", func.__name__,"with Total:", len(arrList)) - #print("--> [Random List]", arrListR, "Use", func.__name__,"with Total:", len(arrList)) - start = time.clock() - arrListR = func(arrListR) - end = time.clock() - runtime = end-start - #print("--> [Sorted List]", arrListR, "Use", func.__name__,"with Total:", len(arrList)) - if operator.eq(arrList, arrListR): - print("[Success]", func.__name__,"with Total:", len(arrList),"in %.5fs" % runtime) - return True - else: - print("[Fail]", func.__name__,"with Total:", len(arrList),"in %.5fs" % runtime) - return False - -def bubbleSort(arr): - for i in range(1, len(arr)): - for j in range(0, len(arr)-i): - if arr[j] > arr[j+1]: - arr[j], arr[j + 1] = arr[j + 1], arr[j] - return arr - -def selectionSort(arr): - for i in range(len(arr)-1): - for j in range(i+1, len(arr)): - if arr[j] < arr[i]: - arr[i], arr[j] = arr[j], arr[i] - return arr - -def insertionSort(arr): - for i in range(len(arr)): - preIndex = i-1 - current = arr[i] - while preIndex >= 0 and arr[preIndex] > current: - arr[preIndex+1] = arr[preIndex] - preIndex-=1 - arr[preIndex+1] = current - return arr - -def shellSort(arr): - import math - gap=1 - while(gap < len(arr)/3): - gap = gap*3+1 - while gap > 0: - for i in range(gap,len(arr)): - temp = arr[i] - j = i-gap - while j >=0 and arr[j] > temp: - arr[j+gap]=arr[j] - j-=gap - arr[j+gap] = temp - gap = math.floor(gap/3) - return arr - -def mergeSort(arr): - import math - if(len(arr)<2): - return arr - middle = math.floor(len(arr)/2) - left, right = arr[0:middle], arr[middle:] - return merge(mergeSort(left), mergeSort(right)) - -def merge(left,right): - result = [] - while left and right: - if left[0] <= right[0]: - result.append(left.pop(0)); - else: - result.append(right.pop(0)); - while left: - result.append(left.pop(0)); - while right: - result.append(right.pop(0)); - return result - -def quickSort(arr, left=None, right=None): - left = 0 if not isinstance(left,(int, float)) else left - right = len(arr)-1 if not isinstance(right,(int, float)) else right - if left < right: - partitionIndex = partition(arr, left, right) - quickSort(arr, left, partitionIndex-1) - quickSort(arr, partitionIndex+1, right) - return arr - -def partition(arr, left, right): - pivot = left - index = pivot+1 - i = index - while i <= right: - if arr[i] < arr[pivot]: - swap(arr, i, index) - index+=1 - i+=1 - swap(arr,pivot,index-1) - return index-1 - -def swap(arr, i, j): - arr[i], arr[j] = arr[j], arr[i] - -def buildMaxHeap(arr): - import math - for i in range(math.floor(len(arr)/2),-1,-1): - heapify(arr,i) - -def heapify(arr, i): - left = 2*i+1 - right = 2*i+2 - largest = i - if left < arrLen and arr[left] > arr[largest]: - largest = left - if right < arrLen and arr[right] > arr[largest]: - largest = right - - if largest != i: - swap(arr, i, largest) - heapify(arr, largest) - -def swap(arr, i, j): - arr[i], arr[j] = arr[j], arr[i] - -def heapSort(arr): - global arrLen - arrLen = len(arr) - buildMaxHeap(arr) - for i in range(len(arr)-1,0,-1): - swap(arr,0,i) - arrLen -=1 - heapify(arr, 0) - return arr - -def countingSort(arr, maxValue=None): - bucketLen = maxValue+1 - bucket = [0]*bucketLen - sortedIndex =0 - arrLen = len(arr) - for i in range(arrLen): - if not bucket[arr[i]]: - bucket[arr[i]]=0 - bucket[arr[i]]+=1 - for j in range(bucketLen): - while bucket[j]>0: - arr[sortedIndex] = j - sortedIndex+=1 - bucket[j]-=1 - return arr - -sortTest(bubbleSort, TOTAL) -sortTest(selectionSort, TOTAL) -sortTest(insertionSort, TOTAL) -sortTest(shellSort, TOTAL) -sortTest(mergeSort, TOTAL) -sortTest(quickSort, TOTAL) -sortTest(heapSort, TOTAL)