Implementing the eight-order algorithm with Python--fast sorting

Source: Internet
Author: User
I. Overview

Recently, I've been using Python to learn the classic 8 sorting algorithms to consolidate the basics while strengthening my python skills. Under the face of quick sort do a brief introduction.

Quick sort: Set the array to be sorted is a[0] ... A[n-1], first select any data (usually the first number of the array) as the key data, and then put all the smaller than it before it, all the number larger than it is placed behind it, this process is called a quick sort. It is noteworthy that fast sorting is not a stable sort algorithm, i.e., the relative position of multiple identical values may change at the end of the algorithm.
Quick Sort Algorithm Flow:
Step 1: Set two variables i,j when sorting begins, i=0,j=n-1
Step 2: The first Data element as the key data, recorded as Key=a[0]
Step 3: Start with J to search j–, find the first value less than key exchange A[j] and A[i]
Step 4: Search i++ backwards from I, find the first value greater than key exchange A[i] and A[j]
Step 5: Repeat the 3rd, 4 steps until the I=J (3,4 step, not find a qualifying value, that is, 3 of a[j] is not less than the key,4 in a[i) when not more than the key change the value of J, I, so that j=j-1,i=i+1, until found so far. Find the value that matches the condition, and the position of the J pointer is unchanged when the exchange is made. In addition, the I==J process must be exactly the time the i+ or j-completes, at which point the loop ends. Second, the sequencing Process array Demo:

Suppose the user enters the following array:
Subscript
0 1 2 3 4 5
Data
6 2 7 3 8 9

First, set the I=0,j=5,key=a[0]=6
We need to move all numbers smaller than key to the left, so start looking for smaller numbers than 6, starting with J and looking to the left, decreasing the value of the variable J. We found that the a[3]=3 was smaller than 6, so we exchanged the positions labeled 0 and 3, and the resulting new array was
Subscript
0 1 2 3 4 5
Data
3 2 7 6 8 9

At this time i=0,j=3,key=6
Next, look for a value larger than the key, and from I start to the right to search a[2]=7>6, then swap two number position
Subscript
0 1 2 3 4 5
Data
3 2 6 7 8 9

At this time i=2,j=3,key=6

The above two times compared to a cycle, then, and then decrement the variable J, repeated the above cycle comparison. Until the i=j loop ends.

Then, the data on both sides of K, and then grouped separately for the above process, until no further grouping. Three, Python implementation

#!/usr/bin/python3 #-*-coding:utf-8-*-def sub_sort (lists,low,high): key = Lists[low] While low < high: While low < high and Lists[high] >= key:high-= 1 Print ("===========") prin  T ("low=") print (low) print (the "high=") print (high) print (lists) while  Low < high and Lists[high] < Key:lists[low] = Lists[high] Low = 1 Lists[high] 
            = Lists[low] Print ("===========") print ("low=") print (low) print ("high=")
    Print (high) print (lists) Lists[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__": Lists = [3, 5, 4, 2, 1, 6] print (lists) Quick_sort (lists, 0, 5) 

Python program execution process and operation results

This article references from:
Http://www.cnblogs.com/yupeng/p/3418253.html

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.