"Algorithm Diagram" fourth chapter Quick Sort Learning Experience _ quick Sort

Source: Internet
Author: User

1. Divide and conquer (divide and conquer, d&c)

How it works: find simple baseline conditions and determine how to scale the problem to meet baseline conditions.

2. Recursive summation

def addarr (arr):
    if arr==[]: return
        0
    else: return
        Arr[0]+addarr (arr[1:])

3. Quick Sort

def quicksort (array):
    If Len (array) <2: return
        array# Baseline condition: An array that is empty or contains only one element is "ordered"
    else:
        Pivot=array [0] #递归条件
        less=[i for I-array[1:] If I <=pivot] #由所有小于基准值的元素组成的子数组
        greater=[i for I in array[1:] If I>=pivo T] #由所有大于基准值的元素组成的子数组 return
        quicksort (less) +[pivot]+quicksort (greater)

4, the understanding of the large O representation

Large O indicates time complexity often does not consider often bright, because of the growth of different orders of magnitude, the role of constants is negligible, such as simple lookup (o (n)) and binary lookup (O (logn)). But consideration must be given to the effect of constants when considering the growth of the same order of magnitude. such as quick sort and merge sort.

5. Two extreme cases of fast sequencing

Worst case scenario: The first element is selected for each datum value, the O (n) layer is total, the time required for each layer is O (n), and the algorithm runs at O (n) *o (n) =o (n^2)

Worst case scenario: Each datum value selects the intermediate element, the total O (logn) layer, the time required for each layer is O (n), and the algorithm runs for O (logn) *o (n) =o (NLOGN)


6. Summary

D&c the problem to a gradual breakdown. When using D&C to process a list, the baseline condition is most likely an empty array or an array containing only one element. When implementing a quick sort, randomly select the elements that are used as datum values. The average elapsed time for a quick sort is O (n log n). Constants in Big O notation sometimes matter, which is why a quick sort is faster than merging a sort. When comparing simple and binary lookups, constants are almost irrelevant, because O (log n) is much faster than O (n) when the list is very long.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.