Quick sorting of data structures and algorithms implemented by Python

Source: Internet
Author: User

Quick sorting of data structures and algorithms implemented by Python

This article describes how to quickly sort data structures and algorithms implemented by Python. Share it with you for your reference. The specific analysis is as follows:

I. Overview

Quick sort is a sort algorithm. This algorithm first selects a partition element (partition element, sometimes called partition), and sorts the list into three parts: left (less than the partition element) divide the elements 'distinct' and 'right' (greater than the partition element 'distinct'). At this time, the partition element 'distinct' is already in the final position of the list, and then recursively sort the left and 'right' parts respectively.

The selection of Division elements directly affects the efficiency of the quick sorting algorithm. Generally, the first or intermediate or last element in the list is selected as the Division element, of course, there are more complex options. The division process sorts the list of elements based on the Division, which is the key to a fast sorting algorithm. The principles of this process are as follows:

<-- Select partition elements -->

<-- Division process -->

<-- Division result -->

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

Ii. Python implementation

1. Standard Implementation

#! /Usr/bin/env python #-*-coding: UTF-8-*-def stdQuicksort (L): qsort (L, 0, len (L)-1) def qsort (L, first, last): if first <last: split = partition (L, first, last) qsort (L, first, split-1) qsort (L, split + 1, last) def partition (L, first, last ): # select the first element in the list as the partitioning element Limit = L [first] leftmark = first + 1 rightmark = last while True: while L [leftmark] <= cursor: # If there is an element in the list that is equal to the partition element limit, make it in the left section # When the partition element limit is the largest element in the list, # prevent leftmark from crossing the border if leftmark = rightmark: break leftmark + = 1 while L [rightmark]> priority: # No detection is required here. When the partition element limit is the smallest element in the list, # rightmark will automatically stop at rightmark-= 1 if leftmark <rightmark: # at this time, the element at leftmark is greater than half, # The element at rightmark is less than or equal to half, exchange the two L [leftmark], L [rightmark] = L [rightmark], L [leftmark] else: break # swap the Division element at first with the element at rightmark L [first], L [rightmark] = L [rightmark], L [first] # return the final position of the partition element, return rightmark

2. Pythonic implementation

#!/usr/bin/env python# -*- coding: utf-8 -*-def pycQuicksort(L):  if len(L) <= 1: return L  return pycQuicksort([x for x in 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 with the standard implementation, we can see that Pythonic is more concise, intuitive, and cool. However, Pythonic implements List parsing (List Comprehension, also called List expansion and List derivation) in Python. A new List is generated for each recursive sorting, therefore, the advantage of the original in-situ sorting of the quick sorting algorithm is lost.

Iii. algorithm Testing

#!/usr/bin/env python# -*- coding: utf-8 -*-if __name__ == '__main__':  L = [54, 26, 93, 17, 77, 31, 44, 55, 20]  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)))

Running result:

$ python testquicksort.pybefore stdQuicksort: [54, 26, 93, 17, 77, 31, 44, 55, 20]after stdQuicksort: [17, 20, 26, 31, 44, 54, 55, 77, 93]before pycQuicksort: [54, 26, 93, 17, 77, 31, 44, 55, 20]after pycQuicksort: [17, 20, 26, 31, 44, 54, 55, 77, 93]

I hope this article will help you with Python programming.

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.