tags: Python Quick sort Sorting Algorithm Sort
# Use the list of rapid sorting to the following list
alist = [30, 24, 5, 58, 18, 36, 12, 42, 39]
def sort_value(alist, start=0, end=len(alist) - 1):
'''
: Param alist: A list of sorting
: Param Start: The default start position is the subscript of the first element of the list
: param end: The default end position is the subscript of the last element of the list
:return:
'''
# If the list has only one element, or
if start >= end:
return
# Define a pointer MID and initially pointing the pointer to the first element of the list.
mid = alist[start]
# Define a pointer PRE pointing list START location
pre = start
# Defines a pointer next point to the list of Len (ALIST) -1 positions
next = end
# If the pre pointer is before, the Next pointer is behind, enter the loop
while pre < next:
# If the list of NEXT points to the element greater than the element pointing to the MID, then the element pointed to by NEXT is not movable.
while alist[next] > mid and pre < next:
# And move the Next pointer forward one
next -= 1
# If you enter the above loop, then the position pointed by the pointer before NEXT-1 does not move, and put the position of the element points pointed to the pointer to the pre-pointer to the PRE pointer.
# If you do not enter the above loop, you assign the element points to the element pointing to the pre-pointer.
alist[pre] = alist[next]
# If the element pointed to by the list is less than or equal to the element pointed to by the MID, then the element pointed to by the pre-pointed
while alist[pre] <= mid and pre < next:
# And move the Pre pointer backward
pre += 1
# If you enter the above loop, then the position pointed by the pointer before pre + 1 does not move, and put the position of the element pointing to the pointer to the NEXT pointer to the position of the pointer.
# If you do not enter the above loop, you assign the element points to the Ele pointer to the PRE pointer.
alist[next] = alist[pre]
# , , , , , ,
#
alist[pre] = mid
# Tune the above function, sort the elements of the left and right sides
sort_value(alist, start, next - 1)
sort_value(alist, pre + 1, end)
sort_value(alist)
print(alist)
Fast sorting Unstable sorting, average time complexity o (nlgn) Bubble Sort The average time complexity o (n^2), the best time complexity O (n), that is, when the array is completely ordered. Select s...
Algorithm efficiency. Generally divided into time efficiency (time complexity), the space efficiency (space complexity) two. time complexityThe main measure of aRunning speed of the algorithm,andSpace...
Sort 1. Basic concept of sorting 1. Stability 2. Internal sorting and external sorting 3. Performance analysis 2. Insert and sort 1. Thought 2. Time complexity analysis 3. Example code 4. Code analysi...
Thinking There are array [26, -3, 14, -15, 0, 324, 98, 1, 22] The array is now sorted and the fast sorting algorithm is used. Come and repeat ideas and steps first: Select the benchmark; Those who are...
Bubbling order time complexity: The time complexity of bubbling sort is O (n ^ 2) Skating sorting principle: Compare adjacent elements. If the previous element is larger than the latter element, the p...
Sorting is roughly divided into two categories: comparative sorting and non-comparative sorting 1. Basic realization of each sort 1. Direct insertion sort and Hill sort 2. Select sort and heap sort 3....
Detailed explanation of the time complexity of the data structure algorithm Progressive growth of function Algorithm time complexity Definition of algorithm time complexity Method of deriving the big ...
Hill Sort (Shellsort) Hill sorting is one of the first algorithms to break the second time barrier. Hill sorting works by comparing elements separated by a certain interval; the distance used for each...
Quick sorting was proposed by C. A. R. Hoare in 1962. Its basic idea is:The data to be sorted is divided into two separate parts by one sorting, in which all the data of one part is smaller than all t...