Python Quick Sort Code instance

Source: Internet
Author: User
First, the algorithm description:

1. First, a number is taken from the series as the base number.
2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.
3. Repeat the second step for the left and right intervals until there is only one number for each interval.

Second, Python quick sort code

Copy the Code code as follows:


#!/usr/bin/python
#-*-Coding:utf-8-*-

def sub_sort (Array,low,high):
Key = Array[low]
While low < high:
While low < high and Array[high] >= key:
High-= 1
While low < high and Array[high] < key:
Array[low] = Array[high]
Low + = 1
Array[high] = Array[low]
Array[low] = key
return low


def quick_sort (Array,low,high):
If low < high:
Key_index = Sub_sort (Array,low,high)
Quick_sort (Array,low,key_index)
Quick_sort (Array,key_index+1,high)


if __name__ = = ' __main__ ':
Array = [8,10,9,6,4,16,5,13,26,18,2,45,34,23,1,7,3]
Print array
Quick_sort (Array,0,len (array)-1)
Print array

Results:
[8, 10, 9, 6, 4, 16, 5, 13, 26, 18, 2, 45, 34, 23, 1, 7, 3]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 16, 18, 23, 26, 34, 45]

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