Skip to content

Commit

Permalink
sort methods
Browse files Browse the repository at this point in the history
实现了常见的排序方法
  • Loading branch information
Jack-Lee-Hiter committed Jul 4, 2016
1 parent 0b61818 commit 0731c25
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 51 deletions.
167 changes: 116 additions & 51 deletions .idea/workspace.xml

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions BubbleSort.py
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))
25 changes: 25 additions & 0 deletions InsertionSort.py
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))
31 changes: 31 additions & 0 deletions MergeSort.py
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)
33 changes: 33 additions & 0 deletions QuickSort.py
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)
12 changes: 12 additions & 0 deletions SelectionSort.py
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))
21 changes: 21 additions & 0 deletions ShellSort.py
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))
2 changes: 2 additions & 0 deletions Target Offer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Python实现剑指offer
剑指offer一书的python实现。

0 comments on commit 0731c25

Please sign in to comment.