Sorting Algorithms python (b)

HEAPSORT # 
# basic idea: 1. The initial list of numbers to be sorted (R1, R2, ..., Rn ) constructed to be larger than the top of the stack, this stack is the initial unordered list
# 2. The top of the stack and the last element R1 a switching element Rn, this time to obtain a new unordered list (R1, R2, ..., Rn -1) and the new ordered list (Rn)
# 3. Since the top of the stack after an exchange of the new stack may violate R1 nature, it is necessary for this unordered list (R1, R2, ..., Rn -1) adapts to the new stack,
# R1 and unordered list and then again the last switching element, to obtain new unordered list (R1, R2, ..., Rn-2) and the new ordered list (. 1-Rn of, Rn of).
# this process is repeated until the number of elements is an ordered list of n-1, the entire sorting process is complete.

By comparing the #sift_down from the size of each node and its child node (Ri <R2i, Ri <R2i + 1), the maximum number of the top of the stack placed
DEF sift_down (numlist, Start, End):
the root = Start
the while true:
Child = 2 * the root +. 1
IF Child> End:
BREAK
IF Child +. 1 <= End and numlist [Child] <numlist [Child +. 1]:
Child + =. 1
IF numlist [the root] <numlist [Child]:
numlist [the root], numlist [Child] = numlist [Child], numlist [the root]
the root = Child
the else:
BREAK

DEF heap_sort (numlist):
# initial stack top of a list of a large
first = len (numList) // 2 - 1
Start in the reversed for (Range (0, First +. 1)):
sift_down (numlist, Start, len (numlist) -1)
Print (numlist)
# to place the maximum number of the last, before the n-1 element heap adjusted
for in End Range (len (numlist) -1, 0, -1):
numlist [0], numlist [End] = numlist [End], numlist [0]
sift_down (numlist, 0,. 1-End)
Print ( numlist)
return numlist
Print (heap_sort ([16,. 7, 3,20,17,8,99,76]))
# [99, 76, 16, 20 is,. 17,. 8,. 3,. 7]
# [76, 20, 16, 7, 17, 8, 3, 99]
# [20, 17, 16, 7, 3, 8, 76, 99]
# [17, 8, 16, 7, 3, 20, 76, 99]
# [16, 8, 3, 7, 17, 20, 76, 99]
# [8, 7, 3, 16, 17, 20, 76, 99]
# [7, 3, 8, 16, 17, 20, 76, 99]
# [3, 7, 8, 16, 17, 20, 76, 99]


# merge sort:
# basic idea: first merge sort using a dichotomy, in the final analysis ideas or divide and rule. Get a long array to keep the two divided left and right,
# then this recursive points down. And then they look like two ordered arrays according to merge.
Example # [4,7,8,3,5,9]
# Step: Score: [4,8,7] [3,9,5] -> [4,8] [7] [3,9 ] [5] -> [4] [8] [7] [3] [9] [5]
# Step: and: [4,8] [7] [3,9] [5] -> [ 4,7,8] [3,5,9] -> [3,4,5,7,8,9]

DEF Merge (a, b): In this case a and b # are already sorted list
c = []
H = J = 0
the while J <len (a) and H <len (b): # sequentially comparing a or b until all elements are drained
if a [j] [h] <b:
c.append (a [J])
J + =. 1
the else:
c.append ([H] b)
H + =. 1
IF J == len (a):
# When all the elements in a row over the remaining Lane b sequentially into elements C
for I in b [H:]:
c.append (I)
the else:
# b when all elements in the row is completed, the remaining elements in a sequence into C
for I in a [J :]:
c.append (I)
return C

# recursion
DEF mergesort (numlist):
Print (numlist)
IF len (numlist) <=. 1:
return numlist
Middle = len (numlist) / 2
left = mergesort (numlist [: Middle])
right = mergesort (numlist [Middle:])
return Merge (left, right)
print(mergesort([1,2,-1,8,6,7,-2]))
# [1, 2, -1, 8, 6, 7, -2]
# [1, 2, -1]
# [1]
# [2, -1]
# [2]
# [-1]
# [8, 6, 7, -2]
# [8, 6]
# [8]
# [6]
# [7, -2]
# [7]
# [-2]

#遍历法
def mergesort1(numList):
lenList = len(numList)
while lenList>1:
newList = []
if lenList % 2 == 0:
for i in range(lenList/2):
a = [numList[2*i]] if type(numList[2*i]) == int else numList[2*i]
b = [numList[2*i+1]] if type(numList[2*i+1]) == int else numList[2*i+1]
newList.append(merge(a, b))
numList = newList
lenList = len(numList)
else:
for i in range(lenList/2):
a = [numList[2 * i]] if type(numList[2*i]) == int else numList[2 * i]
b = [numList[2 * i + 1]] if type(numList[2*i+1]) == int else numList[2 * i + 1]
newList.append(merge(a, b))
last = [numList[-1]] if type(numList[-1]) == int else numList[-1]
newList.append(last)
numList = newList
lenList = len(numList)
print(newList)
print(lenList)
return numList[0]
#print(mergesort1([1,2,-1,8,7,6,-2]))
Print (mergesort1 ([1,2, -1,8,6,7, -2, -13,58,30, -15,18]))
# [[. 1, 2], [-1,. 8], [6, 7], [-2]]
# 4
# [[-1, 1, 2, 8], [-2, 6, 7]]
# 2
# [[-2, -1, 1, 2, 6, 7, 8]]
# 1
# [-2, -1, 1, 2, 6, 7, 8]

# Hill sorting (based on direct insertion sort plus the interval)
# basic idea: one defined. step sequence as t [k, ...,. 1]
# 2. sequence number in steps of k, for k times queue sorting sort
# 3 sorted per trip, according to the corresponding step ti, a column to be sorted ti is divided into subsequences, respectively
# various sub-sequences direct insertion sort.

insertsort DEF (numlist):
Leng = len (numlist)
GAP = Leng
the while (GAP>. 1):
GAP = GAP 2 //
Print (GAP)
for I in Range (GAP, Leng):
IF numlist [I] <numlist [ GAP-I]:
= [I] midnum numlist
j=i-gap
while numList[j] >midnum and j >= 0:
numList[j+gap] = numList[j]
j -=gap
numList[j + gap] = midnum
print(numList)
return numList
print(insertsort([1,2,-1,8,6,7,-2,-13]))
[1,2,-1,8,6,7,-2,-13]
Interval # 4 
# [1, 2 , -1, 8, 6, 7 , -2, -13]
# [1, 2, -1 , 8, 6, 7, -2 , -13]
# [1, 2, -2, 8 , 6, 7, -1, -13 ]
# [ 1 , 2, -2 , -13, 6, 7, -1, 8]
# interval 2
# [-2, 2 , 1 , -13 , 6, 7, -1, 8]
# [ -2 , -13, 1 , 2, 6 , 7, -1, 8]
# [-2, -13 , 1, 2 , 6, 7 , -1, 8]
# [ -2 , -13, 1, 2, 6 , 7, -1 , 8]
# [-2, -13 , -1, 2 , 1, 7 , 6, 8 ]
# [ -2 , -13 , -1, 2, 1, 7, 6, 8]
# interval is 1
# [ -13 , -2 , -1 , 2, 1, 7, 6, 8]
# [ -13 , -2 , -1 , 2 , 1, 7, 6, 8]
# [ -13 , -2 , -1 , 2 , 1 , 7, 6, 8]
# [ -13 , -2, -1, 1, 2, 7, 6, 8]
# [-13, -2, -1, 1, 2, 7, 6, 8]
# [-13, -2, -1, 1, 2, 6, 7, 8]
# [-13, -2, -1, 1, 2, 6, 7, 8]

Guess you like

Origin www.cnblogs.com/xhw19950606/p/12301081.html
Recommended