From 4a672429ba3e2d9b460845fcd0d10ab2d0887544 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 21:03:04 +0800 Subject: [PATCH 01/14] Add Python --- 1.bubbleSort.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index d3871e0..f14d595 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -47,4 +47,16 @@ function bubbleSort(arr) { } return arr; } -``` \ No newline at end of file +``` + + +## 5. Python 代码实现 + +```python +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 +``` From 7834c2699ea129a3fbaa8a60991f859aa2ae1c91 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 21:05:42 +0800 Subject: [PATCH 02/14] Add Python --- 2.selectionSort.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/2.selectionSort.md b/2.selectionSort.md index b9011c1..e471732 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -36,4 +36,15 @@ function selectionSort(arr) { } return arr; } -``` \ No newline at end of file +``` + +## 4. Python 代码实现 + +```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] + return arr +``` From 9d302a208c180b2e9d6dfd037f300916666d7c47 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 21:07:03 +0800 Subject: [PATCH 03/14] Update 3.insertionSort.md --- 3.insertionSort.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/3.insertionSort.md b/3.insertionSort.md index a5f6e61..3a66a0f 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -34,4 +34,18 @@ function insertionSort(arr) { } return arr; } -``` \ No newline at end of file +``` + +## 3. Python 代码实现 + +```python +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 +''' From d41475ab7cdcc0fb178bad7ef630f07f08c4b800 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 21:07:56 +0800 Subject: [PATCH 04/14] Add Python --- 4.shellSort.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/4.shellSort.md b/4.shellSort.md index 4e9647e..ca07d5e 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -40,4 +40,25 @@ function shellSort(arr) { } return arr; } -``` \ No newline at end of file +``` + +## 2. Python 代码实现 + +```python +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 +} +``` From f3e55d5109e17ff3398a8d218b61882506c23390 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 21:09:25 +0800 Subject: [PATCH 05/14] Add Python --- 5.mergeSort.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index c3d5c5e..8fabb83 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -70,4 +70,30 @@ function merge(left, right) return result; } -``` \ No newline at end of file +``` + +## 4. Python 代码实现 + +```python +def mergeSort(arr): + import math + if(len(arr)<2): + return arr + middle = math.floor(len(arr)/2) + left = arr[0:middle] + right = 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 +``` From cbc9a8ca49f2de0bbf6d55b058a0206788204290 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 21:15:16 +0800 Subject: [PATCH 06/14] Fix --- 5.mergeSort.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index 8fabb83..1e71e09 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -72,7 +72,7 @@ function merge(left, right) } ``` -## 4. Python 代码实现 +## 5. Python 代码实现 ```python def mergeSort(arr): @@ -80,8 +80,7 @@ def mergeSort(arr): if(len(arr)<2): return arr middle = math.floor(len(arr)/2) - left = arr[0:middle] - right = arr[middle:] + left, right = arr[0:middle], arr[middle:] return merge(mergeSort(left), mergeSort(right)) def merge(left,right): From 25b3d4e02c6752bd0d1b92dfba5ca73bb2e73e76 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 21:15:56 +0800 Subject: [PATCH 07/14] Fix --- 4.shellSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/4.shellSort.md b/4.shellSort.md index ca07d5e..802fe0d 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -42,7 +42,7 @@ function shellSort(arr) { } ``` -## 2. Python 代码实现 +## 3. Python 代码实现 ```python def shellSort(arr): From 1eb34183ad87342e45b1b0116eed27aae5d33e35 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 21:16:15 +0800 Subject: [PATCH 08/14] Update 3.insertionSort.md --- 3.insertionSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3.insertionSort.md b/3.insertionSort.md index 3a66a0f..be48d76 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -36,7 +36,7 @@ function insertionSort(arr) { } ``` -## 3. Python 代码实现 +## 4. Python 代码实现 ```python def insertionSort(arr): From e832ecef28ea4100e03c2dc365dceef44e95f2f8 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 22:57:10 +0800 Subject: [PATCH 09/14] Add Python --- 6.quickSort.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/6.quickSort.md b/6.quickSort.md index 4bdd11e..d5201ba 100644 --- a/6.quickSort.md +++ b/6.quickSort.md @@ -62,4 +62,34 @@ function swap(arr, i, j) { arr[i] = arr[j]; arr[j] = temp; } -``` \ No newline at end of file +``` + + +## 4. Python 代码实现 + +```python +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 + print(left,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] +``` From bb97a1e2005582f6973fa195832c82823c9be0d8 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 23:58:59 +0800 Subject: [PATCH 10/14] Add Python --- 7.heapSort.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/7.heapSort.md b/7.heapSort.md index f6e7e76..2b42e06 100644 --- a/7.heapSort.md +++ b/7.heapSort.md @@ -71,4 +71,38 @@ function heapSort(arr) { } return arr; } -``` \ No newline at end of file +``` +## 4. Python 代码实现 + +```python +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 +``` From d29ac91eec825cbd369a1e9b07febe9bf0a900e4 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sat, 21 Jan 2017 23:59:22 +0800 Subject: [PATCH 11/14] Fix --- 6.quickSort.md | 1 - 1 file changed, 1 deletion(-) diff --git a/6.quickSort.md b/6.quickSort.md index d5201ba..23f2881 100644 --- a/6.quickSort.md +++ b/6.quickSort.md @@ -71,7 +71,6 @@ function swap(arr, i, j) { 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 - print(left,right) if left < right: partitionIndex = partition(arr, left, right) quickSort(arr, left, partitionIndex-1) From 4b2c21ffa7e23efaa10eca72ea059514708727f9 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sun, 22 Jan 2017 00:56:30 +0800 Subject: [PATCH 12/14] Add Python --- 8.countingSort.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/8.countingSort.md b/8.countingSort.md index c73909a..3beec64 100644 --- a/8.countingSort.md +++ b/8.countingSort.md @@ -32,4 +32,23 @@ function countingSort(arr, maxValue) { return arr; } -``` \ No newline at end of file +``` +## 3. JavaScript 代码实现 + +```python +def countingSort(arr, maxValue): + 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 +``` From 76b6e0353dba0505daa7ba5e50c32fd969d4a1ca Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sun, 22 Jan 2017 13:04:17 +0800 Subject: [PATCH 13/14] Create pythonSortTest.py --- pythonSortTest.py | 165 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 pythonSortTest.py diff --git a/pythonSortTest.py b/pythonSortTest.py new file mode 100644 index 0000000..eb5b137 --- /dev/null +++ b/pythonSortTest.py @@ -0,0 +1,165 @@ +''' +# 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) From 06626459852bace797d201ed5e42273addd072f2 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Sun, 22 Jan 2017 13:04:50 +0800 Subject: [PATCH 14/14] Update 3.insertionSort.md --- 3.insertionSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3.insertionSort.md b/3.insertionSort.md index be48d76..008bbd1 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -48,4 +48,4 @@ def insertionSort(arr): preIndex-=1 arr[preIndex+1] = current    return arr -''' +```