Eight sort of quick sort 20191209-

1. Quick Sort

The core idea of ​​the algorithm

 Taking the number of array to be sorted first as a reference number, to establish an array of left and right left memory array is smaller than the set number of reference, right storage array is greater than the set number of reference, and left and right, respectively, of a recursive invocation sequencing. The algorithm logic is as follows:

  1. Taken as a reference number of a start number in the number of columns.
  2. Partitioning process, than the large full-count put it right, it is less than or equal to the number of full-put it on the left.
  3. And then the second step is repeated around the interval, until each section is only a number

Code to achieve 01

#encoding=utf-8
def quickSort(arr,start,end):
    if start>=end:
        return arr
    low,high = start,end
    temp = arr[low]
    while low<high:
        while low < high and arr[high]>=temp:
            high-=1
        arr[low] = arr[high]
        while low < high and arr[low]<=temp:
            low+=1
        arr[high] = arr[low]
        print("low = %s,high=%s,arr=%s"%(low,high,arr))
    arr[low] = temp
    quickSort(arr,start,low)
    quickSort(arr,low+1,end)
    return arr
arr = [3,2,1,5,6,4,3,9]
print(quickSort(arr,0,len(arr)-1))

Perform parsing 01

By comparison right to left and left to right comparison cycle, the final low = high, at this time the reference data is placed in the low position, and then the number of low and Start --- low + 1 --- end recursive calls sequence

  1. Taken to be sorted array a first number as a reference number, provided two signs: low points to the start position, the end point High
  2. Scanning forward from the high position in the list, if arr [high]> baseline, high- = 1, if arr [high] <baseline, the ARR [high] ARR assigned to [low], and from the start low back scanning
  3. Start scanning from the low back if arr [low] <baseline, low + = 1, if arr [low]> reference number, the ARR [low] ARR assigned to [high], then forward scan starts from high
  4. Repeating the above steps known low high, this time arr [low] position is a position of a reference number =, then 0-low, the number of low-len (arr) is Quicksort

Code to achieve 02

def PythonQuickSort(arr):
    if not arr:
        return arr
    temp = arr[0]
    left = [x for x in arr[1:] if x<=temp]
    right = [x for x in arr[1:] if x > temp]
    return PythonQuickSort(left)+[temp]+PythonQuickSort(right)
print(PythonQuickSort(arr))

Perform parsing 02

PythonQuickSort code implementation is based on the Python language features (variable length of the list to write code), the main idea with the code to achieve the same 01

to sum up

Time complexity: is O (nlogn)

Space complexity: quicksort recursive, recursive use of the stack, so that the space complexity is O (logn)

Stability: quick sort can not guarantee equal to the relative positions of the elements of the same, so it is unstable sorting algorithm

Guess you like

Origin www.cnblogs.com/hyj691001/p/12013406.html
Recommended