forked from Jack-Lee-Hiter/AlgorithmsByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
265 additions
and
51 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Python 实现冒泡排序 | ||
def bubbleSort(alist): | ||
for passnum in range(len(alist)-1, 0, -1): | ||
for i in range(passnum): | ||
if alist[i] > alist[i+1]: | ||
alist[i], alist[i+1] = alist[i+1], alist[i] | ||
return alist | ||
|
||
alist = [54,26,93,17,77,31,44,55,20] | ||
print(bubbleSort(alist)) | ||
|
||
# 改进的冒泡排序, 加入一个校验, 如果某次循环发现没有发生数值交换, 直接跳出循环 | ||
def modiBubbleSort(alist): | ||
exchange = True | ||
passnum = len(alist) - 1 | ||
while passnum >= 1 and exchange: | ||
exchange = False | ||
for i in range(passnum): | ||
if alist[i] > alist[i+1]: | ||
alist[i], alist[i+1] = alist[i+1], alist[i] | ||
exchange = True | ||
passnum -= 1 | ||
return alist | ||
|
||
print(bubbleSort(alist)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
def insertionSort(alist): | ||
for key, item in enumerate(alist): | ||
index = key | ||
while index > 0 and alist[index-1] > item: | ||
alist[index] = alist[index-1] | ||
index -= 1 | ||
alist[index] = item | ||
return alist | ||
|
||
alist = [54,26,93,17,77,31,44,55,20] | ||
print(insertionSort(alist)) | ||
|
||
def insertionSort2(alist): | ||
for index in range(1, len(alist)): | ||
currentvalue = alist[index] | ||
position = index | ||
|
||
while position > 0 and alist[index-1] > currentvalue: | ||
alist[index] = alist[index-1] | ||
position -= 1 | ||
alist[position] = currentvalue | ||
|
||
return alist | ||
|
||
print(insertionSort2(alist)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
def mergeSort(alist): | ||
if len(alist) > 1: | ||
mid = len(alist)//2 | ||
lefthalf = alist[:mid] | ||
righthalf = alist[mid:] | ||
|
||
mergeSort(lefthalf) | ||
mergeSort(righthalf) | ||
|
||
i = 0; j = 0; k = 0 | ||
while i < len(lefthalf) and j < len(righthalf): | ||
if lefthalf[i] < righthalf[j]: | ||
alist[k] = lefthalf[i] | ||
i += 1 | ||
else: | ||
alist[k] = righthalf[j] | ||
j += 1 | ||
k += 1 | ||
|
||
while i < len(lefthalf): | ||
alist[k] = lefthalf[i] | ||
i += 1 | ||
k += 1 | ||
while j < len(righthalf): | ||
alist[k] = righthalf[j] | ||
j += 1 | ||
k += 1 | ||
|
||
alist = [54,26,93,17,77,31,44,55,20] | ||
mergeSort(alist) | ||
print(alist) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def quickSort(alist): | ||
quickSortHelper(alist, 0, len(alist)-1) | ||
|
||
def quickSortHelper(alist, first, last): | ||
if first < last: | ||
splitPoint = partition(alist, first, last) | ||
|
||
quickSortHelper(alist, first, splitPoint-1) | ||
quickSortHelper(alist, splitPoint+1, last) | ||
|
||
def partition(alist, first, last): | ||
pivotvlue = alist[first] | ||
|
||
leftmark = first+1 | ||
rightmark = last | ||
done = False | ||
|
||
while not done: | ||
while alist[leftmark] <= pivotvlue and leftmark <= rightmark: | ||
leftmark += 1 | ||
while alist[rightmark] >= pivotvlue and rightmark >= leftmark: | ||
rightmark -= 1 | ||
|
||
if leftmark > rightmark: | ||
done = True | ||
else: | ||
alist[leftmark], alist[rightmark] = alist[rightmark], alist[leftmark] | ||
alist[rightmark], alist[first] = alist[first], alist[rightmark] | ||
return rightmark | ||
|
||
alist = [54,26,93,17,77,31,44,55,20] | ||
quickSort(alist) | ||
print(alist) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 选择排序, 纯粹练手 - -|| | ||
def selectionSort(alist): | ||
for i in range(len(alist)-1): | ||
min = i | ||
for j in range(i+1, len(alist)): | ||
if alist[j] < alist[min]: | ||
min = j | ||
alist[i], alist[min] = alist[min], alist[i] | ||
return alist | ||
|
||
alist = [54,26,93,17,77,31,44,55,20] | ||
print(selectionSort(alist)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# python实现希尔排序 | ||
def shellSort(alist): | ||
sublistcount= len(alist)//2 | ||
while sublistcount > 0: | ||
for startposition in range(sublistcount): | ||
gapInsertionSort(alist, startposition, sublistcount) | ||
sublistcount = sublistcount//2 | ||
return alist | ||
|
||
def gapInsertionSort(alist, start, gap): | ||
for i in range(start+gap, len(alist), gap): | ||
currentValue = alist[i] | ||
position = i | ||
|
||
while position >= gap and alist[position-gap] > currentValue: | ||
alist[position] = alist[position-gap] | ||
position = position-gap | ||
alist[position] = currentValue | ||
|
||
alist = [54,26,93,17,77,31,44,55,20] | ||
print(shellSort(alist)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Python实现剑指offer | ||
剑指offer一书的python实现。 |