1.sorts method python (8)quick_sort

快排序原理

https://www.cnblogs.com/yangecnu/p/Introduce-Quick-Sort.html

my stupid code:

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 31 10:14:28 2019

@author: yue zhang

E-mails: [email protected]
""" 
def Quick_sort(data,num1):
         data_num = len(data)
         num=num1
         temp = True
         while temp:
               temp = False
               for i in range(num,data_num):
                     if data_num-1-i > num and data[data_num-1-i]data[num]:
                           data[num],data[i] = data[i],data[num]
                           num = i
                           temp = True
                           break               
         
            
         if num-1 > 0:  
               data[:num] = Quick_sort(data[:num],num1)   
         if num+1 

answer:https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort.py

"""
This is a pure python implementation of the quick sort algorithm
For doctests run following command:
python -m doctest -v quick_sort.py
or
python3 -m doctest -v quick_sort.py
For manual testing run:
python quick_sort.py
"""
def quick_sort(collection):
    """Pure implementation of quick sort algorithm in Python
    :param collection: some mutable ordered collection with heterogeneous
    comparable items inside
    :return: the same collection ordered by ascending
    Examples:
    >>> quick_sort([0, 5, 3, 2, 2])
    [0, 2, 2, 3, 5]
    >>> quick_sort([])
    []
    >>> quick_sort([-2, -5, -45])
    [-45, -5, -2]
    """
    length = len(collection)
    if length <= 1:
        return collection
    else:
        # Use the last element as the first pivot
        pivot = collection.pop()
        # Put elements greater than pivot in greater list
        # Put elements lesser than pivot in lesser list
        greater, lesser = [], []
        for element in collection:
            if element > pivot:
                greater.append(element)
            else:
                lesser.append(element)
        return quick_sort(lesser) + [pivot] + quick_sort(greater)


if __name__ == '__main__':
    user_input = input('Enter numbers separated by a comma:\n').strip()
    unsorted = [ int(item) for item in user_input.split(',') ]
    print( quick_sort(unsorted) )

list.pop:

https://www.programiz.com/python-programming/methods/list/pop

3 way quick sort:

原理: https://blog.csdn.net/kzq_qmi/article/details/46454865

code:https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort.py

 

你可能感兴趣的:(python,Coding,Practice)