Detailed explanation of fast sorting algorithm (code is more embarrassed, but there is low time complexity, good efficiency)

tags: Python  Quick sort  Sorting Algorithm  Sort  

The code contains a lot of comments, you can refer to it, no more

Writing a code in the next day, a quick sort algorithm is simple and easy to understand, but the time complexity is high.

# 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)

Intelligent Recommendation

Sorting algorithm and time complexity

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...

Efficiency of the algorithm, time complexity, space complexity.

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...

[Sorting] Data Structure -Detailed Explanation of Sorting Algorithm and Code (Insert, Bubble, Fast, Hill)

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...

[Algorithm] Explanation and code demonstration of fast sorting algorithms

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...

Java bubbling sorting - detailed explanation + case + time complexity

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...

More Recommendation

Sorting summary, insertion sorting selection sorting exchange sorting merge sorting count sorting time complexity space complexity stability detailed explanation

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 algorithm Algorithm knowledge that a programmer should know

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 ...

Sorting algorithm-Hill sorting diagram, code implementation and time complexity analysis

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...

Fast Sorting of Sorting Algorithms and Calculation of Time Complexity

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top