Fast sequencing of data structures and algorithms implemented by Python _python

Source: Internet
Author: User
Tags data structures in python

This article illustrates the fast sequencing of data structures and algorithms implemented by Python. Share to everyone for your reference. The specific analysis is as follows:

I. Overview

Quick Sort is a sort of divide-and-conquer algorithm. The algorithm first selects a partitioning element (partition element, sometimes called a pivot), and then rearranges the table to divide it into three parts: left (less than the part that divides the elements pivot), dividing elements pivot, Right (greater than the portion of the partition element pivot), at which point the dividing element pivot is already in the final position of the list, then recursively sorts the left and two parts.

Among them, the selection of elements directly affect the efficiency of the fast sorting algorithm, usually select the first element of the list or the middle element or the last element as the dividing element, of course, there are more complex selection methods; Dividing the process according to the dividing elements to rearrange the table is the key to the fast sorting algorithm, The schematic diagram of the process is as follows:

<--Select the partition element-->

<--Division Process-->

<--Division Results-->

The advantage of the fast sorting algorithm is that in-situ sequencing (using only a small auxiliary stack), the average time complexity is O (n log n). The disadvantage of the fast sorting algorithm is that it is an unstable sort algorithm, and at worst the time complexity is O (n2).

Second, Python implementation

1, the standard realization

#!/usr/bin/env python #-*-coding:utf-8-*-def stdquicksort (l): qsort (L, 0, Len (l)-1) def qsort (L, I, last): If < Last:split = partition (L, I, last) qsort (L, A, split-1) Qsort (l, split + 1, last) de F partition (L, I, last): # Select the first element in the list as the dividing element pivot = L[first] Leftmark = top + 1 RightMark = last while Tr Ue:while L[leftmark] <= pivot: # If there is an element in the list that is equal to the partition element pivot, let it be in the left part # The following detection is used to divide element pivot as the largest element in the list #防止leftma RK out of bounds if Leftmark = = Rightmark:break Leftmark = 1 while L[rightmark] > pivot: # There's no need to detect here, When dividing element pivot is the smallest element in the list, # RightMark automatically stops at the RightMark-= 1 if Leftmark < rightmark: # At this point, Leftmark
      The elements are larger than pivot, #而rightmark处的元素小于等于pivot, exchanged between L[leftmark], l[rightmark] = L[rightmark], L[leftmark] Else: Break # Swap The elements of the division with the RightMark element L[first], L[rightmark] = L[rightmark], L[first] # returns the final position of the pivot of the partition element return Righ Tmark

2, Pythonic implementation

#!/usr/bin/env python
#-*-coding:utf-8-*-
def pycquicksort (l):
  If Len (l) <= 1:return L return
  PYCQ Uicksort ([x for X on L if x < l[0]]) + \
      [x for X in L if x = = l[0]] + \
      pycquicksort ([x for X in L if x > L [0]])

Compared to the standard implementation can be seen, pythonic to achieve a more concise, more intuitive, more cool. It should be noted, however, that the Pythonic implementation uses the list parsing in Python (list comprehension, also called listing expansion, List derivation), and each recursive sort produces a new list, thus losing the advantage of the quick sort algorithm's original in-situ sequencing.

Third, the algorithm test

#!/usr/bin/env python
#-*-coding:utf-8-*-
if __name__ = ' __main__ ':
  L = [54, 26, 93, 17, 77, 31, 44, 5 5]
  M = l[:]
  print (' before Stdquicksort: ' + str (l))
  Stdquicksort (L)
  print (' after Stdquicksort: ' + str (L)
  print (' before Pycquicksort: ' + str (m))
  print (' after Pycquicksort: ' + str (pycquicksort (m))

Run Result:

$ python testquicksort.py
before stdquicksort: [
17, 20, 2] after stdquicksort: [Si, num, June, 6, before, Pycquicksort, the
following pycquicksort: [A] [a] [Si, num,,,,,,,,,,,,]
26, 31, 44, 54, 55, 77, 93]

I hope this article will help you with your Python programming.

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.