Python data structures and algorithms - quick sort

quicksort

Quicksort (English: Quicksort), also known as partition-exchange sort (partition-exchange sort), divides the data to be sorted into two independent parts by one pass, and all the data in one part is smaller than all the data in the other part. , and then quickly sort the two parts of the data by this method. The entire sorting process can be performed recursively, so that the entire data becomes an ordered sequence.

The steps are:

  1. Pick out an element from the sequence, called a "pivot",
  2. Reorder the sequence, all elements smaller than the reference value are placed in front of the reference, and all elements larger than the reference value are placed at the back of the reference (same numbers can go to either side). After this partition ends, the benchmark is in the middle of the sequence. This is called a partition operation.
  3. Recursively sort the subarrays of elements smaller than the reference value and the subarrays of elements larger than the reference value.

The bottom case of recursion is that the size of the sequence is zero or one, which is always sorted. Although it keeps recursing, the algorithm will always end, because in each iteration, it will put at least one element to its last position.

Analysis of Quick Sort


def quick_sort(alist,start,end):
    #The recursive condition is satisfied only when end is larger than start. When there is only one element, there is no need to execute the recursive condition.
    if start>=end:
        return
    mid_value=alist[start]
    low=start
    high=end
    while low<high:
        #If the value of the element pointed to by high is larger than mid_value, this value does not need any operation, the cursor of high goes directly to the left, and exits when it encounters a value smaller than the reference value, giving the value to low
        while low<high and alist[high]>=mid_value:
            high-=1
        alist[low]=alist[high]
        # If the value of the element pointed to by low is smaller than mid_value, this value does not need any operation, the cursor of low moves directly to the right, the cursor of low moves to the right, and if it encounters a value larger than the reference value, give high
        while low<high and alist[low]<mid_value:
            low+=1
        alist[high]=alist[low]
    alist[low]=mid_value
    quick_sort(alist,start,low)
    quick_sort(alist,high+1,end)
alist = [54,226,93,17,77,31,44,55,44,20]
quick_sort (alist, 0, len (alist) -1)
print(alist)
def quick_sort1(alist):
    if len(alist)<=1:
        return alist
    else:
        mid_value=alist[0]
        low=[i for i in alist[1:] if i<=mid_value]
        high=[i for i in alist[1:] if i>mid_value]
        return quick_sort1(low)+[mid_value]+quick_sort1(high)

alist = [54,226,93,17,77,31,44,55,44,20]
print(quick_sort1(alist))

[17, 20, 31, 44, 44, 54, 55, 77, 93, 226]

time complexity

  • Optimal time complexity: O(nlogn)
  • Worst time complexity: O(n 2 )
  • Stability: Unstable

It's not obvious that quicksort takes O(n log n) time on average from the start. But it is not difficult to observe the partition operation, the elements of the array are visited once in each loop, using O(n) time. In the version using concatenation, this operation is also O(n).

In the best case, every time we run a partition, we divide an array into two nearly equal pieces. This means that each recursive call processes an array of half the size. Therefore, we only need to make log n nested calls before reaching an array of size one. This means that the depth of the call tree is O(log n). But in two program calls of the same hierarchy, the same part of the original sequence is not processed; therefore, each hierarchy of program calls in total only takes O(n) time (each call has some common Extra cost, but since there are only O(n) calls per hierarchy, these are summed up in O(n) coefficients). The result is that this algorithm only takes O(n log n) time.

Quick Sort Demo



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325652141&siteId=291194637