Using Python to implement 8 sorting algorithms-Fast sorting

Source: Internet
Author: User
The basic idea of quick sorting:

By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into ordered order.

Cases:

arr = [49,38,04,97,76,13,27,49,55,65], set the first digit 49 as the key value, from right to left to find a number smaller than the key value, the number of the found assigned to the first digit;

arr = [27,38,04,97,76,13,27,49,55,65], and then from the left first bit to the right to find a number larger than the key value, the number of the found is assigned to the last right-to-left to find the number;

arr = [27,38,04,97,76,13,97,49,55,65], then right-to-left, left-to-right, until left=right, jumps out of the loop and assigns the key value to the index value. Finally, the groups on both sides are recursively recursive.

Code:

def quick_sort (lists, left, right):    #快速排序    If left >= right:  #当递归调用的分组为1个数时返回列表        return lists    Key = Lists[left]  #保存key值, at the end of a round call, save to middle value low = left high = right #供递归调用时使用 while left    < right:< c10/> #通过下面两个循环依次交替赋值并使key值两侧为大小分组 while left < right and        Lists[right] >= key: Right-              = 1        lists[ Left] = Lists[right] While left < right and        Lists[left] <= key: Left            + + 1        lists[right] = lists[left]< C17/>lists[right] = key    quick_sort (lists, low, left-1)  #对key值左侧进行排序分组    quick_sort (lists, left+1, high)  #对key值右侧进行排序分组    return lists
  • Related Article

    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.